| 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 | /** Service hash table item. */
|
|---|
| 43 | typedef struct {
|
|---|
| 44 | ht_link_t link;
|
|---|
| 45 |
|
|---|
| 46 | /** Service ID */
|
|---|
| 47 | service_t service;
|
|---|
| 48 |
|
|---|
| 49 | /** Phone registered with the interface */
|
|---|
| 50 | sysarg_t phone;
|
|---|
| 51 |
|
|---|
| 52 | /** Incoming phone hash */
|
|---|
| 53 | sysarg_t in_phone_hash;
|
|---|
| 54 | } hashed_service_t;
|
|---|
| 55 |
|
|---|
| 56 | static size_t service_key_hash(void *key)
|
|---|
| 57 | {
|
|---|
| 58 | return *(service_t *) key;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | static size_t service_hash(const ht_link_t *item)
|
|---|
| 62 | {
|
|---|
| 63 | hashed_service_t *service =
|
|---|
| 64 | hash_table_get_inst(item, hashed_service_t, link);
|
|---|
| 65 |
|
|---|
| 66 | return service->service;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | static bool service_key_equal(void *key, const ht_link_t *item)
|
|---|
| 70 | {
|
|---|
| 71 | hashed_service_t *service =
|
|---|
| 72 | hash_table_get_inst(item, hashed_service_t, link);
|
|---|
| 73 |
|
|---|
| 74 | return service->service == *(service_t *) key;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /** Operations for service hash table. */
|
|---|
| 78 | static hash_table_ops_t service_hash_table_ops = {
|
|---|
| 79 | .hash = service_hash,
|
|---|
| 80 | .key_hash = service_key_hash,
|
|---|
| 81 | .key_equal = service_key_equal,
|
|---|
| 82 | .equal = NULL,
|
|---|
| 83 | .remove_callback = NULL
|
|---|
| 84 | };
|
|---|
| 85 |
|
|---|
| 86 | /** Service hash table structure. */
|
|---|
| 87 | static hash_table_t service_hash_table;
|
|---|
| 88 |
|
|---|
| 89 | /** Pending connection structure. */
|
|---|
| 90 | typedef struct {
|
|---|
| 91 | link_t link;
|
|---|
| 92 | service_t service; /**< Service ID */
|
|---|
| 93 | iface_t iface; /**< Interface ID */
|
|---|
| 94 | ipc_callid_t callid; /**< Call ID waiting for the connection */
|
|---|
| 95 | sysarg_t arg3; /**< Third argument */
|
|---|
| 96 | } pending_conn_t;
|
|---|
| 97 |
|
|---|
| 98 | static list_t pending_conn;
|
|---|
| 99 |
|
|---|
| 100 | int service_init(void)
|
|---|
| 101 | {
|
|---|
| 102 | if (!hash_table_create(&service_hash_table, 0, 0,
|
|---|
| 103 | &service_hash_table_ops)) {
|
|---|
| 104 | printf("%s: No memory available for services\n", NAME);
|
|---|
| 105 | return ENOMEM;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | list_initialize(&pending_conn);
|
|---|
| 109 |
|
|---|
| 110 | return EOK;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /** Process pending connection requests */
|
|---|
| 114 | void process_pending_conn(void)
|
|---|
| 115 | {
|
|---|
| 116 | loop:
|
|---|
| 117 | list_foreach(pending_conn, link, pending_conn_t, pending) {
|
|---|
| 118 | ht_link_t *link = hash_table_find(&service_hash_table, &pending->service);
|
|---|
| 119 | if (!link)
|
|---|
| 120 | continue;
|
|---|
| 121 |
|
|---|
| 122 | hashed_service_t *hashed_service = hash_table_get_inst(link, hashed_service_t, link);
|
|---|
| 123 | (void) ipc_forward_fast(pending->callid, hashed_service->phone,
|
|---|
| 124 | pending->iface, pending->arg3, 0, IPC_FF_NONE);
|
|---|
| 125 |
|
|---|
| 126 | list_remove(&pending->link);
|
|---|
| 127 | free(pending);
|
|---|
| 128 |
|
|---|
| 129 | goto loop;
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /** Register service.
|
|---|
| 134 | *
|
|---|
| 135 | * @param service Service to be registered.
|
|---|
| 136 | * @param phone Phone to be used for connections to the service.
|
|---|
| 137 | * @param call Pointer to call structure.
|
|---|
| 138 | *
|
|---|
| 139 | * @return Zero on success or a value from @ref errno.h.
|
|---|
| 140 | *
|
|---|
| 141 | */
|
|---|
| 142 | int register_service(service_t service, sysarg_t phone, ipc_call_t *call)
|
|---|
| 143 | {
|
|---|
| 144 | if (hash_table_find(&service_hash_table, &service))
|
|---|
| 145 | return EEXISTS;
|
|---|
| 146 |
|
|---|
| 147 | hashed_service_t *hashed_service =
|
|---|
| 148 | (hashed_service_t *) malloc(sizeof(hashed_service_t));
|
|---|
| 149 | if (!hashed_service)
|
|---|
| 150 | return ENOMEM;
|
|---|
| 151 |
|
|---|
| 152 | hashed_service->service = service;
|
|---|
| 153 | hashed_service->phone = phone;
|
|---|
| 154 | hashed_service->in_phone_hash = call->in_phone_hash;
|
|---|
| 155 |
|
|---|
| 156 | hash_table_insert(&service_hash_table, &hashed_service->link);
|
|---|
| 157 |
|
|---|
| 158 | return EOK;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /** Connect client to service.
|
|---|
| 162 | *
|
|---|
| 163 | * @param service Service to be connected to.
|
|---|
| 164 | * @param iface Interface to be connected to.
|
|---|
| 165 | * @param call Pointer to call structure.
|
|---|
| 166 | * @param callid Call ID of the request.
|
|---|
| 167 | *
|
|---|
| 168 | * @return Zero on success or a value from @ref errno.h.
|
|---|
| 169 | *
|
|---|
| 170 | */
|
|---|
| 171 | void connect_to_service(service_t service, iface_t iface, ipc_call_t *call,
|
|---|
| 172 | ipc_callid_t callid)
|
|---|
| 173 | {
|
|---|
| 174 | sysarg_t arg3 = IPC_GET_ARG3(*call);
|
|---|
| 175 | sysarg_t flags = IPC_GET_ARG4(*call);
|
|---|
| 176 | sysarg_t retval;
|
|---|
| 177 |
|
|---|
| 178 | ht_link_t *link = hash_table_find(&service_hash_table, &service);
|
|---|
| 179 | if (!link) {
|
|---|
| 180 | if (flags & IPC_FLAG_BLOCKING) {
|
|---|
| 181 | /* Blocking connection, add to pending list */
|
|---|
| 182 | pending_conn_t *pending =
|
|---|
| 183 | (pending_conn_t *) malloc(sizeof(pending_conn_t));
|
|---|
| 184 | if (!pending) {
|
|---|
| 185 | retval = ENOMEM;
|
|---|
| 186 | goto out;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | link_initialize(&pending->link);
|
|---|
| 190 | pending->service = service;
|
|---|
| 191 | pending->iface = iface;
|
|---|
| 192 | pending->callid = callid;
|
|---|
| 193 | pending->arg3 = arg3;
|
|---|
| 194 |
|
|---|
| 195 | list_append(&pending->link, &pending_conn);
|
|---|
| 196 | return;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | retval = ENOENT;
|
|---|
| 200 | goto out;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | hashed_service_t *hashed_service = hash_table_get_inst(link, hashed_service_t, link);
|
|---|
| 204 | (void) ipc_forward_fast(callid, hashed_service->phone, iface, arg3,
|
|---|
| 205 | 0, IPC_FF_NONE);
|
|---|
| 206 | return;
|
|---|
| 207 |
|
|---|
| 208 | out:
|
|---|
| 209 | if (!(callid & IPC_CALLID_NOTIFICATION))
|
|---|
| 210 | ipc_answer_0(callid, retval);
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | /**
|
|---|
| 214 | * @}
|
|---|
| 215 | */
|
|---|