[babe786] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
[babe786] | 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 |
|
---|
[231a60a] | 29 | /** @addtogroup ns
|
---|
[ce5bcb4] | 30 | * @{
|
---|
[1fcfc94] | 31 | */
|
---|
[ce5bcb4] | 32 |
|
---|
[babe786] | 33 | /**
|
---|
[1fcfc94] | 34 | * @file ns.c
|
---|
| 35 | * @brief Naming service for HelenOS IPC.
|
---|
[babe786] | 36 | */
|
---|
| 37 |
|
---|
[ce5bcb4] | 38 |
|
---|
[38edb96] | 39 | #include <ipc/ipc.h>
|
---|
| 40 | #include <ipc/ns.h>
|
---|
[51dbadf3] | 41 | #include <ipc/services.h>
|
---|
[69cdeec] | 42 | #include <stdio.h>
|
---|
[bfd1546] | 43 | #include <bool.h>
|
---|
[69cdeec] | 44 | #include <unistd.h>
|
---|
| 45 | #include <stdlib.h>
|
---|
| 46 | #include <errno.h>
|
---|
[babe786] | 47 | #include <assert.h>
|
---|
| 48 | #include <libadt/list.h>
|
---|
| 49 | #include <libadt/hash_table.h>
|
---|
[0b99e40] | 50 | #include <sysinfo.h>
|
---|
[bfd1546] | 51 | #include <loader/loader.h>
|
---|
[0b99e40] | 52 | #include <ddi.h>
|
---|
| 53 | #include <as.h>
|
---|
[babe786] | 54 |
|
---|
[1fcfc94] | 55 | #define NAME "ns"
|
---|
[babe786] | 56 |
|
---|
[1fcfc94] | 57 | #define NS_HASH_TABLE_CHAINS 20
|
---|
[babe786] | 58 |
|
---|
[043dcc27] | 59 | static int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call);
|
---|
[1fcfc94] | 60 | static void connect_to_service(ipcarg_t service, ipc_call_t *call,
|
---|
[0eb58f1] | 61 | ipc_callid_t callid);
|
---|
[043dcc27] | 62 |
|
---|
[bfd1546] | 63 | void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call,
|
---|
| 64 | ipc_callid_t callid);
|
---|
| 65 | void connect_to_clonable(ipcarg_t service, ipc_call_t *call,
|
---|
| 66 | ipc_callid_t callid);
|
---|
| 67 |
|
---|
[1fcfc94] | 68 |
|
---|
[babe786] | 69 | /* Static functions implementing NS hash table operations. */
|
---|
| 70 | static hash_index_t ns_hash(unsigned long *key);
|
---|
| 71 | static int ns_compare(unsigned long *key, hash_count_t keys, link_t *item);
|
---|
[043dcc27] | 72 | static void ns_remove(link_t *item);
|
---|
[babe786] | 73 |
|
---|
| 74 | /** Operations for NS hash table. */
|
---|
| 75 | static hash_table_operations_t ns_hash_table_ops = {
|
---|
| 76 | .hash = ns_hash,
|
---|
| 77 | .compare = ns_compare,
|
---|
[043dcc27] | 78 | .remove_callback = ns_remove
|
---|
[babe786] | 79 | };
|
---|
| 80 |
|
---|
| 81 | /** NS hash table structure. */
|
---|
| 82 | static hash_table_t ns_hash_table;
|
---|
| 83 |
|
---|
| 84 | /** NS hash table item. */
|
---|
| 85 | typedef struct {
|
---|
| 86 | link_t link;
|
---|
[1fcfc94] | 87 | ipcarg_t service; /**< Number of the service. */
|
---|
| 88 | ipcarg_t phone; /**< Phone registered with the service. */
|
---|
| 89 | ipcarg_t in_phone_hash; /**< Incoming phone hash. */
|
---|
[babe786] | 90 | } hashed_service_t;
|
---|
| 91 |
|
---|
[1fcfc94] | 92 | /** Pending connection structure. */
|
---|
| 93 | typedef struct {
|
---|
| 94 | link_t link;
|
---|
| 95 | ipcarg_t service; /**< Number of the service. */
|
---|
| 96 | ipc_callid_t callid; /**< Call ID waiting for the connection */
|
---|
| 97 | ipcarg_t arg2; /**< Second argument */
|
---|
| 98 | ipcarg_t arg3; /**< Third argument */
|
---|
| 99 | } pending_req_t;
|
---|
| 100 |
|
---|
| 101 | static link_t pending_req;
|
---|
[51dbadf3] | 102 |
|
---|
[bfd1546] | 103 | /** Request for connection to a clonable service. */
|
---|
| 104 | typedef struct {
|
---|
| 105 | link_t link;
|
---|
| 106 | ipcarg_t service;
|
---|
[76dd25b] | 107 | ipc_call_t call;
|
---|
[bfd1546] | 108 | ipc_callid_t callid;
|
---|
| 109 | } cs_req_t;
|
---|
| 110 |
|
---|
| 111 | /** List of clonable-service connection requests. */
|
---|
| 112 | static link_t cs_req;
|
---|
| 113 |
|
---|
[1fcfc94] | 114 | static void *clockaddr = NULL;
|
---|
| 115 | static void *klogaddr = NULL;
|
---|
| 116 |
|
---|
[bfd1546] | 117 | /** Return true if @a service is clonable. */
|
---|
| 118 | static bool service_clonable(int service)
|
---|
| 119 | {
|
---|
[1fcfc94] | 120 | return (service == SERVICE_LOAD);
|
---|
[bfd1546] | 121 | }
|
---|
| 122 |
|
---|
[ae318d3] | 123 | static void get_as_area(ipc_callid_t callid, ipc_call_t *call, char *name, void **addr)
|
---|
[0b99e40] | 124 | {
|
---|
| 125 | void *ph_addr;
|
---|
[ae318d3] | 126 |
|
---|
[51dbadf3] | 127 | if (!*addr) {
|
---|
[f8ddd17] | 128 | ph_addr = (void *) sysinfo_value(name);
|
---|
[0b99e40] | 129 | if (!ph_addr) {
|
---|
[b74959bd] | 130 | ipc_answer_0(callid, ENOENT);
|
---|
[0b99e40] | 131 | return;
|
---|
| 132 | }
|
---|
[2057572] | 133 | *addr = as_get_mappable_page(PAGE_SIZE);
|
---|
[ae318d3] | 134 | if (physmem_map(ph_addr, *addr, 1,
|
---|
| 135 | AS_AREA_READ | AS_AREA_CACHEABLE) != 0) {
|
---|
| 136 | ipc_answer_0(callid, ENOENT);
|
---|
| 137 | return;
|
---|
| 138 | }
|
---|
[0b99e40] | 139 | }
|
---|
[b74959bd] | 140 | ipc_answer_2(callid, EOK, (ipcarg_t) *addr, AS_AREA_READ);
|
---|
[0b99e40] | 141 | }
|
---|
| 142 |
|
---|
[1fcfc94] | 143 | /** Process pending connection requests */
|
---|
| 144 | static void process_pending_req()
|
---|
| 145 | {
|
---|
| 146 | link_t *cur;
|
---|
| 147 |
|
---|
| 148 | loop:
|
---|
| 149 | for (cur = pending_req.next; cur != &pending_req; cur = cur->next) {
|
---|
| 150 | pending_req_t *pr = list_get_instance(cur, pending_req_t, link);
|
---|
| 151 |
|
---|
| 152 | unsigned long keys[3] = {
|
---|
| 153 | pr->service,
|
---|
| 154 | 0,
|
---|
| 155 | 0
|
---|
| 156 | };
|
---|
| 157 |
|
---|
| 158 | link_t *link = hash_table_find(&ns_hash_table, keys);
|
---|
| 159 | if (!link)
|
---|
| 160 | continue;
|
---|
| 161 |
|
---|
| 162 | hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link);
|
---|
| 163 | ipcarg_t retval = ipc_forward_fast(pr->callid, hs->phone,
|
---|
| 164 | pr->arg2, pr->arg3, 0, IPC_FF_NONE);
|
---|
| 165 |
|
---|
| 166 | if (!(pr->callid & IPC_CALLID_NOTIFICATION))
|
---|
| 167 | ipc_answer_0(pr->callid, retval);
|
---|
| 168 |
|
---|
| 169 | list_remove(cur);
|
---|
| 170 | free(pr);
|
---|
| 171 | goto loop;
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[69cdeec] | 175 | int main(int argc, char **argv)
|
---|
| 176 | {
|
---|
[e623197] | 177 | printf(NAME ": HelenOS IPC Naming Service\n");
|
---|
| 178 |
|
---|
[0eb58f1] | 179 | if (!hash_table_create(&ns_hash_table, NS_HASH_TABLE_CHAINS, 3,
|
---|
| 180 | &ns_hash_table_ops)) {
|
---|
[1fcfc94] | 181 | printf(NAME ": No memory available for services\n");
|
---|
[babe786] | 182 | return ENOMEM;
|
---|
| 183 | }
|
---|
[1fcfc94] | 184 |
|
---|
| 185 | list_initialize(&pending_req);
|
---|
[bfd1546] | 186 | list_initialize(&cs_req);
|
---|
[e623197] | 187 |
|
---|
| 188 | printf(NAME ": Accepting connections\n");
|
---|
[1fcfc94] | 189 | while (true) {
|
---|
| 190 | process_pending_req();
|
---|
| 191 |
|
---|
| 192 | ipc_call_t call;
|
---|
| 193 | ipc_callid_t callid = ipc_wait_for_call(&call);
|
---|
| 194 | ipcarg_t retval;
|
---|
| 195 |
|
---|
[4c61e60] | 196 | switch (IPC_GET_METHOD(call)) {
|
---|
[27d293a] | 197 | case IPC_M_SHARE_IN:
|
---|
[51dbadf3] | 198 | switch (IPC_GET_ARG3(call)) {
|
---|
| 199 | case SERVICE_MEM_REALTIME:
|
---|
[ae318d3] | 200 | get_as_area(callid, &call, "clock.faddr", &clockaddr);
|
---|
[51dbadf3] | 201 | break;
|
---|
| 202 | case SERVICE_MEM_KLOG:
|
---|
[ae318d3] | 203 | get_as_area(callid, &call, "klog.faddr", &klogaddr);
|
---|
[51dbadf3] | 204 | break;
|
---|
| 205 | default:
|
---|
[b74959bd] | 206 | ipc_answer_0(callid, ENOENT);
|
---|
[51dbadf3] | 207 | }
|
---|
[0b99e40] | 208 | continue;
|
---|
[7048773] | 209 | case IPC_M_PHONE_HUNGUP:
|
---|
[20614d0] | 210 | retval = EOK;
|
---|
[7048773] | 211 | break;
|
---|
[043dcc27] | 212 | case IPC_M_CONNECT_TO_ME:
|
---|
[babe786] | 213 | /*
|
---|
[043dcc27] | 214 | * Server requests service registration.
|
---|
[babe786] | 215 | */
|
---|
[bfd1546] | 216 | if (service_clonable(IPC_GET_ARG1(call))) {
|
---|
| 217 | register_clonable(IPC_GET_ARG1(call),
|
---|
| 218 | IPC_GET_ARG5(call), &call, callid);
|
---|
| 219 | continue;
|
---|
| 220 | } else {
|
---|
| 221 | retval = register_service(IPC_GET_ARG1(call),
|
---|
| 222 | IPC_GET_ARG5(call), &call);
|
---|
| 223 | }
|
---|
[69cdeec] | 224 | break;
|
---|
[7048773] | 225 | case IPC_M_CONNECT_ME_TO:
|
---|
[043dcc27] | 226 | /*
|
---|
| 227 | * Client requests to be connected to a service.
|
---|
| 228 | */
|
---|
[bfd1546] | 229 | if (service_clonable(IPC_GET_ARG1(call))) {
|
---|
| 230 | connect_to_clonable(IPC_GET_ARG1(call),
|
---|
| 231 | &call, callid);
|
---|
| 232 | continue;
|
---|
| 233 | } else {
|
---|
[1fcfc94] | 234 | connect_to_service(IPC_GET_ARG1(call), &call,
|
---|
| 235 | callid);
|
---|
| 236 | continue;
|
---|
[bfd1546] | 237 | }
|
---|
[11eae82] | 238 | break;
|
---|
[69cdeec] | 239 | default:
|
---|
| 240 | retval = ENOENT;
|
---|
| 241 | break;
|
---|
| 242 | }
|
---|
[1fcfc94] | 243 |
|
---|
| 244 | if (!(callid & IPC_CALLID_NOTIFICATION))
|
---|
[b74959bd] | 245 | ipc_answer_0(callid, retval);
|
---|
[69cdeec] | 246 | }
|
---|
[e623197] | 247 |
|
---|
| 248 | /* Not reached */
|
---|
| 249 | return 0;
|
---|
[69cdeec] | 250 | }
|
---|
[babe786] | 251 |
|
---|
[108602e] | 252 | /** Register service.
|
---|
[043dcc27] | 253 | *
|
---|
[1fcfc94] | 254 | * @param service Service to be registered.
|
---|
| 255 | * @param phone Phone to be used for connections to the service.
|
---|
| 256 | * @param call Pointer to call structure.
|
---|
| 257 | *
|
---|
| 258 | * @return Zero on success or a value from @ref errno.h.
|
---|
[043dcc27] | 259 | *
|
---|
| 260 | */
|
---|
| 261 | int register_service(ipcarg_t service, ipcarg_t phone, ipc_call_t *call)
|
---|
| 262 | {
|
---|
[0eb58f1] | 263 | unsigned long keys[3] = {
|
---|
| 264 | service,
|
---|
| 265 | call->in_phone_hash,
|
---|
| 266 | 0
|
---|
| 267 | };
|
---|
[1fcfc94] | 268 |
|
---|
| 269 | if (hash_table_find(&ns_hash_table, keys))
|
---|
[043dcc27] | 270 | return EEXISTS;
|
---|
[1fcfc94] | 271 |
|
---|
| 272 | hashed_service_t *hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
|
---|
| 273 | if (!hs)
|
---|
[043dcc27] | 274 | return ENOMEM;
|
---|
[1fcfc94] | 275 |
|
---|
[043dcc27] | 276 | link_initialize(&hs->link);
|
---|
| 277 | hs->service = service;
|
---|
| 278 | hs->phone = phone;
|
---|
| 279 | hs->in_phone_hash = call->in_phone_hash;
|
---|
| 280 | hash_table_insert(&ns_hash_table, keys, &hs->link);
|
---|
[1fcfc94] | 281 |
|
---|
[043dcc27] | 282 | return 0;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | /** Connect client to service.
|
---|
| 286 | *
|
---|
[1fcfc94] | 287 | * @param service Service to be connected to.
|
---|
| 288 | * @param call Pointer to call structure.
|
---|
| 289 | * @param callid Call ID of the request.
|
---|
[043dcc27] | 290 | *
|
---|
| 291 | * @return Zero on success or a value from @ref errno.h.
|
---|
[1fcfc94] | 292 | *
|
---|
[043dcc27] | 293 | */
|
---|
[1fcfc94] | 294 | void connect_to_service(ipcarg_t service, ipc_call_t *call, ipc_callid_t callid)
|
---|
[043dcc27] | 295 | {
|
---|
[1fcfc94] | 296 | ipcarg_t retval;
|
---|
| 297 | unsigned long keys[3] = {
|
---|
| 298 | service,
|
---|
| 299 | 0,
|
---|
| 300 | 0
|
---|
| 301 | };
|
---|
| 302 |
|
---|
| 303 | link_t *link = hash_table_find(&ns_hash_table, keys);
|
---|
| 304 | if (!link) {
|
---|
| 305 | if (IPC_GET_ARG4(*call) & IPC_FLAG_BLOCKING) {
|
---|
| 306 | /* Blocking connection, add to pending list */
|
---|
| 307 | pending_req_t *pr = (pending_req_t *) malloc(sizeof(pending_req_t));
|
---|
| 308 | if (!pr) {
|
---|
| 309 | retval = ENOMEM;
|
---|
| 310 | goto out;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | pr->service = service;
|
---|
| 314 | pr->callid = callid;
|
---|
| 315 | pr->arg2 = IPC_GET_ARG2(*call);
|
---|
| 316 | pr->arg3 = IPC_GET_ARG3(*call);
|
---|
| 317 | list_append(&pr->link, &pending_req);
|
---|
| 318 | return;
|
---|
| 319 | }
|
---|
| 320 | retval = ENOENT;
|
---|
| 321 | goto out;
|
---|
[043dcc27] | 322 | }
|
---|
[1fcfc94] | 323 |
|
---|
| 324 | hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link);
|
---|
| 325 | retval = ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call),
|
---|
| 326 | IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
|
---|
| 327 | out:
|
---|
| 328 | if (!(callid & IPC_CALLID_NOTIFICATION))
|
---|
| 329 | ipc_answer_0(callid, retval);
|
---|
[043dcc27] | 330 | }
|
---|
| 331 |
|
---|
[bfd1546] | 332 | /** Register clonable service.
|
---|
| 333 | *
|
---|
[1fcfc94] | 334 | * @param service Service to be registered.
|
---|
| 335 | * @param phone Phone to be used for connections to the service.
|
---|
| 336 | * @param call Pointer to call structure.
|
---|
| 337 | *
|
---|
[bfd1546] | 338 | */
|
---|
| 339 | void register_clonable(ipcarg_t service, ipcarg_t phone, ipc_call_t *call,
|
---|
| 340 | ipc_callid_t callid)
|
---|
| 341 | {
|
---|
| 342 | if (list_empty(&cs_req)) {
|
---|
| 343 | /* There was no pending connection request. */
|
---|
| 344 | printf(NAME ": Unexpected clonable server.\n");
|
---|
| 345 | ipc_answer_0(callid, EBUSY);
|
---|
| 346 | return;
|
---|
| 347 | }
|
---|
[1fcfc94] | 348 |
|
---|
| 349 | cs_req_t *csr = list_get_instance(cs_req.next, cs_req_t, link);
|
---|
[bfd1546] | 350 | list_remove(&csr->link);
|
---|
[1fcfc94] | 351 |
|
---|
[bfd1546] | 352 | /* Currently we can only handle a single type of clonable service. */
|
---|
| 353 | assert(csr->service == SERVICE_LOAD);
|
---|
[1fcfc94] | 354 |
|
---|
[bfd1546] | 355 | ipc_answer_0(callid, EOK);
|
---|
[1fcfc94] | 356 |
|
---|
| 357 | int rc = ipc_forward_fast(csr->callid, phone, IPC_GET_ARG2(csr->call),
|
---|
[76dd25b] | 358 | IPC_GET_ARG3(csr->call), 0, IPC_FF_NONE);
|
---|
[482c86f] | 359 |
|
---|
[bfd1546] | 360 | free(csr);
|
---|
[482c86f] | 361 | ipc_hangup(phone);
|
---|
[bfd1546] | 362 | }
|
---|
| 363 |
|
---|
| 364 | /** Connect client to clonable service.
|
---|
| 365 | *
|
---|
[1fcfc94] | 366 | * @param service Service to be connected to.
|
---|
| 367 | * @param call Pointer to call structure.
|
---|
| 368 | * @param callid Call ID of the request.
|
---|
| 369 | *
|
---|
| 370 | * @return Zero on success or a value from @ref errno.h.
|
---|
[bfd1546] | 371 | *
|
---|
| 372 | */
|
---|
| 373 | void connect_to_clonable(ipcarg_t service, ipc_call_t *call,
|
---|
| 374 | ipc_callid_t callid)
|
---|
| 375 | {
|
---|
| 376 | assert(service == SERVICE_LOAD);
|
---|
[1fcfc94] | 377 |
|
---|
| 378 | cs_req_t *csr = malloc(sizeof(cs_req_t));
|
---|
[bfd1546] | 379 | if (csr == NULL) {
|
---|
| 380 | ipc_answer_0(callid, ENOMEM);
|
---|
| 381 | return;
|
---|
| 382 | }
|
---|
[1fcfc94] | 383 |
|
---|
[bfd1546] | 384 | /* Spawn a loader. */
|
---|
[1fcfc94] | 385 | int rc = loader_spawn("loader");
|
---|
| 386 |
|
---|
[bfd1546] | 387 | if (rc < 0) {
|
---|
| 388 | free(csr);
|
---|
| 389 | ipc_answer_0(callid, rc);
|
---|
| 390 | return;
|
---|
| 391 | }
|
---|
[1fcfc94] | 392 |
|
---|
[bfd1546] | 393 | csr->service = service;
|
---|
[76dd25b] | 394 | csr->call = *call;
|
---|
[bfd1546] | 395 | csr->callid = callid;
|
---|
[1fcfc94] | 396 |
|
---|
[bfd1546] | 397 | /*
|
---|
| 398 | * We can forward the call only after the server we spawned connects
|
---|
| 399 | * to us. Meanwhile we might need to service more connection requests.
|
---|
| 400 | * Thus we store the call in a queue.
|
---|
| 401 | */
|
---|
| 402 | list_append(&csr->link, &cs_req);
|
---|
| 403 | }
|
---|
| 404 |
|
---|
[babe786] | 405 | /** Compute hash index into NS hash table.
|
---|
| 406 | *
|
---|
[043dcc27] | 407 | * @param key Pointer keys. However, only the first key (i.e. service number)
|
---|
[1fcfc94] | 408 | * is used to compute the hash index.
|
---|
| 409 | *
|
---|
[043dcc27] | 410 | * @return Hash index corresponding to key[0].
|
---|
[1fcfc94] | 411 | *
|
---|
[babe786] | 412 | */
|
---|
| 413 | hash_index_t ns_hash(unsigned long *key)
|
---|
| 414 | {
|
---|
| 415 | assert(key);
|
---|
[1fcfc94] | 416 | return (*key % NS_HASH_TABLE_CHAINS);
|
---|
[babe786] | 417 | }
|
---|
| 418 |
|
---|
| 419 | /** Compare a key with hashed item.
|
---|
| 420 | *
|
---|
[043dcc27] | 421 | * This compare function always ignores the third key.
|
---|
| 422 | * It exists only to make it possible to remove records
|
---|
| 423 | * originating from connection with key[1] in_phone_hash
|
---|
| 424 | * value. Note that this is close to being classified
|
---|
| 425 | * as a nasty hack.
|
---|
| 426 | *
|
---|
[1fcfc94] | 427 | * @param key Array of keys.
|
---|
[043dcc27] | 428 | * @param keys Must be lesser or equal to 3.
|
---|
[babe786] | 429 | * @param item Pointer to a hash table item.
|
---|
[1fcfc94] | 430 | *
|
---|
[babe786] | 431 | * @return Non-zero if the key matches the item, zero otherwise.
|
---|
[1fcfc94] | 432 | *
|
---|
[babe786] | 433 | */
|
---|
[043dcc27] | 434 | int ns_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
[babe786] | 435 | {
|
---|
| 436 | assert(key);
|
---|
[043dcc27] | 437 | assert(keys <= 3);
|
---|
[babe786] | 438 | assert(item);
|
---|
| 439 |
|
---|
[1fcfc94] | 440 | hashed_service_t *hs = hash_table_get_instance(item, hashed_service_t, link);
|
---|
[babe786] | 441 |
|
---|
[043dcc27] | 442 | if (keys == 2)
|
---|
| 443 | return key[1] == hs->in_phone_hash;
|
---|
| 444 | else
|
---|
| 445 | return key[0] == hs->service;
|
---|
| 446 | }
|
---|
| 447 |
|
---|
| 448 | /** Perform actions after removal of item from the hash table.
|
---|
| 449 | *
|
---|
| 450 | * @param item Item that was removed from the hash table.
|
---|
[1fcfc94] | 451 | *
|
---|
[043dcc27] | 452 | */
|
---|
| 453 | void ns_remove(link_t *item)
|
---|
| 454 | {
|
---|
| 455 | assert(item);
|
---|
| 456 | free(hash_table_get_instance(item, hashed_service_t, link));
|
---|
[babe786] | 457 | }
|
---|
[a46da63] | 458 |
|
---|
[1fcfc94] | 459 | /**
|
---|
[ce5bcb4] | 460 | * @}
|
---|
| 461 | */
|
---|