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