1 | /*
|
---|
2 | * Copyright (c) 2024 Jiri Svoboda
|
---|
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>
|
---|
37 | #include <stdlib.h>
|
---|
38 | #include <adt/prodcons.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <macros.h>
|
---|
41 | #include <mem.h>
|
---|
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>
|
---|
49 | #include <inet/tcp.h>
|
---|
50 | #include <io/console.h>
|
---|
51 | #include <inttypes.h>
|
---|
52 | #include <assert.h>
|
---|
53 | #include "remcons.h"
|
---|
54 | #include "user.h"
|
---|
55 | #include "telnet.h"
|
---|
56 |
|
---|
57 | static FIBRIL_MUTEX_INITIALIZE(users_guard);
|
---|
58 | static LIST_INITIALIZE(users);
|
---|
59 |
|
---|
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 |
|
---|
64 | /** Create new telnet user.
|
---|
65 | *
|
---|
66 | * @param conn Incoming connection.
|
---|
67 | * @param cb Callback functions
|
---|
68 | * @param arg Argument to callback functions
|
---|
69 | * @return New telnet user or NULL when out of memory.
|
---|
70 | */
|
---|
71 | telnet_user_t *telnet_user_create(tcp_conn_t *conn, telnet_cb_t *cb, void *arg)
|
---|
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 |
|
---|
80 | user->cb = cb;
|
---|
81 | user->arg = arg;
|
---|
82 | user->id = ++telnet_user_id_counter;
|
---|
83 |
|
---|
84 | int rc = asprintf(&user->service_name, "%s/telnet%u.%d", NAMESPACE,
|
---|
85 | (unsigned)task_get_id(), user->id);
|
---|
86 | if (rc < 0) {
|
---|
87 | free(user);
|
---|
88 | return NULL;
|
---|
89 | }
|
---|
90 |
|
---|
91 | user->conn = conn;
|
---|
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;
|
---|
96 | user->send_buf_used = 0;
|
---|
97 |
|
---|
98 | fibril_condvar_initialize(&user->refcount_cv);
|
---|
99 | fibril_mutex_initialize(&user->send_lock);
|
---|
100 | fibril_mutex_initialize(&user->recv_lock);
|
---|
101 | user->task_finished = false;
|
---|
102 | user->socket_closed = false;
|
---|
103 | user->locsrv_connection_count = 0;
|
---|
104 |
|
---|
105 | user->cursor_x = 0;
|
---|
106 | user->cursor_y = 0;
|
---|
107 |
|
---|
108 | return user;
|
---|
109 | }
|
---|
110 |
|
---|
111 | void telnet_user_add(telnet_user_t *user)
|
---|
112 | {
|
---|
113 | fibril_mutex_lock(&users_guard);
|
---|
114 | list_append(&user->link, &users);
|
---|
115 | fibril_mutex_unlock(&users_guard);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /** Destroy telnet user structure.
|
---|
119 | *
|
---|
120 | * @param user User to be destroyed.
|
---|
121 | */
|
---|
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 |
|
---|
133 | /** Find user by service id and increments reference counter.
|
---|
134 | *
|
---|
135 | * @param id Location service id of the telnet user's terminal.
|
---|
136 | */
|
---|
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);
|
---|
142 | list_foreach(users, link, telnet_user_t, tmp) {
|
---|
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;
|
---|
154 | fibril_mutex_lock(&tmp->recv_lock);
|
---|
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 |
|
---|
166 | fibril_mutex_unlock(&tmp->recv_lock);
|
---|
167 |
|
---|
168 | fibril_mutex_unlock(&users_guard);
|
---|
169 |
|
---|
170 | return user;
|
---|
171 | }
|
---|
172 |
|
---|
173 | /** Notify that client disconnected from the remote terminal.
|
---|
174 | *
|
---|
175 | * @param user To which user the client was connected.
|
---|
176 | */
|
---|
177 | void telnet_user_notify_client_disconnected(telnet_user_t *user)
|
---|
178 | {
|
---|
179 | fibril_mutex_lock(&user->recv_lock);
|
---|
180 | assert(user->locsrv_connection_count > 0);
|
---|
181 | user->locsrv_connection_count--;
|
---|
182 | fibril_condvar_signal(&user->refcount_cv);
|
---|
183 | fibril_mutex_unlock(&user->recv_lock);
|
---|
184 | }
|
---|
185 |
|
---|
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 | {
|
---|
192 | fibril_mutex_lock(&user->recv_lock);
|
---|
193 | bool zombie = user->socket_closed || user->task_finished;
|
---|
194 | fibril_mutex_unlock(&user->recv_lock);
|
---|
195 |
|
---|
196 | return zombie;
|
---|
197 | }
|
---|
198 |
|
---|
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 |
|
---|
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
|
---|
226 | */
|
---|
227 | static errno_t telnet_user_recv_next_byte_locked(telnet_user_t *user,
|
---|
228 | uint8_t *byte)
|
---|
229 | {
|
---|
230 | errno_t rc;
|
---|
231 |
|
---|
232 | /* No more buffered data? */
|
---|
233 | if (user->socket_buffer_len <= user->socket_buffer_pos) {
|
---|
234 | rc = telnet_user_fill_recv_buf(user);
|
---|
235 | if (rc != EOK)
|
---|
236 | return rc;
|
---|
237 | }
|
---|
238 |
|
---|
239 | *byte = (uint8_t)user->socket_buffer[user->socket_buffer_pos++];
|
---|
240 | return EOK;
|
---|
241 | }
|
---|
242 |
|
---|
243 | /** Determine if a received byte is available without waiting.
|
---|
244 | *
|
---|
245 | * @param user Telnet user
|
---|
246 | * @return @c true iff a byte is currently available
|
---|
247 | */
|
---|
248 | static bool telnet_user_byte_avail(telnet_user_t *user)
|
---|
249 | {
|
---|
250 | return user->socket_buffer_len > user->socket_buffer_pos;
|
---|
251 | }
|
---|
252 |
|
---|
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.
|
---|
366 | *
|
---|
367 | * @param user Telnet user structure.
|
---|
368 | * @param option_code Command option code.
|
---|
369 | * @param cmd Telnet command.
|
---|
370 | */
|
---|
371 | static void process_telnet_command(telnet_user_t *user,
|
---|
372 | telnet_cmd_t option_code, telnet_cmd_t cmd)
|
---|
373 | {
|
---|
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 |
|
---|
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 |
|
---|
392 | /** Receive data from telnet connection.
|
---|
393 | *
|
---|
394 | * @param user Telnet user.
|
---|
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
|
---|
399 | */
|
---|
400 | errno_t telnet_user_recv(telnet_user_t *user, void *buf, size_t size,
|
---|
401 | size_t *nread)
|
---|
402 | {
|
---|
403 | uint8_t *bp = (uint8_t *)buf;
|
---|
404 | fibril_mutex_lock(&user->recv_lock);
|
---|
405 |
|
---|
406 | assert(size > 0);
|
---|
407 | *nread = 0;
|
---|
408 |
|
---|
409 | do {
|
---|
410 | uint8_t next_byte = 0;
|
---|
411 | bool inside_telnet_command = false;
|
---|
412 |
|
---|
413 | telnet_cmd_t telnet_option_code = 0;
|
---|
414 |
|
---|
415 | /* Skip zeros, bail-out on error. */
|
---|
416 | do {
|
---|
417 | errno_t rc = telnet_user_recv_next_byte_locked(user,
|
---|
418 | &next_byte);
|
---|
419 | if (rc != EOK) {
|
---|
420 | fibril_mutex_unlock(&user->recv_lock);
|
---|
421 | return rc;
|
---|
422 | }
|
---|
423 | uint8_t byte = next_byte;
|
---|
424 |
|
---|
425 | /* Skip telnet commands. */
|
---|
426 | if (inside_telnet_command) {
|
---|
427 | inside_telnet_command = false;
|
---|
428 | next_byte = 0;
|
---|
429 | if (TELNET_IS_OPTION_CODE(byte) ||
|
---|
430 | byte == TELNET_SB) {
|
---|
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 | }
|
---|
442 | } while (next_byte == 0 && telnet_user_byte_avail(user));
|
---|
443 |
|
---|
444 | /* CR-LF conversions. */
|
---|
445 | if (next_byte == 13) {
|
---|
446 | next_byte = 10;
|
---|
447 | }
|
---|
448 |
|
---|
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));
|
---|
455 |
|
---|
456 | fibril_mutex_unlock(&user->recv_lock);
|
---|
457 | return EOK;
|
---|
458 | }
|
---|
459 |
|
---|
460 | static errno_t telnet_user_send_raw_locked(telnet_user_t *user,
|
---|
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 |
|
---|
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 | */
|
---|
496 | static errno_t telnet_user_send_data_locked(telnet_user_t *user,
|
---|
497 | const char *data, size_t size)
|
---|
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;
|
---|
509 | if (user->cursor_y < (int)user->rows - 1)
|
---|
510 | ++user->cursor_y;
|
---|
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 |
|
---|
521 | errno_t rc = telnet_user_send_raw_locked(user, converted,
|
---|
522 | converted_size);
|
---|
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 | */
|
---|
534 | errno_t telnet_user_send_data(telnet_user_t *user, const char *data,
|
---|
535 | size_t size)
|
---|
536 | {
|
---|
537 | fibril_mutex_lock(&user->send_lock);
|
---|
538 |
|
---|
539 | errno_t rc = telnet_user_send_data_locked(user, data, size);
|
---|
540 |
|
---|
541 | fibril_mutex_unlock(&user->send_lock);
|
---|
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 | {
|
---|
555 | fibril_mutex_lock(&user->send_lock);
|
---|
556 |
|
---|
557 | errno_t rc = telnet_user_send_raw_locked(user, data, size);
|
---|
558 |
|
---|
559 | fibril_mutex_unlock(&user->send_lock);
|
---|
560 |
|
---|
561 | return rc;
|
---|
562 | }
|
---|
563 |
|
---|
564 | static errno_t telnet_user_flush_locked(telnet_user_t *user)
|
---|
565 | {
|
---|
566 | errno_t rc;
|
---|
567 |
|
---|
568 | rc = tcp_conn_send(user->conn, user->send_buf, user->send_buf_used);
|
---|
569 | if (rc != EOK)
|
---|
570 | return rc;
|
---|
571 |
|
---|
572 | user->send_buf_used = 0;
|
---|
573 | return EOK;
|
---|
574 | }
|
---|
575 |
|
---|
576 | errno_t telnet_user_flush(telnet_user_t *user)
|
---|
577 | {
|
---|
578 | errno_t rc;
|
---|
579 |
|
---|
580 | fibril_mutex_lock(&user->send_lock);
|
---|
581 | rc = telnet_user_flush_locked(user);
|
---|
582 | fibril_mutex_unlock(&user->send_lock);
|
---|
583 | return rc;
|
---|
584 | }
|
---|
585 |
|
---|
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 | {
|
---|
595 | fibril_mutex_lock(&user->send_lock);
|
---|
596 | if (user->cursor_x - 1 == new_x) {
|
---|
597 | char data = '\b';
|
---|
598 | /* Ignore errors. */
|
---|
599 | telnet_user_send_data_locked(user, &data, 1);
|
---|
600 | }
|
---|
601 | user->cursor_x = new_x;
|
---|
602 | fibril_mutex_unlock(&user->send_lock);
|
---|
603 |
|
---|
604 | }
|
---|
605 |
|
---|
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 |
|
---|
622 | /**
|
---|
623 | * @}
|
---|
624 | */
|
---|