| 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>
|
|---|
| 34 | #include <adt/hash_table.h>
|
|---|
| 35 | #include <assert.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <malloc.h>
|
|---|
| 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;
|
|---|
| 47 | sysarg_t service; /**< Service ID. */
|
|---|
| 48 | sysarg_t phone; /**< Phone registered with the service. */
|
|---|
| 49 | sysarg_t in_phone_hash; /**< Incoming phone hash. */
|
|---|
| 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 | */
|
|---|
| 60 | static hash_index_t service_hash(unsigned long key[])
|
|---|
| 61 | {
|
|---|
| 62 | assert(key);
|
|---|
| 63 | return (key[0] % SERVICE_HASH_TABLE_CHAINS);
|
|---|
| 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)
|
|---|
| 90 | return ((key[0] == hs->service) && (key[1] == hs->in_phone_hash));
|
|---|
| 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;
|
|---|
| 119 | sysarg_t service; /**< Number of the service. */
|
|---|
| 120 | ipc_callid_t callid; /**< Call ID waiting for the connection */
|
|---|
| 121 | sysarg_t arg2; /**< Second argument */
|
|---|
| 122 | sysarg_t arg3; /**< Third argument */
|
|---|
| 123 | } pending_conn_t;
|
|---|
| 124 |
|
|---|
| 125 | static list_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 | loop:
|
|---|
| 144 | list_foreach(pending_conn, cur) {
|
|---|
| 145 | pending_conn_t *pr = list_get_instance(cur, pending_conn_t, link);
|
|---|
| 146 |
|
|---|
| 147 | unsigned long keys[3] = {
|
|---|
| 148 | pr->service,
|
|---|
| 149 | 0,
|
|---|
| 150 | 0
|
|---|
| 151 | };
|
|---|
| 152 |
|
|---|
| 153 | link_t *link = hash_table_find(&service_hash_table, keys);
|
|---|
| 154 | if (!link)
|
|---|
| 155 | continue;
|
|---|
| 156 |
|
|---|
| 157 | hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link);
|
|---|
| 158 | (void) ipc_forward_fast(pr->callid, hs->phone, pr->arg2,
|
|---|
| 159 | pr->arg3, 0, IPC_FF_NONE);
|
|---|
| 160 |
|
|---|
| 161 | list_remove(cur);
|
|---|
| 162 | free(pr);
|
|---|
| 163 | goto loop;
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | /** Register service.
|
|---|
| 168 | *
|
|---|
| 169 | * @param service Service to be registered.
|
|---|
| 170 | * @param phone Phone to be used for connections to the service.
|
|---|
| 171 | * @param call Pointer to call structure.
|
|---|
| 172 | *
|
|---|
| 173 | * @return Zero on success or a value from @ref errno.h.
|
|---|
| 174 | *
|
|---|
| 175 | */
|
|---|
| 176 | int register_service(sysarg_t service, sysarg_t phone, ipc_call_t *call)
|
|---|
| 177 | {
|
|---|
| 178 | unsigned long keys[3] = {
|
|---|
| 179 | service,
|
|---|
| 180 | call->in_phone_hash,
|
|---|
| 181 | 0
|
|---|
| 182 | };
|
|---|
| 183 |
|
|---|
| 184 | if (hash_table_find(&service_hash_table, keys))
|
|---|
| 185 | return EEXISTS;
|
|---|
| 186 |
|
|---|
| 187 | hashed_service_t *hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
|
|---|
| 188 | if (!hs)
|
|---|
| 189 | return ENOMEM;
|
|---|
| 190 |
|
|---|
| 191 | link_initialize(&hs->link);
|
|---|
| 192 | hs->service = service;
|
|---|
| 193 | hs->phone = phone;
|
|---|
| 194 | hs->in_phone_hash = call->in_phone_hash;
|
|---|
| 195 | hash_table_insert(&service_hash_table, keys, &hs->link);
|
|---|
| 196 |
|
|---|
| 197 | return EOK;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | /** Connect client to service.
|
|---|
| 201 | *
|
|---|
| 202 | * @param service Service to be connected to.
|
|---|
| 203 | * @param call Pointer to call structure.
|
|---|
| 204 | * @param callid Call ID of the request.
|
|---|
| 205 | *
|
|---|
| 206 | * @return Zero on success or a value from @ref errno.h.
|
|---|
| 207 | *
|
|---|
| 208 | */
|
|---|
| 209 | void connect_to_service(sysarg_t service, ipc_call_t *call, ipc_callid_t callid)
|
|---|
| 210 | {
|
|---|
| 211 | sysarg_t retval;
|
|---|
| 212 | unsigned long keys[3] = {
|
|---|
| 213 | service,
|
|---|
| 214 | 0,
|
|---|
| 215 | 0
|
|---|
| 216 | };
|
|---|
| 217 |
|
|---|
| 218 | link_t *link = hash_table_find(&service_hash_table, keys);
|
|---|
| 219 | if (!link) {
|
|---|
| 220 | if (IPC_GET_ARG4(*call) & IPC_FLAG_BLOCKING) {
|
|---|
| 221 | /* Blocking connection, add to pending list */
|
|---|
| 222 | pending_conn_t *pr =
|
|---|
| 223 | (pending_conn_t *) malloc(sizeof(pending_conn_t));
|
|---|
| 224 | if (!pr) {
|
|---|
| 225 | retval = ENOMEM;
|
|---|
| 226 | goto out;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | link_initialize(&pr->link);
|
|---|
| 230 | pr->service = service;
|
|---|
| 231 | pr->callid = callid;
|
|---|
| 232 | pr->arg2 = IPC_GET_ARG2(*call);
|
|---|
| 233 | pr->arg3 = IPC_GET_ARG3(*call);
|
|---|
| 234 | list_append(&pr->link, &pending_conn);
|
|---|
| 235 | return;
|
|---|
| 236 | }
|
|---|
| 237 | retval = ENOENT;
|
|---|
| 238 | goto out;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link);
|
|---|
| 242 | (void) ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call),
|
|---|
| 243 | IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
|
|---|
| 244 | return;
|
|---|
| 245 |
|
|---|
| 246 | out:
|
|---|
| 247 | if (!(callid & IPC_CALLID_NOTIFICATION))
|
|---|
| 248 | ipc_answer_0(callid, retval);
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | /**
|
|---|
| 252 | * @}
|
|---|
| 253 | */
|
|---|