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