[40313e4] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Martin Decky
|
---|
| 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 ns
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | #include <ipc/ipc.h>
|
---|
[d9c8c81] | 34 | #include <adt/hash_table.h>
|
---|
[40313e4] | 35 | #include <assert.h>
|
---|
| 36 | #include <errno.h>
|
---|
[c7bbf029] | 37 | #include <stdio.h>
|
---|
| 38 | #include <malloc.h>
|
---|
[40313e4] | 39 | #include "service.h"
|
---|
| 40 | #include "ns.h"
|
---|
| 41 |
|
---|
| 42 | #define SERVICE_HASH_TABLE_CHAINS 20
|
---|
| 43 |
|
---|
| 44 | /** Service hash table item. */
|
---|
| 45 | typedef struct {
|
---|
| 46 | link_t link;
|
---|
[007e6efa] | 47 | sysarg_t service; /**< Service ID. */
|
---|
[96b02eb9] | 48 | sysarg_t phone; /**< Phone registered with the service. */
|
---|
| 49 | sysarg_t in_phone_hash; /**< Incoming phone hash. */
|
---|
[40313e4] | 50 | } hashed_service_t;
|
---|
| 51 |
|
---|
| 52 | /** Compute hash index into service hash table.
|
---|
| 53 | *
|
---|
| 54 | * @param key Pointer keys. However, only the first key (i.e. service number)
|
---|
| 55 | * is used to compute the hash index.
|
---|
| 56 | *
|
---|
| 57 | * @return Hash index corresponding to key[0].
|
---|
| 58 | *
|
---|
| 59 | */
|
---|
[007e6efa] | 60 | static hash_index_t service_hash(unsigned long key[])
|
---|
[40313e4] | 61 | {
|
---|
| 62 | assert(key);
|
---|
[007e6efa] | 63 | return (key[0] % SERVICE_HASH_TABLE_CHAINS);
|
---|
[40313e4] | 64 | }
|
---|
| 65 |
|
---|
| 66 | /** Compare a key with hashed item.
|
---|
| 67 | *
|
---|
| 68 | * This compare function always ignores the third key.
|
---|
| 69 | * It exists only to make it possible to remove records
|
---|
| 70 | * originating from connection with key[1] in_phone_hash
|
---|
| 71 | * value. Note that this is close to being classified
|
---|
| 72 | * as a nasty hack.
|
---|
| 73 | *
|
---|
| 74 | * @param key Array of keys.
|
---|
| 75 | * @param keys Must be lesser or equal to 3.
|
---|
| 76 | * @param item Pointer to a hash table item.
|
---|
| 77 | *
|
---|
| 78 | * @return Non-zero if the key matches the item, zero otherwise.
|
---|
| 79 | *
|
---|
| 80 | */
|
---|
| 81 | static int service_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
| 82 | {
|
---|
| 83 | assert(key);
|
---|
| 84 | assert(keys <= 3);
|
---|
| 85 | assert(item);
|
---|
| 86 |
|
---|
| 87 | hashed_service_t *hs = hash_table_get_instance(item, hashed_service_t, link);
|
---|
| 88 |
|
---|
| 89 | if (keys == 2)
|
---|
[007e6efa] | 90 | return ((key[0] == hs->service) && (key[1] == hs->in_phone_hash));
|
---|
[40313e4] | 91 | else
|
---|
| 92 | return (key[0] == hs->service);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /** Perform actions after removal of item from the hash table.
|
---|
| 96 | *
|
---|
| 97 | * @param item Item that was removed from the hash table.
|
---|
| 98 | *
|
---|
| 99 | */
|
---|
| 100 | static void service_remove(link_t *item)
|
---|
| 101 | {
|
---|
| 102 | assert(item);
|
---|
| 103 | free(hash_table_get_instance(item, hashed_service_t, link));
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /** Operations for service hash table. */
|
---|
| 107 | static hash_table_operations_t service_hash_table_ops = {
|
---|
| 108 | .hash = service_hash,
|
---|
| 109 | .compare = service_compare,
|
---|
| 110 | .remove_callback = service_remove
|
---|
| 111 | };
|
---|
| 112 |
|
---|
| 113 | /** Service hash table structure. */
|
---|
| 114 | static hash_table_t service_hash_table;
|
---|
| 115 |
|
---|
| 116 | /** Pending connection structure. */
|
---|
| 117 | typedef struct {
|
---|
| 118 | link_t link;
|
---|
[96b02eb9] | 119 | sysarg_t service; /**< Number of the service. */
|
---|
[40313e4] | 120 | ipc_callid_t callid; /**< Call ID waiting for the connection */
|
---|
[96b02eb9] | 121 | sysarg_t arg2; /**< Second argument */
|
---|
| 122 | sysarg_t arg3; /**< Third argument */
|
---|
[40313e4] | 123 | } pending_conn_t;
|
---|
| 124 |
|
---|
| 125 | static link_t pending_conn;
|
---|
| 126 |
|
---|
| 127 | int service_init(void)
|
---|
| 128 | {
|
---|
| 129 | if (!hash_table_create(&service_hash_table, SERVICE_HASH_TABLE_CHAINS,
|
---|
| 130 | 3, &service_hash_table_ops)) {
|
---|
| 131 | printf(NAME ": No memory available for services\n");
|
---|
| 132 | return ENOMEM;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | list_initialize(&pending_conn);
|
---|
| 136 |
|
---|
| 137 | return EOK;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /** Process pending connection requests */
|
---|
| 141 | void process_pending_conn(void)
|
---|
| 142 | {
|
---|
| 143 | link_t *cur;
|
---|
| 144 |
|
---|
| 145 | loop:
|
---|
| 146 | for (cur = pending_conn.next; cur != &pending_conn; cur = cur->next) {
|
---|
| 147 | pending_conn_t *pr = list_get_instance(cur, pending_conn_t, link);
|
---|
| 148 |
|
---|
| 149 | unsigned long keys[3] = {
|
---|
| 150 | pr->service,
|
---|
| 151 | 0,
|
---|
| 152 | 0
|
---|
| 153 | };
|
---|
| 154 |
|
---|
| 155 | link_t *link = hash_table_find(&service_hash_table, keys);
|
---|
| 156 | if (!link)
|
---|
| 157 | continue;
|
---|
| 158 |
|
---|
| 159 | hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link);
|
---|
[c638c07] | 160 | (void) ipc_forward_fast(pr->callid, hs->phone, pr->arg2,
|
---|
| 161 | pr->arg3, 0, IPC_FF_NONE);
|
---|
[40313e4] | 162 |
|
---|
| 163 | list_remove(cur);
|
---|
| 164 | free(pr);
|
---|
| 165 | goto loop;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /** Register service.
|
---|
| 170 | *
|
---|
| 171 | * @param service Service to be registered.
|
---|
| 172 | * @param phone Phone to be used for connections to the service.
|
---|
| 173 | * @param call Pointer to call structure.
|
---|
| 174 | *
|
---|
| 175 | * @return Zero on success or a value from @ref errno.h.
|
---|
| 176 | *
|
---|
| 177 | */
|
---|
[96b02eb9] | 178 | int register_service(sysarg_t service, sysarg_t phone, ipc_call_t *call)
|
---|
[40313e4] | 179 | {
|
---|
| 180 | unsigned long keys[3] = {
|
---|
| 181 | service,
|
---|
| 182 | call->in_phone_hash,
|
---|
| 183 | 0
|
---|
| 184 | };
|
---|
| 185 |
|
---|
| 186 | if (hash_table_find(&service_hash_table, keys))
|
---|
| 187 | return EEXISTS;
|
---|
| 188 |
|
---|
| 189 | hashed_service_t *hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
|
---|
| 190 | if (!hs)
|
---|
| 191 | return ENOMEM;
|
---|
| 192 |
|
---|
| 193 | link_initialize(&hs->link);
|
---|
| 194 | hs->service = service;
|
---|
| 195 | hs->phone = phone;
|
---|
| 196 | hs->in_phone_hash = call->in_phone_hash;
|
---|
| 197 | hash_table_insert(&service_hash_table, keys, &hs->link);
|
---|
| 198 |
|
---|
[007e6efa] | 199 | return EOK;
|
---|
[40313e4] | 200 | }
|
---|
| 201 |
|
---|
| 202 | /** Connect client to service.
|
---|
| 203 | *
|
---|
| 204 | * @param service Service to be connected to.
|
---|
| 205 | * @param call Pointer to call structure.
|
---|
| 206 | * @param callid Call ID of the request.
|
---|
| 207 | *
|
---|
| 208 | * @return Zero on success or a value from @ref errno.h.
|
---|
| 209 | *
|
---|
| 210 | */
|
---|
[96b02eb9] | 211 | void connect_to_service(sysarg_t service, ipc_call_t *call, ipc_callid_t callid)
|
---|
[40313e4] | 212 | {
|
---|
[96b02eb9] | 213 | sysarg_t retval;
|
---|
[40313e4] | 214 | unsigned long keys[3] = {
|
---|
| 215 | service,
|
---|
| 216 | 0,
|
---|
| 217 | 0
|
---|
| 218 | };
|
---|
| 219 |
|
---|
| 220 | link_t *link = hash_table_find(&service_hash_table, keys);
|
---|
| 221 | if (!link) {
|
---|
| 222 | if (IPC_GET_ARG4(*call) & IPC_FLAG_BLOCKING) {
|
---|
| 223 | /* Blocking connection, add to pending list */
|
---|
| 224 | pending_conn_t *pr =
|
---|
| 225 | (pending_conn_t *) malloc(sizeof(pending_conn_t));
|
---|
| 226 | if (!pr) {
|
---|
| 227 | retval = ENOMEM;
|
---|
| 228 | goto out;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[007e6efa] | 231 | link_initialize(&pr->link);
|
---|
[40313e4] | 232 | pr->service = service;
|
---|
| 233 | pr->callid = callid;
|
---|
| 234 | pr->arg2 = IPC_GET_ARG2(*call);
|
---|
| 235 | pr->arg3 = IPC_GET_ARG3(*call);
|
---|
| 236 | list_append(&pr->link, &pending_conn);
|
---|
| 237 | return;
|
---|
| 238 | }
|
---|
| 239 | retval = ENOENT;
|
---|
| 240 | goto out;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link);
|
---|
[c638c07] | 244 | (void) ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call),
|
---|
[40313e4] | 245 | IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
|
---|
[c638c07] | 246 | return;
|
---|
[40313e4] | 247 |
|
---|
| 248 | out:
|
---|
| 249 | if (!(callid & IPC_CALLID_NOTIFICATION))
|
---|
| 250 | ipc_answer_0(callid, retval);
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | /**
|
---|
| 254 | * @}
|
---|
| 255 | */
|
---|