| 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 <ipc/input.h>
|
|---|
| 38 | #include <ipc/console.h>
|
|---|
| 39 | #include <ipc/vfs.h>
|
|---|
| 40 | #include <errno.h>
|
|---|
| 41 | #include <str_error.h>
|
|---|
| 42 | #include <loc.h>
|
|---|
| 43 | #include <event.h>
|
|---|
| 44 | #include <io/keycode.h>
|
|---|
| 45 | #include <align.h>
|
|---|
| 46 | #include <malloc.h>
|
|---|
| 47 | #include <as.h>
|
|---|
| 48 | #include <fibril_synch.h>
|
|---|
| 49 | #include <task.h>
|
|---|
| 50 | #include <net/in.h>
|
|---|
| 51 | #include <net/inet.h>
|
|---|
| 52 | #include <net/socket.h>
|
|---|
| 53 | #include <io/console.h>
|
|---|
| 54 | #include <inttypes.h>
|
|---|
| 55 | #include "user.h"
|
|---|
| 56 |
|
|---|
| 57 | static FIBRIL_MUTEX_INITIALIZE(users_guard);
|
|---|
| 58 | static LIST_INITIALIZE(users);
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | telnet_user_t *telnet_user_create(int socket)
|
|---|
| 62 | {
|
|---|
| 63 | static int telnet_user_id_counter = 0;
|
|---|
| 64 |
|
|---|
| 65 | telnet_user_t *user = malloc(sizeof(telnet_user_t));
|
|---|
| 66 | if (user == NULL) {
|
|---|
| 67 | return NULL;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | user->id = ++telnet_user_id_counter;
|
|---|
| 71 |
|
|---|
| 72 | int rc = asprintf(&user->service_name, "%s/telnet%d", NAMESPACE, user->id);
|
|---|
| 73 | if (rc < 0) {
|
|---|
| 74 | free(user);
|
|---|
| 75 | return NULL;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | user->socket = socket;
|
|---|
| 79 | user->service_id = (service_id_t) -1;
|
|---|
| 80 | prodcons_initialize(&user->in_events);
|
|---|
| 81 | link_initialize(&user->link);
|
|---|
| 82 | user->socket_buffer_len = 0;
|
|---|
| 83 | user->socket_buffer_pos = 0;
|
|---|
| 84 |
|
|---|
| 85 | fibril_condvar_initialize(&user->refcount_cv);
|
|---|
| 86 | fibril_mutex_initialize(&user->refcount_mutex);
|
|---|
| 87 | user->task_finished = false;
|
|---|
| 88 | user->socket_closed = false;
|
|---|
| 89 | user->locsrv_connection_count = 0;
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 | fibril_mutex_lock(&users_guard);
|
|---|
| 93 | list_append(&user->link, &users);
|
|---|
| 94 | fibril_mutex_unlock(&users_guard);
|
|---|
| 95 |
|
|---|
| 96 | return user;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | void telnet_user_destroy(telnet_user_t *user)
|
|---|
| 100 | {
|
|---|
| 101 | assert(user);
|
|---|
| 102 |
|
|---|
| 103 | fibril_mutex_lock(&users_guard);
|
|---|
| 104 | list_remove(&user->link);
|
|---|
| 105 | fibril_mutex_unlock(&users_guard);
|
|---|
| 106 |
|
|---|
| 107 | free(user);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | telnet_user_t *telnet_user_get_for_client_connection(service_id_t id)
|
|---|
| 111 | {
|
|---|
| 112 | telnet_user_t *user = NULL;
|
|---|
| 113 |
|
|---|
| 114 | fibril_mutex_lock(&users_guard);
|
|---|
| 115 | list_foreach(users, link) {
|
|---|
| 116 | telnet_user_t *tmp = list_get_instance(link, telnet_user_t, link);
|
|---|
| 117 | if (tmp->service_id == id) {
|
|---|
| 118 | user = tmp;
|
|---|
| 119 | break;
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 | if (user == NULL) {
|
|---|
| 123 | fibril_mutex_unlock(&users_guard);
|
|---|
| 124 | return NULL;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | telnet_user_t *tmp = user;
|
|---|
| 128 | fibril_mutex_lock(&tmp->refcount_mutex);
|
|---|
| 129 | user->locsrv_connection_count++;
|
|---|
| 130 |
|
|---|
| 131 | /*
|
|---|
| 132 | * Refuse to return user whose task already finished or when
|
|---|
| 133 | * the socket is already closed().
|
|---|
| 134 | */
|
|---|
| 135 | if (user->task_finished || user->socket_closed) {
|
|---|
| 136 | user = NULL;
|
|---|
| 137 | user->locsrv_connection_count--;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | fibril_mutex_unlock(&tmp->refcount_mutex);
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 | fibril_mutex_unlock(&users_guard);
|
|---|
| 144 |
|
|---|
| 145 | return user;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 | /**
|
|---|
| 151 | * @}
|
|---|
| 152 | */
|
|---|