| 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 <stdio.h>
|
|---|
| 37 | #include <adt/prodcons.h>
|
|---|
| 38 | #include <ipc/input.h>
|
|---|
| 39 | #include <ipc/console.h>
|
|---|
| 40 | #include <ipc/vfs.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 | #include <str_error.h>
|
|---|
| 43 | #include <loc.h>
|
|---|
| 44 | #include <event.h>
|
|---|
| 45 | #include <io/keycode.h>
|
|---|
| 46 | #include <align.h>
|
|---|
| 47 | #include <malloc.h>
|
|---|
| 48 | #include <as.h>
|
|---|
| 49 | #include <fibril_synch.h>
|
|---|
| 50 | #include <task.h>
|
|---|
| 51 | #include <net/in.h>
|
|---|
| 52 | #include <net/inet.h>
|
|---|
| 53 | #include <net/socket.h>
|
|---|
| 54 | #include <io/console.h>
|
|---|
| 55 | #include <inttypes.h>
|
|---|
| 56 | #include "telnet.h"
|
|---|
| 57 | #include "user.h"
|
|---|
| 58 |
|
|---|
| 59 | #define APP_GETTERM "/app/getterm"
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 | /** Telnet commands to force character mode
|
|---|
| 63 | * (redundant to be on the safe side).
|
|---|
| 64 | * See
|
|---|
| 65 | * http://stackoverflow.com/questions/273261/force-telnet-user-into-character-mode
|
|---|
| 66 | * for discussion.
|
|---|
| 67 | */
|
|---|
| 68 | static const telnet_cmd_t telnet_force_character_mode_command[] = {
|
|---|
| 69 | TELNET_IAC, TELNET_WILL, TELNET_ECHO,
|
|---|
| 70 | TELNET_IAC, TELNET_WILL, TELNET_SUPPRESS_GO_AHEAD,
|
|---|
| 71 | TELNET_IAC, TELNET_WONT, TELNET_LINEMODE
|
|---|
| 72 | };
|
|---|
| 73 | static const size_t telnet_force_character_mode_command_count =
|
|---|
| 74 | sizeof(telnet_force_character_mode_command) / sizeof(telnet_cmd_t);
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 | static kbd_event_t* new_kbd_event(kbd_event_type_t type, wchar_t c) {
|
|---|
| 78 | kbd_event_t *event = malloc(sizeof(kbd_event_t));
|
|---|
| 79 | assert(event);
|
|---|
| 80 |
|
|---|
| 81 | link_initialize(&event->link);
|
|---|
| 82 | event->type = type;
|
|---|
| 83 | event->c = c;
|
|---|
| 84 | event->mods = 0;
|
|---|
| 85 | event->key = (c == '\n' ? KC_ENTER : KC_A);
|
|---|
| 86 |
|
|---|
| 87 | return event;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | static void client_connection_message_loop(telnet_user_t *user)
|
|---|
| 91 | {
|
|---|
| 92 | while (true) {
|
|---|
| 93 | ipc_call_t call;
|
|---|
| 94 | ipc_callid_t callid = 0;
|
|---|
| 95 | while (callid == 0) {
|
|---|
| 96 | callid = async_get_call_timeout(&call, 1000);
|
|---|
| 97 |
|
|---|
| 98 | fibril_mutex_lock(&user->refcount_mutex);
|
|---|
| 99 | bool bail_out = user->socket_closed || user->task_finished;
|
|---|
| 100 | fibril_mutex_unlock(&user->refcount_mutex);
|
|---|
| 101 |
|
|---|
| 102 | if (bail_out) {
|
|---|
| 103 | if (callid != 0) {
|
|---|
| 104 | async_answer_0(callid, EINTR);
|
|---|
| 105 | }
|
|---|
| 106 | return;
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | if (!IPC_GET_IMETHOD(call)) {
|
|---|
| 111 | /* Clean-up. */
|
|---|
| 112 | return;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| 116 | case CONSOLE_GET_SIZE:
|
|---|
| 117 | async_answer_2(callid, EOK, 100, 1);
|
|---|
| 118 | break;
|
|---|
| 119 | case CONSOLE_GET_POS:
|
|---|
| 120 | async_answer_2(callid, EOK, 0, 0);
|
|---|
| 121 | break;
|
|---|
| 122 | case CONSOLE_GET_EVENT: {
|
|---|
| 123 | if (list_empty(&user->in_events.list)) {
|
|---|
| 124 | retry:
|
|---|
| 125 | if (user->socket_buffer_len <= user->socket_buffer_pos) {
|
|---|
| 126 | int recv_length = recv(user->socket, user->socket_buffer, BUFFER_SIZE, 0);
|
|---|
| 127 | if ((recv_length == 0) || (recv_length == ENOTCONN)) {
|
|---|
| 128 | fibril_mutex_lock(&user->refcount_mutex);
|
|---|
| 129 | user->socket_closed = true;
|
|---|
| 130 | fibril_mutex_unlock(&user->refcount_mutex);
|
|---|
| 131 | async_answer_0(callid, ENOENT);
|
|---|
| 132 | return;
|
|---|
| 133 | }
|
|---|
| 134 | if (recv_length < 0) {
|
|---|
| 135 | async_answer_0(callid, EINVAL);
|
|---|
| 136 | return;
|
|---|
| 137 | }
|
|---|
| 138 | user->socket_buffer_len = recv_length;
|
|---|
| 139 | user->socket_buffer_pos = 0;
|
|---|
| 140 | }
|
|---|
| 141 | char data = user->socket_buffer[user->socket_buffer_pos++];
|
|---|
| 142 | if (data == 13) {
|
|---|
| 143 | data = 10;
|
|---|
| 144 | }
|
|---|
| 145 | if (data == 0)
|
|---|
| 146 | goto retry;
|
|---|
| 147 |
|
|---|
| 148 | kbd_event_t *down = new_kbd_event(KEY_PRESS, data);
|
|---|
| 149 | kbd_event_t *up = new_kbd_event(KEY_RELEASE, data);
|
|---|
| 150 | assert(down);
|
|---|
| 151 | assert(up);
|
|---|
| 152 | prodcons_produce(&user->in_events, &down->link);
|
|---|
| 153 | prodcons_produce(&user->in_events, &up->link);
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 | link_t *link = prodcons_consume(&user->in_events);
|
|---|
| 158 | kbd_event_t *event = list_get_instance(link, kbd_event_t, link);
|
|---|
| 159 | async_answer_4(callid, EOK, event->type, event->key, event->mods, event->c);
|
|---|
| 160 | free(event);
|
|---|
| 161 | break;
|
|---|
| 162 | }
|
|---|
| 163 | case CONSOLE_GOTO:
|
|---|
| 164 | async_answer_0(callid, ENOTSUP);
|
|---|
| 165 | break;
|
|---|
| 166 | case VFS_OUT_READ:
|
|---|
| 167 | async_answer_0(callid, ENOTSUP);
|
|---|
| 168 | break;
|
|---|
| 169 | case VFS_OUT_WRITE: {
|
|---|
| 170 | char *buf;
|
|---|
| 171 | char *buf_converted;
|
|---|
| 172 | size_t size;
|
|---|
| 173 | int rc = async_data_write_accept((void **)&buf, false, 0, 0, 0, &size);
|
|---|
| 174 |
|
|---|
| 175 | if (rc != EOK) {
|
|---|
| 176 | async_answer_0(callid, rc);
|
|---|
| 177 | break;
|
|---|
| 178 | }
|
|---|
| 179 | buf_converted = malloc(2 * size);
|
|---|
| 180 | assert(buf_converted);
|
|---|
| 181 | int buf_converted_size = 0;
|
|---|
| 182 | /* Convert new-lines. */
|
|---|
| 183 | for (size_t i = 0; i < size; i++) {
|
|---|
| 184 | if (buf[i] == 10) {
|
|---|
| 185 | buf_converted[buf_converted_size++] = 13;
|
|---|
| 186 | buf_converted[buf_converted_size++] = 10;
|
|---|
| 187 | } else {
|
|---|
| 188 | buf_converted[buf_converted_size++] = buf[i];
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| 191 | rc = send(user->socket, buf_converted, buf_converted_size, 0);
|
|---|
| 192 | free(buf);
|
|---|
| 193 |
|
|---|
| 194 | if (rc != EOK) {
|
|---|
| 195 | printf("Problem sending data: %s\n", str_error(rc));
|
|---|
| 196 | async_answer_0(callid, rc);
|
|---|
| 197 | break;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | async_answer_1(callid, EOK, size);
|
|---|
| 201 |
|
|---|
| 202 | break;
|
|---|
| 203 | }
|
|---|
| 204 | case VFS_OUT_SYNC:
|
|---|
| 205 | async_answer_0(callid, EOK);
|
|---|
| 206 | break;
|
|---|
| 207 | case CONSOLE_CLEAR:
|
|---|
| 208 | async_answer_0(callid, EOK);
|
|---|
| 209 | break;
|
|---|
| 210 |
|
|---|
| 211 | case CONSOLE_GET_COLOR_CAP:
|
|---|
| 212 | async_answer_1(callid, EOK, CONSOLE_CAP_NONE);
|
|---|
| 213 | break;
|
|---|
| 214 | case CONSOLE_SET_STYLE:
|
|---|
| 215 | async_answer_0(callid, ENOTSUP);
|
|---|
| 216 | break;
|
|---|
| 217 | case CONSOLE_SET_COLOR:
|
|---|
| 218 | async_answer_0(callid, ENOTSUP);
|
|---|
| 219 | break;
|
|---|
| 220 | case CONSOLE_SET_RGB_COLOR:
|
|---|
| 221 | async_answer_0(callid, ENOTSUP);
|
|---|
| 222 | break;
|
|---|
| 223 |
|
|---|
| 224 | case CONSOLE_CURSOR_VISIBILITY:
|
|---|
| 225 | async_answer_0(callid, ENOTSUP);
|
|---|
| 226 | break;
|
|---|
| 227 |
|
|---|
| 228 | default:
|
|---|
| 229 | async_answer_0(callid, EINVAL);
|
|---|
| 230 | break;
|
|---|
| 231 | }
|
|---|
| 232 | }
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | static void client_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
|---|
| 236 | {
|
|---|
| 237 | /* Find the user. */
|
|---|
| 238 | telnet_user_t *user = telnet_user_get_for_client_connection(IPC_GET_ARG1(*icall));
|
|---|
| 239 | if (user == NULL) {
|
|---|
| 240 | async_answer_0(iid, ENOENT);
|
|---|
| 241 | return;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | printf("New user for service %s.\n", user->service_name);
|
|---|
| 245 |
|
|---|
| 246 | /* Accept the connection, increment reference. */
|
|---|
| 247 | async_answer_0(iid, EOK);
|
|---|
| 248 |
|
|---|
| 249 | /* Force character mode. */
|
|---|
| 250 | send(user->socket, (void *)telnet_force_character_mode_command,
|
|---|
| 251 | telnet_force_character_mode_command_count, 0);
|
|---|
| 252 |
|
|---|
| 253 | client_connection_message_loop(user);
|
|---|
| 254 |
|
|---|
| 255 | /* Announce user disconnection. */
|
|---|
| 256 | telnet_user_notify_client_disconnected(user);
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | static int spawn_task_fibril(void *arg)
|
|---|
| 260 | {
|
|---|
| 261 | telnet_user_t *user = arg;
|
|---|
| 262 | int rc;
|
|---|
| 263 |
|
|---|
| 264 | char term[LOC_NAME_MAXLEN];
|
|---|
| 265 | snprintf(term, LOC_NAME_MAXLEN, "%s/%s", "/loc", user->service_name);
|
|---|
| 266 |
|
|---|
| 267 | task_id_t task;
|
|---|
| 268 | rc = task_spawnl(&task, APP_GETTERM, APP_GETTERM, term, "/app/bdsh", NULL);
|
|---|
| 269 | if (rc != EOK) {
|
|---|
| 270 | printf("%s: Error spawning %s -w %s %s (%s)\n", NAME,
|
|---|
| 271 | APP_GETTERM, term, "/app/bdsh", str_error(rc));
|
|---|
| 272 | fibril_mutex_lock(&user->refcount_mutex);
|
|---|
| 273 | user->task_finished = true;
|
|---|
| 274 | fibril_condvar_signal(&user->refcount_cv);
|
|---|
| 275 | fibril_mutex_unlock(&user->refcount_mutex);
|
|---|
| 276 | return EOK;
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | fibril_mutex_lock(&user->refcount_mutex);
|
|---|
| 280 | user->task_id = task;
|
|---|
| 281 | fibril_mutex_unlock(&user->refcount_mutex);
|
|---|
| 282 |
|
|---|
| 283 | task_exit_t task_exit;
|
|---|
| 284 | int task_retval;
|
|---|
| 285 | task_wait(task, &task_exit, &task_retval);
|
|---|
| 286 | printf("%s: getterm terminated: %d, %d\n", NAME, task_exit, task_retval);
|
|---|
| 287 |
|
|---|
| 288 | /* Announce destruction. */
|
|---|
| 289 | fibril_mutex_lock(&user->refcount_mutex);
|
|---|
| 290 | user->task_finished = true;
|
|---|
| 291 | fibril_condvar_signal(&user->refcount_cv);
|
|---|
| 292 | fibril_mutex_unlock(&user->refcount_mutex);
|
|---|
| 293 |
|
|---|
| 294 | return EOK;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | static bool user_can_be_destroyed_no_lock(telnet_user_t *user)
|
|---|
| 298 | {
|
|---|
| 299 | return user->task_finished && user->socket_closed &&
|
|---|
| 300 | (user->locsrv_connection_count == 0);
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | static int network_user_fibril(void *arg)
|
|---|
| 304 | {
|
|---|
| 305 | int rc;
|
|---|
| 306 | telnet_user_t *user = arg;
|
|---|
| 307 |
|
|---|
| 308 | rc = loc_service_register(user->service_name, &user->service_id);
|
|---|
| 309 | if (rc != EOK) {
|
|---|
| 310 | fprintf(stderr, "%s: Unable to register device %s\n", NAME,
|
|---|
| 311 | user->service_name);
|
|---|
| 312 | return EOK;
|
|---|
| 313 | }
|
|---|
| 314 | printf("Service %s registered as %" PRIun "\n", user->service_name,
|
|---|
| 315 | user->service_id);
|
|---|
| 316 |
|
|---|
| 317 | fid_t spawn_fibril = fibril_create(spawn_task_fibril, user);
|
|---|
| 318 | assert(spawn_fibril);
|
|---|
| 319 | fibril_add_ready(spawn_fibril);
|
|---|
| 320 |
|
|---|
| 321 | /* Wait for all clients to exit. */
|
|---|
| 322 | fibril_mutex_lock(&user->refcount_mutex);
|
|---|
| 323 | while (!user_can_be_destroyed_no_lock(user)) {
|
|---|
| 324 | if (user->task_finished) {
|
|---|
| 325 | closesocket(user->socket);
|
|---|
| 326 | user->socket_closed = true;
|
|---|
| 327 | continue;
|
|---|
| 328 | } else if (user->socket_closed) {
|
|---|
| 329 | if (user->task_id != 0) {
|
|---|
| 330 | task_kill(user->task_id);
|
|---|
| 331 | }
|
|---|
| 332 | }
|
|---|
| 333 | fibril_condvar_wait_timeout(&user->refcount_cv, &user->refcount_mutex, 1000);
|
|---|
| 334 | }
|
|---|
| 335 | fibril_mutex_unlock(&user->refcount_mutex);
|
|---|
| 336 |
|
|---|
| 337 | rc = loc_service_unregister(user->service_id);
|
|---|
| 338 | if (rc != EOK) {
|
|---|
| 339 | fprintf(stderr, "Warning: failed to unregister %s: %s\n", user->service_name, str_error(rc));
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | printf("Destroying service %s.\n", user->service_name);
|
|---|
| 343 | telnet_user_destroy(user);
|
|---|
| 344 |
|
|---|
| 345 | return EOK;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | int main(int argc, char *argv[])
|
|---|
| 349 | {
|
|---|
| 350 | int port = 2223;
|
|---|
| 351 |
|
|---|
| 352 | int rc = loc_server_register(NAME, client_connection);
|
|---|
| 353 | if (rc < 0) {
|
|---|
| 354 | printf("%s: Unable to register server (%s).\n", NAME,
|
|---|
| 355 | str_error(rc));
|
|---|
| 356 | return 1;
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | struct sockaddr_in addr;
|
|---|
| 360 |
|
|---|
| 361 | addr.sin_family = AF_INET;
|
|---|
| 362 | addr.sin_port = htons(port);
|
|---|
| 363 |
|
|---|
| 364 | rc = inet_pton(AF_INET, "127.0.0.1", (void *)
|
|---|
| 365 | &addr.sin_addr.s_addr);
|
|---|
| 366 | if (rc != EOK) {
|
|---|
| 367 | fprintf(stderr, "Error parsing network address (%s)\n",
|
|---|
| 368 | str_error(rc));
|
|---|
| 369 | return 2;
|
|---|
| 370 | }
|
|---|
| 371 |
|
|---|
| 372 | int listen_sd = socket(PF_INET, SOCK_STREAM, 0);
|
|---|
| 373 | if (listen_sd < 0) {
|
|---|
| 374 | fprintf(stderr, "Error creating listening socket (%s)\n",
|
|---|
| 375 | str_error(listen_sd));
|
|---|
| 376 | return 3;
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | rc = bind(listen_sd, (struct sockaddr *) &addr, sizeof(addr));
|
|---|
| 380 | if (rc != EOK) {
|
|---|
| 381 | fprintf(stderr, "Error binding socket (%s)\n",
|
|---|
| 382 | str_error(rc));
|
|---|
| 383 | return 4;
|
|---|
| 384 | }
|
|---|
| 385 |
|
|---|
| 386 | rc = listen(listen_sd, BACKLOG_SIZE);
|
|---|
| 387 | if (rc != EOK) {
|
|---|
| 388 | fprintf(stderr, "listen() failed (%s)\n", str_error(rc));
|
|---|
| 389 | return 5;
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | printf("%s: HelenOS Remote console service\n", NAME);
|
|---|
| 393 |
|
|---|
| 394 | while (true) {
|
|---|
| 395 | struct sockaddr_in raddr;
|
|---|
| 396 | socklen_t raddr_len = sizeof(raddr);
|
|---|
| 397 | int conn_sd = accept(listen_sd, (struct sockaddr *) &raddr,
|
|---|
| 398 | &raddr_len);
|
|---|
| 399 |
|
|---|
| 400 | if (conn_sd < 0) {
|
|---|
| 401 | fprintf(stderr, "accept() failed (%s)\n", str_error(rc));
|
|---|
| 402 | continue;
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | telnet_user_t *user = telnet_user_create(conn_sd);
|
|---|
| 406 | assert(user);
|
|---|
| 407 |
|
|---|
| 408 | fid_t fid = fibril_create(network_user_fibril, user);
|
|---|
| 409 | assert(fid);
|
|---|
| 410 | fibril_add_ready(fid);
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | return 0;
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 | /** @}
|
|---|
| 417 | */
|
|---|