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