source: mainline/uspace/srv/hid/remcons/user.c@ e1c6d5df

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e1c6d5df was 5d94b16c, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Factor out server side of console IPC protocol.

  • Property mode set to 100644
File size: 9.9 KB
Line 
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#include <async.h>
35#include <stdio.h>
36#include <adt/prodcons.h>
37#include <errno.h>
38#include <str_error.h>
39#include <loc.h>
40#include <event.h>
41#include <io/keycode.h>
42#include <align.h>
43#include <malloc.h>
44#include <as.h>
45#include <fibril_synch.h>
46#include <task.h>
47#include <net/in.h>
48#include <net/inet.h>
49#include <net/socket.h>
50#include <io/console.h>
51#include <inttypes.h>
52#include <assert.h>
53#include "user.h"
54#include "telnet.h"
55
56static FIBRIL_MUTEX_INITIALIZE(users_guard);
57static LIST_INITIALIZE(users);
58
59/** Create new telnet user.
60 *
61 * @param socket Socket the user communicates through.
62 * @return New telnet user or NULL when out of memory.
63 */
64telnet_user_t *telnet_user_create(int socket)
65{
66 static int telnet_user_id_counter = 0;
67
68 telnet_user_t *user = malloc(sizeof(telnet_user_t));
69 if (user == NULL) {
70 return NULL;
71 }
72
73 user->id = ++telnet_user_id_counter;
74
75 int rc = asprintf(&user->service_name, "%s/telnet%d", NAMESPACE, user->id);
76 if (rc < 0) {
77 free(user);
78 return NULL;
79 }
80
81 user->socket = socket;
82 user->service_id = (service_id_t) -1;
83 prodcons_initialize(&user->in_events);
84 link_initialize(&user->link);
85 user->socket_buffer_len = 0;
86 user->socket_buffer_pos = 0;
87
88 fibril_condvar_initialize(&user->refcount_cv);
89 fibril_mutex_initialize(&user->guard);
90 user->task_finished = false;
91 user->socket_closed = false;
92 user->locsrv_connection_count = 0;
93
94 user->cursor_x = 0;
95
96 return user;
97}
98
99void telnet_user_add(telnet_user_t *user)
100{
101 fibril_mutex_lock(&users_guard);
102 list_append(&user->link, &users);
103 fibril_mutex_unlock(&users_guard);
104}
105
106/** Destroy telnet user structure.
107 *
108 * @param user User to be destroyed.
109 */
110void telnet_user_destroy(telnet_user_t *user)
111{
112 assert(user);
113
114 fibril_mutex_lock(&users_guard);
115 list_remove(&user->link);
116 fibril_mutex_unlock(&users_guard);
117
118 free(user);
119}
120
121/** Find user by service id and increments reference counter.
122 *
123 * @param id Location service id of the telnet user's terminal.
124 */
125telnet_user_t *telnet_user_get_for_client_connection(service_id_t id)
126{
127 telnet_user_t *user = NULL;
128
129 fibril_mutex_lock(&users_guard);
130 list_foreach(users, link) {
131 telnet_user_t *tmp = list_get_instance(link, telnet_user_t, link);
132 if (tmp->service_id == id) {
133 user = tmp;
134 break;
135 }
136 }
137 if (user == NULL) {
138 fibril_mutex_unlock(&users_guard);
139 return NULL;
140 }
141
142 telnet_user_t *tmp = user;
143 fibril_mutex_lock(&tmp->guard);
144 user->locsrv_connection_count++;
145
146 /*
147 * Refuse to return user whose task already finished or when
148 * the socket is already closed().
149 */
150 if (user->task_finished || user->socket_closed) {
151 user = NULL;
152 user->locsrv_connection_count--;
153 }
154
155 fibril_mutex_unlock(&tmp->guard);
156
157
158 fibril_mutex_unlock(&users_guard);
159
160 return user;
161}
162
163/** Notify that client disconnected from the remote terminal.
164 *
165 * @param user To which user the client was connected.
166 */
167void telnet_user_notify_client_disconnected(telnet_user_t *user)
168{
169 fibril_mutex_lock(&user->guard);
170 assert(user->locsrv_connection_count > 0);
171 user->locsrv_connection_count--;
172 fibril_condvar_signal(&user->refcount_cv);
173 fibril_mutex_unlock(&user->guard);
174}
175
176/** Tell whether the launched task already exited and socket is already closed.
177 *
178 * @param user Telnet user in question.
179 */
180bool telnet_user_is_zombie(telnet_user_t *user)
181{
182 fibril_mutex_lock(&user->guard);
183 bool zombie = user->socket_closed || user->task_finished;
184 fibril_mutex_unlock(&user->guard);
185
186 return zombie;
187}
188
189/** Receive next byte from a socket (use buffering.
190 * We need to return the value via extra argument because the read byte
191 * might be negative.
192 */
193static int telnet_user_recv_next_byte_no_lock(telnet_user_t *user, char *byte)
194{
195 /* No more buffered data? */
196 if (user->socket_buffer_len <= user->socket_buffer_pos) {
197 int recv_length = recv(user->socket, user->socket_buffer, BUFFER_SIZE, 0);
198 if ((recv_length == 0) || (recv_length == ENOTCONN)) {
199 user->socket_closed = true;
200 user->srvs.aborted = true;
201 return ENOENT;
202 }
203 if (recv_length < 0) {
204 return recv_length;
205 }
206 user->socket_buffer_len = recv_length;
207 user->socket_buffer_pos = 0;
208 }
209
210 *byte = user->socket_buffer[user->socket_buffer_pos++];
211
212 return EOK;
213}
214
215/** Creates new keyboard event from given char.
216 *
217 * @param type Event type (press / release).
218 * @param c Pressed character.
219 */
220static kbd_event_t* new_kbd_event(kbd_event_type_t type, wchar_t c) {
221 kbd_event_t *event = malloc(sizeof(kbd_event_t));
222 assert(event);
223
224 link_initialize(&event->link);
225 event->type = type;
226 event->c = c;
227 event->mods = 0;
228
229 switch (c) {
230 case '\n':
231 event->key = KC_ENTER;
232 break;
233 case '\t':
234 event->key = KC_TAB;
235 break;
236 case '\b':
237 case 127: /* This is what Linux telnet sends. */
238 event->key = KC_BACKSPACE;
239 event->c = '\b';
240 break;
241 default:
242 event->key = KC_A;
243 break;
244 }
245
246 return event;
247}
248
249/** Process telnet command (currently only print to screen).
250 *
251 * @param user Telnet user structure.
252 * @param option_code Command option code.
253 * @param cmd Telnet command.
254 */
255static void process_telnet_command(telnet_user_t *user,
256 telnet_cmd_t option_code, telnet_cmd_t cmd)
257{
258 if (option_code != 0) {
259 telnet_user_log(user, "Ignoring telnet command %u %u %u.",
260 TELNET_IAC, option_code, cmd);
261 } else {
262 telnet_user_log(user, "Ignoring telnet command %u %u.",
263 TELNET_IAC, cmd);
264 }
265}
266
267/** Get next keyboard event.
268 *
269 * @param user Telnet user.
270 * @param event Where to store the keyboard event.
271 * @return Error code.
272 */
273int telnet_user_get_next_keyboard_event(telnet_user_t *user, kbd_event_t *event)
274{
275 fibril_mutex_lock(&user->guard);
276 if (list_empty(&user->in_events.list)) {
277 char next_byte = 0;
278 bool inside_telnet_command = false;
279
280 telnet_cmd_t telnet_option_code = 0;
281
282 /* Skip zeros, bail-out on error. */
283 while (next_byte == 0) {
284 int rc = telnet_user_recv_next_byte_no_lock(user, &next_byte);
285 if (rc != EOK) {
286 fibril_mutex_unlock(&user->guard);
287 return rc;
288 }
289 uint8_t byte = (uint8_t) next_byte;
290
291 /* Skip telnet commands. */
292 if (inside_telnet_command) {
293 inside_telnet_command = false;
294 next_byte = 0;
295 if (TELNET_IS_OPTION_CODE(byte)) {
296 telnet_option_code = byte;
297 inside_telnet_command = true;
298 } else {
299 process_telnet_command(user,
300 telnet_option_code, byte);
301 }
302 }
303 if (byte == TELNET_IAC) {
304 inside_telnet_command = true;
305 next_byte = 0;
306 }
307 }
308
309 /* CR-LF conversions. */
310 if (next_byte == 13) {
311 next_byte = 10;
312 }
313
314 kbd_event_t *down = new_kbd_event(KEY_PRESS, next_byte);
315 kbd_event_t *up = new_kbd_event(KEY_RELEASE, next_byte);
316 assert(down);
317 assert(up);
318 prodcons_produce(&user->in_events, &down->link);
319 prodcons_produce(&user->in_events, &up->link);
320 }
321
322 link_t *link = prodcons_consume(&user->in_events);
323 kbd_event_t *tmp = list_get_instance(link, kbd_event_t, link);
324
325 fibril_mutex_unlock(&user->guard);
326
327 *event = *tmp;
328
329 free(tmp);
330
331 return EOK;
332}
333
334/** Send data (convert them first) to the socket, no locking.
335 *
336 * @param user Telnet user.
337 * @param data Data buffer (not zero terminated).
338 * @param size Size of @p data buffer in bytes.
339 */
340static int telnet_user_send_data_no_lock(telnet_user_t *user, uint8_t *data, size_t size)
341{
342 uint8_t *converted = malloc(3 * size + 1);
343 assert(converted);
344 int converted_size = 0;
345
346 /* Convert new-lines. */
347 for (size_t i = 0; i < size; i++) {
348 if (data[i] == 10) {
349 converted[converted_size++] = 13;
350 converted[converted_size++] = 10;
351 user->cursor_x = 0;
352 } else {
353 converted[converted_size++] = data[i];
354 if (data[i] == '\b') {
355 user->cursor_x--;
356 } else {
357 user->cursor_x++;
358 }
359 }
360 }
361
362
363 int rc = send(user->socket, converted, converted_size, 0);
364 free(converted);
365
366 return rc;
367}
368
369/** Send data (convert them first) to the socket.
370 *
371 * @param user Telnet user.
372 * @param data Data buffer (not zero terminated).
373 * @param size Size of @p data buffer in bytes.
374 */
375int telnet_user_send_data(telnet_user_t *user, uint8_t *data, size_t size)
376{
377 fibril_mutex_lock(&user->guard);
378
379 int rc = telnet_user_send_data_no_lock(user, data, size);
380
381 fibril_mutex_unlock(&user->guard);
382
383 return rc;
384}
385
386/** Update cursor X position.
387 *
388 * This call may result in sending control commands over socket.
389 *
390 * @param user Telnet user.
391 * @param new_x New cursor location.
392 */
393void telnet_user_update_cursor_x(telnet_user_t *user, int new_x)
394{
395 fibril_mutex_lock(&user->guard);
396 if (user->cursor_x - 1 == new_x) {
397 uint8_t data = '\b';
398 /* Ignore errors. */
399 telnet_user_send_data_no_lock(user, &data, 1);
400 }
401 user->cursor_x = new_x;
402 fibril_mutex_unlock(&user->guard);
403
404}
405
406/**
407 * @}
408 */
Note: See TracBrowser for help on using the repository browser.