[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>
|
---|
| 37 | #include "service.h"
|
---|
| 38 | #include "ns.h"
|
---|
| 39 |
|
---|
| 40 | #define SERVICE_HASH_TABLE_CHAINS 20
|
---|
| 41 |
|
---|
| 42 | /** Service hash table item. */
|
---|
| 43 | typedef struct {
|
---|
| 44 | link_t link;
|
---|
[96b02eb9] | 45 | sysarg_t service; /**< Number of the service. */
|
---|
| 46 | sysarg_t phone; /**< Phone registered with the service. */
|
---|
| 47 | sysarg_t in_phone_hash; /**< Incoming phone hash. */
|
---|
[40313e4] | 48 | } hashed_service_t;
|
---|
| 49 |
|
---|
| 50 | /** Compute hash index into service hash table.
|
---|
| 51 | *
|
---|
| 52 | * @param key Pointer keys. However, only the first key (i.e. service number)
|
---|
| 53 | * is used to compute the hash index.
|
---|
| 54 | *
|
---|
| 55 | * @return Hash index corresponding to key[0].
|
---|
| 56 | *
|
---|
| 57 | */
|
---|
| 58 | static hash_index_t service_hash(unsigned long *key)
|
---|
| 59 | {
|
---|
| 60 | assert(key);
|
---|
| 61 | return (*key % SERVICE_HASH_TABLE_CHAINS);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | /** Compare a key with hashed item.
|
---|
| 65 | *
|
---|
| 66 | * This compare function always ignores the third key.
|
---|
| 67 | * It exists only to make it possible to remove records
|
---|
| 68 | * originating from connection with key[1] in_phone_hash
|
---|
| 69 | * value. Note that this is close to being classified
|
---|
| 70 | * as a nasty hack.
|
---|
| 71 | *
|
---|
| 72 | * @param key Array of keys.
|
---|
| 73 | * @param keys Must be lesser or equal to 3.
|
---|
| 74 | * @param item Pointer to a hash table item.
|
---|
| 75 | *
|
---|
| 76 | * @return Non-zero if the key matches the item, zero otherwise.
|
---|
| 77 | *
|
---|
| 78 | */
|
---|
| 79 | static int service_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
| 80 | {
|
---|
| 81 | assert(key);
|
---|
| 82 | assert(keys <= 3);
|
---|
| 83 | assert(item);
|
---|
| 84 |
|
---|
| 85 | hashed_service_t *hs = hash_table_get_instance(item, hashed_service_t, link);
|
---|
| 86 |
|
---|
| 87 | if (keys == 2)
|
---|
| 88 | return (key[1] == hs->in_phone_hash);
|
---|
| 89 | else
|
---|
| 90 | return (key[0] == hs->service);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /** Perform actions after removal of item from the hash table.
|
---|
| 94 | *
|
---|
| 95 | * @param item Item that was removed from the hash table.
|
---|
| 96 | *
|
---|
| 97 | */
|
---|
| 98 | static void service_remove(link_t *item)
|
---|
| 99 | {
|
---|
| 100 | assert(item);
|
---|
| 101 | free(hash_table_get_instance(item, hashed_service_t, link));
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /** Operations for service hash table. */
|
---|
| 105 | static hash_table_operations_t service_hash_table_ops = {
|
---|
| 106 | .hash = service_hash,
|
---|
| 107 | .compare = service_compare,
|
---|
| 108 | .remove_callback = service_remove
|
---|
| 109 | };
|
---|
| 110 |
|
---|
| 111 | /** Service hash table structure. */
|
---|
| 112 | static hash_table_t service_hash_table;
|
---|
| 113 |
|
---|
| 114 | /** Pending connection structure. */
|
---|
| 115 | typedef struct {
|
---|
| 116 | link_t link;
|
---|
[96b02eb9] | 117 | sysarg_t service; /**< Number of the service. */
|
---|
[40313e4] | 118 | ipc_callid_t callid; /**< Call ID waiting for the connection */
|
---|
[96b02eb9] | 119 | sysarg_t arg2; /**< Second argument */
|
---|
| 120 | sysarg_t arg3; /**< Third argument */
|
---|
[40313e4] | 121 | } pending_conn_t;
|
---|
| 122 |
|
---|
| 123 | static link_t pending_conn;
|
---|
| 124 |
|
---|
| 125 | int service_init(void)
|
---|
| 126 | {
|
---|
| 127 | if (!hash_table_create(&service_hash_table, SERVICE_HASH_TABLE_CHAINS,
|
---|
| 128 | 3, &service_hash_table_ops)) {
|
---|
| 129 | printf(NAME ": No memory available for services\n");
|
---|
| 130 | return ENOMEM;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | list_initialize(&pending_conn);
|
---|
| 134 |
|
---|
| 135 | return EOK;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /** Process pending connection requests */
|
---|
| 139 | void process_pending_conn(void)
|
---|
| 140 | {
|
---|
| 141 | link_t *cur;
|
---|
| 142 |
|
---|
| 143 | loop:
|
---|
| 144 | for (cur = pending_conn.next; cur != &pending_conn; cur = cur->next) {
|
---|
| 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);
|
---|
[c638c07] | 158 | (void) ipc_forward_fast(pr->callid, hs->phone, pr->arg2,
|
---|
| 159 | pr->arg3, 0, IPC_FF_NONE);
|
---|
[40313e4] | 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 | */
|
---|
[96b02eb9] | 176 | int register_service(sysarg_t service, sysarg_t phone, ipc_call_t *call)
|
---|
[40313e4] | 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 0;
|
---|
| 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 | */
|
---|
[96b02eb9] | 209 | void connect_to_service(sysarg_t service, ipc_call_t *call, ipc_callid_t callid)
|
---|
[40313e4] | 210 | {
|
---|
[96b02eb9] | 211 | sysarg_t retval;
|
---|
[40313e4] | 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 | pr->service = service;
|
---|
| 230 | pr->callid = callid;
|
---|
| 231 | pr->arg2 = IPC_GET_ARG2(*call);
|
---|
| 232 | pr->arg3 = IPC_GET_ARG3(*call);
|
---|
| 233 | list_append(&pr->link, &pending_conn);
|
---|
| 234 | return;
|
---|
| 235 | }
|
---|
| 236 | retval = ENOENT;
|
---|
| 237 | goto out;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link);
|
---|
[c638c07] | 241 | (void) ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call),
|
---|
[40313e4] | 242 | IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
|
---|
[c638c07] | 243 | return;
|
---|
[40313e4] | 244 |
|
---|
| 245 | out:
|
---|
| 246 | if (!(callid & IPC_CALLID_NOTIFICATION))
|
---|
| 247 | ipc_answer_0(callid, retval);
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | /**
|
---|
| 251 | * @}
|
---|
| 252 | */
|
---|