[30d4706] | 1 | /*
|
---|
[d3109ff] | 2 | * Copyright (c) 2024 Jiri Svoboda
|
---|
[30d4706] | 3 | * Copyright (c) 2012 Vojtech Horky
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup remcons
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 | #include <async.h>
|
---|
| 36 | #include <stdio.h>
|
---|
[38d150e] | 37 | #include <stdlib.h>
|
---|
[30d4706] | 38 | #include <adt/prodcons.h>
|
---|
| 39 | #include <errno.h>
|
---|
[c23a1fe] | 40 | #include <macros.h>
|
---|
[d3109ff] | 41 | #include <mem.h>
|
---|
[30d4706] | 42 | #include <str_error.h>
|
---|
| 43 | #include <loc.h>
|
---|
| 44 | #include <io/keycode.h>
|
---|
| 45 | #include <align.h>
|
---|
| 46 | #include <as.h>
|
---|
| 47 | #include <fibril_synch.h>
|
---|
| 48 | #include <task.h>
|
---|
[fab2746] | 49 | #include <inet/tcp.h>
|
---|
[30d4706] | 50 | #include <io/console.h>
|
---|
| 51 | #include <inttypes.h>
|
---|
[8562724] | 52 | #include <assert.h>
|
---|
[d3109ff] | 53 | #include "remcons.h"
|
---|
[30d4706] | 54 | #include "user.h"
|
---|
[2a180307] | 55 | #include "telnet.h"
|
---|
[30d4706] | 56 |
|
---|
| 57 | static FIBRIL_MUTEX_INITIALIZE(users_guard);
|
---|
| 58 | static LIST_INITIALIZE(users);
|
---|
| 59 |
|
---|
[47d060d] | 60 | static errno_t telnet_user_send_raw_locked(telnet_user_t *, const void *,
|
---|
| 61 | size_t);
|
---|
| 62 | static errno_t telnet_user_flush_locked(telnet_user_t *);
|
---|
| 63 |
|
---|
[5923cf82] | 64 | /** Create new telnet user.
|
---|
| 65 | *
|
---|
[fab2746] | 66 | * @param conn Incoming connection.
|
---|
[47d060d] | 67 | * @param cb Callback functions
|
---|
| 68 | * @param arg Argument to callback functions
|
---|
[5923cf82] | 69 | * @return New telnet user or NULL when out of memory.
|
---|
| 70 | */
|
---|
[47d060d] | 71 | telnet_user_t *telnet_user_create(tcp_conn_t *conn, telnet_cb_t *cb, void *arg)
|
---|
[30d4706] | 72 | {
|
---|
| 73 | static int telnet_user_id_counter = 0;
|
---|
| 74 |
|
---|
| 75 | telnet_user_t *user = malloc(sizeof(telnet_user_t));
|
---|
| 76 | if (user == NULL) {
|
---|
| 77 | return NULL;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[47d060d] | 80 | user->cb = cb;
|
---|
| 81 | user->arg = arg;
|
---|
[30d4706] | 82 | user->id = ++telnet_user_id_counter;
|
---|
| 83 |
|
---|
[6a753a9c] | 84 | int rc = asprintf(&user->service_name, "%s/telnet%u.%d", NAMESPACE,
|
---|
| 85 | (unsigned)task_get_id(), user->id);
|
---|
[30d4706] | 86 | if (rc < 0) {
|
---|
| 87 | free(user);
|
---|
| 88 | return NULL;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[fab2746] | 91 | user->conn = conn;
|
---|
[30d4706] | 92 | user->service_id = (service_id_t) -1;
|
---|
| 93 | link_initialize(&user->link);
|
---|
| 94 | user->socket_buffer_len = 0;
|
---|
| 95 | user->socket_buffer_pos = 0;
|
---|
[c23a1fe] | 96 | user->send_buf_used = 0;
|
---|
[30d4706] | 97 |
|
---|
| 98 | fibril_condvar_initialize(&user->refcount_cv);
|
---|
[5f5d375] | 99 | fibril_mutex_initialize(&user->send_lock);
|
---|
| 100 | fibril_mutex_initialize(&user->recv_lock);
|
---|
[30d4706] | 101 | user->task_finished = false;
|
---|
| 102 | user->socket_closed = false;
|
---|
| 103 | user->locsrv_connection_count = 0;
|
---|
| 104 |
|
---|
[5d94b16c] | 105 | user->cursor_x = 0;
|
---|
[d3109ff] | 106 | user->cursor_y = 0;
|
---|
[30d4706] | 107 |
|
---|
[5d94b16c] | 108 | return user;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | void telnet_user_add(telnet_user_t *user)
|
---|
| 112 | {
|
---|
[30d4706] | 113 | fibril_mutex_lock(&users_guard);
|
---|
| 114 | list_append(&user->link, &users);
|
---|
| 115 | fibril_mutex_unlock(&users_guard);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[5923cf82] | 118 | /** Destroy telnet user structure.
|
---|
| 119 | *
|
---|
| 120 | * @param user User to be destroyed.
|
---|
| 121 | */
|
---|
[30d4706] | 122 | void telnet_user_destroy(telnet_user_t *user)
|
---|
| 123 | {
|
---|
| 124 | assert(user);
|
---|
| 125 |
|
---|
| 126 | fibril_mutex_lock(&users_guard);
|
---|
| 127 | list_remove(&user->link);
|
---|
| 128 | fibril_mutex_unlock(&users_guard);
|
---|
| 129 |
|
---|
| 130 | free(user);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[5923cf82] | 133 | /** Find user by service id and increments reference counter.
|
---|
| 134 | *
|
---|
| 135 | * @param id Location service id of the telnet user's terminal.
|
---|
| 136 | */
|
---|
[30d4706] | 137 | telnet_user_t *telnet_user_get_for_client_connection(service_id_t id)
|
---|
| 138 | {
|
---|
| 139 | telnet_user_t *user = NULL;
|
---|
| 140 |
|
---|
| 141 | fibril_mutex_lock(&users_guard);
|
---|
[feeac0d] | 142 | list_foreach(users, link, telnet_user_t, tmp) {
|
---|
[30d4706] | 143 | if (tmp->service_id == id) {
|
---|
| 144 | user = tmp;
|
---|
| 145 | break;
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | if (user == NULL) {
|
---|
| 149 | fibril_mutex_unlock(&users_guard);
|
---|
| 150 | return NULL;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | telnet_user_t *tmp = user;
|
---|
[5f5d375] | 154 | fibril_mutex_lock(&tmp->recv_lock);
|
---|
[30d4706] | 155 | user->locsrv_connection_count++;
|
---|
| 156 |
|
---|
| 157 | /*
|
---|
| 158 | * Refuse to return user whose task already finished or when
|
---|
| 159 | * the socket is already closed().
|
---|
| 160 | */
|
---|
| 161 | if (user->task_finished || user->socket_closed) {
|
---|
| 162 | user = NULL;
|
---|
| 163 | user->locsrv_connection_count--;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[5f5d375] | 166 | fibril_mutex_unlock(&tmp->recv_lock);
|
---|
[30d4706] | 167 |
|
---|
| 168 | fibril_mutex_unlock(&users_guard);
|
---|
| 169 |
|
---|
| 170 | return user;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[5923cf82] | 173 | /** Notify that client disconnected from the remote terminal.
|
---|
| 174 | *
|
---|
| 175 | * @param user To which user the client was connected.
|
---|
| 176 | */
|
---|
[8562724] | 177 | void telnet_user_notify_client_disconnected(telnet_user_t *user)
|
---|
| 178 | {
|
---|
[5f5d375] | 179 | fibril_mutex_lock(&user->recv_lock);
|
---|
[8562724] | 180 | assert(user->locsrv_connection_count > 0);
|
---|
| 181 | user->locsrv_connection_count--;
|
---|
| 182 | fibril_condvar_signal(&user->refcount_cv);
|
---|
[5f5d375] | 183 | fibril_mutex_unlock(&user->recv_lock);
|
---|
[8562724] | 184 | }
|
---|
[30d4706] | 185 |
|
---|
[99c2e9f3] | 186 | /** Tell whether the launched task already exited and socket is already closed.
|
---|
| 187 | *
|
---|
| 188 | * @param user Telnet user in question.
|
---|
| 189 | */
|
---|
| 190 | bool telnet_user_is_zombie(telnet_user_t *user)
|
---|
| 191 | {
|
---|
[5f5d375] | 192 | fibril_mutex_lock(&user->recv_lock);
|
---|
[99c2e9f3] | 193 | bool zombie = user->socket_closed || user->task_finished;
|
---|
[5f5d375] | 194 | fibril_mutex_unlock(&user->recv_lock);
|
---|
[99c2e9f3] | 195 |
|
---|
| 196 | return zombie;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[d3109ff] | 199 | static errno_t telnet_user_fill_recv_buf(telnet_user_t *user)
|
---|
| 200 | {
|
---|
| 201 | errno_t rc;
|
---|
| 202 | size_t recv_length;
|
---|
| 203 |
|
---|
| 204 | rc = tcp_conn_recv_wait(user->conn, user->socket_buffer,
|
---|
| 205 | BUFFER_SIZE, &recv_length);
|
---|
| 206 | if (rc != EOK)
|
---|
| 207 | return rc;
|
---|
| 208 |
|
---|
| 209 | if (recv_length == 0) {
|
---|
| 210 | user->socket_closed = true;
|
---|
| 211 | user->srvs.aborted = true;
|
---|
| 212 | return ENOENT;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | user->socket_buffer_len = recv_length;
|
---|
| 216 | user->socket_buffer_pos = 0;
|
---|
| 217 |
|
---|
| 218 | return EOK;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[6907f26] | 221 | /** Receive next byte from a socket (use buffering).
|
---|
| 222 | *
|
---|
| 223 | * @param user Telnet user
|
---|
| 224 | * @param byte Place to store the received byte
|
---|
| 225 | * @return EOK on success or an error code
|
---|
[99c2e9f3] | 226 | */
|
---|
[47d060d] | 227 | static errno_t telnet_user_recv_next_byte_locked(telnet_user_t *user,
|
---|
| 228 | uint8_t *byte)
|
---|
[99c2e9f3] | 229 | {
|
---|
[d3109ff] | 230 | errno_t rc;
|
---|
| 231 |
|
---|
[99c2e9f3] | 232 | /* No more buffered data? */
|
---|
| 233 | if (user->socket_buffer_len <= user->socket_buffer_pos) {
|
---|
[d3109ff] | 234 | rc = telnet_user_fill_recv_buf(user);
|
---|
[fab2746] | 235 | if (rc != EOK)
|
---|
| 236 | return rc;
|
---|
[99c2e9f3] | 237 | }
|
---|
| 238 |
|
---|
[47d060d] | 239 | *byte = (uint8_t)user->socket_buffer[user->socket_buffer_pos++];
|
---|
[d3109ff] | 240 | return EOK;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[6907f26] | 243 | /** Determine if a received byte is available without waiting.
|
---|
[99c2e9f3] | 244 | *
|
---|
[6907f26] | 245 | * @param user Telnet user
|
---|
| 246 | * @return @c true iff a byte is currently available
|
---|
[99c2e9f3] | 247 | */
|
---|
[6907f26] | 248 | static bool telnet_user_byte_avail(telnet_user_t *user)
|
---|
[1433ecda] | 249 | {
|
---|
[6907f26] | 250 | return user->socket_buffer_len > user->socket_buffer_pos;
|
---|
[99c2e9f3] | 251 | }
|
---|
| 252 |
|
---|
[47d060d] | 253 | static errno_t telnet_user_send_opt(telnet_user_t *user, telnet_cmd_t cmd,
|
---|
| 254 | telnet_cmd_t opt)
|
---|
| 255 | {
|
---|
| 256 | uint8_t cmdb[3];
|
---|
| 257 |
|
---|
| 258 | cmdb[0] = TELNET_IAC;
|
---|
| 259 | cmdb[1] = cmd;
|
---|
| 260 | cmdb[2] = opt;
|
---|
| 261 |
|
---|
| 262 | return telnet_user_send_raw_locked(user, (char *)cmdb, sizeof(cmdb));
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | /** Process telnet WILL NAWS command.
|
---|
| 266 | *
|
---|
| 267 | * @param user Telnet user structure.
|
---|
| 268 | * @param cmd Telnet command.
|
---|
| 269 | */
|
---|
| 270 | static void process_telnet_will_naws(telnet_user_t *user)
|
---|
| 271 | {
|
---|
| 272 | telnet_user_log(user, "WILL NAWS");
|
---|
| 273 | /* Send DO NAWS */
|
---|
| 274 | (void) telnet_user_send_opt(user, TELNET_DO, TELNET_NAWS);
|
---|
| 275 | (void) telnet_user_flush_locked(user);
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | /** Process telnet SB NAWS command.
|
---|
| 279 | *
|
---|
| 280 | * @param user Telnet user structure.
|
---|
| 281 | * @param cmd Telnet command.
|
---|
| 282 | */
|
---|
| 283 | static void process_telnet_sb_naws(telnet_user_t *user)
|
---|
| 284 | {
|
---|
| 285 | uint8_t chi, clo;
|
---|
| 286 | uint8_t rhi, rlo;
|
---|
| 287 | uint16_t cols;
|
---|
| 288 | uint16_t rows;
|
---|
| 289 | uint8_t iac;
|
---|
| 290 | uint8_t se;
|
---|
| 291 | errno_t rc;
|
---|
| 292 |
|
---|
| 293 | telnet_user_log(user, "SB NAWS...");
|
---|
| 294 |
|
---|
| 295 | rc = telnet_user_recv_next_byte_locked(user, &chi);
|
---|
| 296 | if (rc != EOK)
|
---|
| 297 | return;
|
---|
| 298 | rc = telnet_user_recv_next_byte_locked(user, &clo);
|
---|
| 299 | if (rc != EOK)
|
---|
| 300 | return;
|
---|
| 301 |
|
---|
| 302 | rc = telnet_user_recv_next_byte_locked(user, &rhi);
|
---|
| 303 | if (rc != EOK)
|
---|
| 304 | return;
|
---|
| 305 | rc = telnet_user_recv_next_byte_locked(user, &rlo);
|
---|
| 306 | if (rc != EOK)
|
---|
| 307 | return;
|
---|
| 308 |
|
---|
| 309 | rc = telnet_user_recv_next_byte_locked(user, &iac);
|
---|
| 310 | if (rc != EOK)
|
---|
| 311 | return;
|
---|
| 312 | rc = telnet_user_recv_next_byte_locked(user, &se);
|
---|
| 313 | if (rc != EOK)
|
---|
| 314 | return;
|
---|
| 315 |
|
---|
| 316 | cols = (chi << 8) | clo;
|
---|
| 317 | rows = (rhi << 8) | rlo;
|
---|
| 318 |
|
---|
| 319 | telnet_user_log(user, "cols=%u rows=%u\n", cols, rows);
|
---|
| 320 |
|
---|
| 321 | if (cols < 1 || rows < 1) {
|
---|
| 322 | telnet_user_log(user, "Ignoring invalid window size update.");
|
---|
| 323 | return;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | user->cb->ws_update(user->arg, cols, rows);
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | /** Process telnet WILL command.
|
---|
| 330 | *
|
---|
| 331 | * @param user Telnet user structure.
|
---|
| 332 | * @param opt Option code.
|
---|
| 333 | */
|
---|
| 334 | static void process_telnet_will(telnet_user_t *user, telnet_cmd_t opt)
|
---|
| 335 | {
|
---|
| 336 | telnet_user_log(user, "WILL");
|
---|
| 337 | switch (opt) {
|
---|
| 338 | case TELNET_NAWS:
|
---|
| 339 | process_telnet_will_naws(user);
|
---|
| 340 | return;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | telnet_user_log(user, "Ignoring telnet command %u %u %u.",
|
---|
| 344 | TELNET_IAC, TELNET_WILL, opt);
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | /** Process telnet SB command.
|
---|
| 348 | *
|
---|
| 349 | * @param user Telnet user structure.
|
---|
| 350 | * @param opt Option code.
|
---|
| 351 | */
|
---|
| 352 | static void process_telnet_sb(telnet_user_t *user, telnet_cmd_t opt)
|
---|
| 353 | {
|
---|
| 354 | telnet_user_log(user, "SB");
|
---|
| 355 | switch (opt) {
|
---|
| 356 | case TELNET_NAWS:
|
---|
| 357 | process_telnet_sb_naws(user);
|
---|
| 358 | return;
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | telnet_user_log(user, "Ignoring telnet command %u %u %u.",
|
---|
| 362 | TELNET_IAC, TELNET_SB, opt);
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | /** Process telnet command.
|
---|
[178d6a3] | 366 | *
|
---|
| 367 | * @param user Telnet user structure.
|
---|
| 368 | * @param option_code Command option code.
|
---|
| 369 | * @param cmd Telnet command.
|
---|
| 370 | */
|
---|
[2a180307] | 371 | static void process_telnet_command(telnet_user_t *user,
|
---|
| 372 | telnet_cmd_t option_code, telnet_cmd_t cmd)
|
---|
| 373 | {
|
---|
[47d060d] | 374 | switch (option_code) {
|
---|
| 375 | case TELNET_SB:
|
---|
| 376 | process_telnet_sb(user, cmd);
|
---|
| 377 | return;
|
---|
| 378 | case TELNET_WILL:
|
---|
| 379 | process_telnet_will(user, cmd);
|
---|
| 380 | return;
|
---|
| 381 | }
|
---|
| 382 |
|
---|
[2a180307] | 383 | if (option_code != 0) {
|
---|
| 384 | telnet_user_log(user, "Ignoring telnet command %u %u %u.",
|
---|
| 385 | TELNET_IAC, option_code, cmd);
|
---|
| 386 | } else {
|
---|
| 387 | telnet_user_log(user, "Ignoring telnet command %u %u.",
|
---|
| 388 | TELNET_IAC, cmd);
|
---|
| 389 | }
|
---|
| 390 | }
|
---|
| 391 |
|
---|
[6907f26] | 392 | /** Receive data from telnet connection.
|
---|
[178d6a3] | 393 | *
|
---|
| 394 | * @param user Telnet user.
|
---|
[6907f26] | 395 | * @param buf Destination buffer
|
---|
| 396 | * @param size Buffer size
|
---|
| 397 | * @param nread Place to store number of bytes read (>0 on success)
|
---|
| 398 | * @return EOK on success or an error code
|
---|
[178d6a3] | 399 | */
|
---|
[6907f26] | 400 | errno_t telnet_user_recv(telnet_user_t *user, void *buf, size_t size,
|
---|
| 401 | size_t *nread)
|
---|
[99c2e9f3] | 402 | {
|
---|
[6907f26] | 403 | uint8_t *bp = (uint8_t *)buf;
|
---|
[5f5d375] | 404 | fibril_mutex_lock(&user->recv_lock);
|
---|
[6907f26] | 405 |
|
---|
| 406 | assert(size > 0);
|
---|
| 407 | *nread = 0;
|
---|
| 408 |
|
---|
| 409 | do {
|
---|
[47d060d] | 410 | uint8_t next_byte = 0;
|
---|
[2a180307] | 411 | bool inside_telnet_command = false;
|
---|
| 412 |
|
---|
| 413 | telnet_cmd_t telnet_option_code = 0;
|
---|
| 414 |
|
---|
[99c2e9f3] | 415 | /* Skip zeros, bail-out on error. */
|
---|
[6907f26] | 416 | do {
|
---|
| 417 | errno_t rc = telnet_user_recv_next_byte_locked(user,
|
---|
| 418 | &next_byte);
|
---|
| 419 | if (rc != EOK) {
|
---|
[5f5d375] | 420 | fibril_mutex_unlock(&user->recv_lock);
|
---|
[99c2e9f3] | 421 | return rc;
|
---|
[6907f26] | 422 | }
|
---|
[47d060d] | 423 | uint8_t byte = next_byte;
|
---|
[2a180307] | 424 |
|
---|
| 425 | /* Skip telnet commands. */
|
---|
| 426 | if (inside_telnet_command) {
|
---|
| 427 | inside_telnet_command = false;
|
---|
| 428 | next_byte = 0;
|
---|
[47d060d] | 429 | if (TELNET_IS_OPTION_CODE(byte) ||
|
---|
| 430 | byte == TELNET_SB) {
|
---|
[2a180307] | 431 | telnet_option_code = byte;
|
---|
| 432 | inside_telnet_command = true;
|
---|
| 433 | } else {
|
---|
| 434 | process_telnet_command(user,
|
---|
| 435 | telnet_option_code, byte);
|
---|
| 436 | }
|
---|
| 437 | }
|
---|
| 438 | if (byte == TELNET_IAC) {
|
---|
| 439 | inside_telnet_command = true;
|
---|
| 440 | next_byte = 0;
|
---|
| 441 | }
|
---|
[6907f26] | 442 | } while (next_byte == 0 && telnet_user_byte_avail(user));
|
---|
[99c2e9f3] | 443 |
|
---|
| 444 | /* CR-LF conversions. */
|
---|
| 445 | if (next_byte == 13) {
|
---|
| 446 | next_byte = 10;
|
---|
| 447 | }
|
---|
| 448 |
|
---|
[6907f26] | 449 | if (next_byte != 0) {
|
---|
| 450 | *bp++ = next_byte;
|
---|
| 451 | ++*nread;
|
---|
| 452 | --size;
|
---|
| 453 | }
|
---|
| 454 | } while (size > 0 && (telnet_user_byte_avail(user) || *nread == 0));
|
---|
[99c2e9f3] | 455 |
|
---|
[5f5d375] | 456 | fibril_mutex_unlock(&user->recv_lock);
|
---|
[99c2e9f3] | 457 | return EOK;
|
---|
| 458 | }
|
---|
[30d4706] | 459 |
|
---|
[89e5c0c7] | 460 | static errno_t telnet_user_send_raw_locked(telnet_user_t *user,
|
---|
[c23a1fe] | 461 | const void *data, size_t size)
|
---|
| 462 | {
|
---|
| 463 | size_t remain;
|
---|
| 464 | size_t now;
|
---|
| 465 | errno_t rc;
|
---|
| 466 |
|
---|
| 467 | remain = sizeof(user->send_buf) - user->send_buf_used;
|
---|
| 468 | while (size > 0) {
|
---|
| 469 | if (remain == 0) {
|
---|
| 470 | rc = tcp_conn_send(user->conn, user->send_buf,
|
---|
| 471 | sizeof(user->send_buf));
|
---|
| 472 | if (rc != EOK)
|
---|
| 473 | return rc;
|
---|
| 474 |
|
---|
| 475 | user->send_buf_used = 0;
|
---|
| 476 | remain = sizeof(user->send_buf);
|
---|
| 477 | }
|
---|
| 478 |
|
---|
| 479 | now = min(remain, size);
|
---|
| 480 | memcpy(user->send_buf + user->send_buf_used, data, now);
|
---|
| 481 | user->send_buf_used += now;
|
---|
| 482 | remain -= now;
|
---|
| 483 | data += now;
|
---|
| 484 | size -= now;
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | return EOK;
|
---|
| 488 | }
|
---|
| 489 |
|
---|
[178d6a3] | 490 | /** Send data (convert them first) to the socket, no locking.
|
---|
| 491 | *
|
---|
| 492 | * @param user Telnet user.
|
---|
| 493 | * @param data Data buffer (not zero terminated).
|
---|
| 494 | * @param size Size of @p data buffer in bytes.
|
---|
| 495 | */
|
---|
[89e5c0c7] | 496 | static errno_t telnet_user_send_data_locked(telnet_user_t *user,
|
---|
[d3109ff] | 497 | const char *data, size_t size)
|
---|
[178d6a3] | 498 | {
|
---|
| 499 | uint8_t *converted = malloc(3 * size + 1);
|
---|
| 500 | assert(converted);
|
---|
| 501 | int converted_size = 0;
|
---|
| 502 |
|
---|
| 503 | /* Convert new-lines. */
|
---|
| 504 | for (size_t i = 0; i < size; i++) {
|
---|
| 505 | if (data[i] == 10) {
|
---|
| 506 | converted[converted_size++] = 13;
|
---|
| 507 | converted[converted_size++] = 10;
|
---|
| 508 | user->cursor_x = 0;
|
---|
[c23a1fe] | 509 | if (user->cursor_y < (int)user->rows - 1)
|
---|
[d3109ff] | 510 | ++user->cursor_y;
|
---|
[178d6a3] | 511 | } else {
|
---|
| 512 | converted[converted_size++] = data[i];
|
---|
| 513 | if (data[i] == '\b') {
|
---|
| 514 | user->cursor_x--;
|
---|
| 515 | } else {
|
---|
| 516 | user->cursor_x++;
|
---|
| 517 | }
|
---|
| 518 | }
|
---|
| 519 | }
|
---|
| 520 |
|
---|
[89e5c0c7] | 521 | errno_t rc = telnet_user_send_raw_locked(user, converted,
|
---|
| 522 | converted_size);
|
---|
[178d6a3] | 523 | free(converted);
|
---|
| 524 |
|
---|
| 525 | return rc;
|
---|
| 526 | }
|
---|
| 527 |
|
---|
| 528 | /** Send data (convert them first) to the socket.
|
---|
| 529 | *
|
---|
| 530 | * @param user Telnet user.
|
---|
| 531 | * @param data Data buffer (not zero terminated).
|
---|
| 532 | * @param size Size of @p data buffer in bytes.
|
---|
| 533 | */
|
---|
[d3109ff] | 534 | errno_t telnet_user_send_data(telnet_user_t *user, const char *data,
|
---|
| 535 | size_t size)
|
---|
[178d6a3] | 536 | {
|
---|
[5f5d375] | 537 | fibril_mutex_lock(&user->send_lock);
|
---|
[178d6a3] | 538 |
|
---|
[89e5c0c7] | 539 | errno_t rc = telnet_user_send_data_locked(user, data, size);
|
---|
| 540 |
|
---|
[5f5d375] | 541 | fibril_mutex_unlock(&user->send_lock);
|
---|
[89e5c0c7] | 542 |
|
---|
| 543 | return rc;
|
---|
| 544 | }
|
---|
| 545 |
|
---|
| 546 | /** Send raw non-printable data to the socket.
|
---|
| 547 | *
|
---|
| 548 | * @param user Telnet user.
|
---|
| 549 | * @param data Data buffer (not zero terminated).
|
---|
| 550 | * @param size Size of @p data buffer in bytes.
|
---|
| 551 | */
|
---|
| 552 | errno_t telnet_user_send_raw(telnet_user_t *user, const char *data,
|
---|
| 553 | size_t size)
|
---|
| 554 | {
|
---|
[5f5d375] | 555 | fibril_mutex_lock(&user->send_lock);
|
---|
[89e5c0c7] | 556 |
|
---|
| 557 | errno_t rc = telnet_user_send_raw_locked(user, data, size);
|
---|
[178d6a3] | 558 |
|
---|
[5f5d375] | 559 | fibril_mutex_unlock(&user->send_lock);
|
---|
[178d6a3] | 560 |
|
---|
| 561 | return rc;
|
---|
| 562 | }
|
---|
| 563 |
|
---|
[47d060d] | 564 | static errno_t telnet_user_flush_locked(telnet_user_t *user)
|
---|
[c23a1fe] | 565 | {
|
---|
| 566 | errno_t rc;
|
---|
| 567 |
|
---|
| 568 | rc = tcp_conn_send(user->conn, user->send_buf, user->send_buf_used);
|
---|
[47d060d] | 569 | if (rc != EOK)
|
---|
[c23a1fe] | 570 | return rc;
|
---|
| 571 |
|
---|
| 572 | user->send_buf_used = 0;
|
---|
| 573 | return EOK;
|
---|
| 574 | }
|
---|
| 575 |
|
---|
[47d060d] | 576 | errno_t telnet_user_flush(telnet_user_t *user)
|
---|
| 577 | {
|
---|
| 578 | errno_t rc;
|
---|
| 579 |
|
---|
[5f5d375] | 580 | fibril_mutex_lock(&user->send_lock);
|
---|
[47d060d] | 581 | rc = telnet_user_flush_locked(user);
|
---|
[5f5d375] | 582 | fibril_mutex_unlock(&user->send_lock);
|
---|
[47d060d] | 583 | return rc;
|
---|
| 584 | }
|
---|
| 585 |
|
---|
[178d6a3] | 586 | /** Update cursor X position.
|
---|
| 587 | *
|
---|
| 588 | * This call may result in sending control commands over socket.
|
---|
| 589 | *
|
---|
| 590 | * @param user Telnet user.
|
---|
| 591 | * @param new_x New cursor location.
|
---|
| 592 | */
|
---|
| 593 | void telnet_user_update_cursor_x(telnet_user_t *user, int new_x)
|
---|
| 594 | {
|
---|
[5f5d375] | 595 | fibril_mutex_lock(&user->send_lock);
|
---|
[178d6a3] | 596 | if (user->cursor_x - 1 == new_x) {
|
---|
[d3109ff] | 597 | char data = '\b';
|
---|
[178d6a3] | 598 | /* Ignore errors. */
|
---|
[89e5c0c7] | 599 | telnet_user_send_data_locked(user, &data, 1);
|
---|
[178d6a3] | 600 | }
|
---|
| 601 | user->cursor_x = new_x;
|
---|
[5f5d375] | 602 | fibril_mutex_unlock(&user->send_lock);
|
---|
[178d6a3] | 603 |
|
---|
| 604 | }
|
---|
| 605 |
|
---|
[47d060d] | 606 | /** Resize telnet session.
|
---|
| 607 | *
|
---|
| 608 | * @param user Telnet user
|
---|
| 609 | * @param cols New number of columns
|
---|
| 610 | * @param rows New number of rows
|
---|
| 611 | */
|
---|
| 612 | void telnet_user_resize(telnet_user_t *user, unsigned cols, unsigned rows)
|
---|
| 613 | {
|
---|
| 614 | user->cols = cols;
|
---|
| 615 | user->rows = rows;
|
---|
| 616 | if ((unsigned)user->cursor_x > cols - 1)
|
---|
| 617 | user->cursor_x = cols - 1;
|
---|
| 618 | if ((unsigned)user->cursor_y > rows - 1)
|
---|
| 619 | user->cursor_y = rows - 1;
|
---|
| 620 | }
|
---|
| 621 |
|
---|
[30d4706] | 622 | /**
|
---|
| 623 | * @}
|
---|
| 624 | */
|
---|