[9a1b20c] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Jiri Svoboda
|
---|
| 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 trace
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <stdio.h>
|
---|
| 36 | #include <stdlib.h>
|
---|
[c1694b6b] | 37 | #include <str_error.h>
|
---|
[7354b5e] | 38 | #include <inttypes.h>
|
---|
[d9c8c81] | 39 | #include <adt/hash_table.h>
|
---|
[c0699467] | 40 | #include <abi/ipc/methods.h>
|
---|
[062d900] | 41 | #include <macros.h>
|
---|
[9a1b20c] | 42 | #include "ipc_desc.h"
|
---|
| 43 | #include "proto.h"
|
---|
[1643855] | 44 | #include "trace.h"
|
---|
[9a1b20c] | 45 | #include "ipcp.h"
|
---|
| 46 |
|
---|
| 47 | typedef struct {
|
---|
[96b02eb9] | 48 | sysarg_t phone_hash;
|
---|
[9a1b20c] | 49 | ipc_call_t question;
|
---|
[abf3564] | 50 | oper_t *oper;
|
---|
[9a1b20c] | 51 |
|
---|
[a5c3f73] | 52 | ipc_callid_t call_hash;
|
---|
[9a1b20c] | 53 |
|
---|
[062d900] | 54 | ht_link_t link;
|
---|
[9a1b20c] | 55 | } pending_call_t;
|
---|
| 56 |
|
---|
| 57 | typedef struct {
|
---|
| 58 | int server;
|
---|
| 59 | proto_t *proto;
|
---|
| 60 | } connection_t;
|
---|
| 61 |
|
---|
| 62 | #define MAX_PHONE 64
|
---|
| 63 | connection_t connections[MAX_PHONE];
|
---|
| 64 | int have_conn[MAX_PHONE];
|
---|
| 65 |
|
---|
[062d900] | 66 | static hash_table_t pending_calls;
|
---|
[9a1b20c] | 67 |
|
---|
[8c125ad] | 68 | /*
|
---|
| 69 | * Pseudo-protocols
|
---|
| 70 | */
|
---|
| 71 | proto_t *proto_system; /**< Protocol describing system IPC methods. */
|
---|
| 72 | proto_t *proto_unknown; /**< Protocol with no known methods. */
|
---|
| 73 |
|
---|
[9a1b20c] | 74 |
|
---|
[062d900] | 75 | static size_t pending_call_key_hash(void *key)
|
---|
[9a1b20c] | 76 | {
|
---|
[062d900] | 77 | ipc_callid_t *call_id = (ipc_callid_t *)key;
|
---|
| 78 | return *call_id;
|
---|
[9a1b20c] | 79 | }
|
---|
| 80 |
|
---|
[062d900] | 81 | static size_t pending_call_hash(const ht_link_t *item)
|
---|
[9a1b20c] | 82 | {
|
---|
[062d900] | 83 | pending_call_t *hs = hash_table_get_inst(item, pending_call_t, link);
|
---|
| 84 | return hs->call_hash;
|
---|
[9a1b20c] | 85 | }
|
---|
| 86 |
|
---|
[062d900] | 87 | static bool pending_call_key_equal(void *key, const ht_link_t *item)
|
---|
[9a1b20c] | 88 | {
|
---|
[062d900] | 89 | ipc_callid_t *call_id = (ipc_callid_t *)key;
|
---|
| 90 | pending_call_t *hs = hash_table_get_inst(item, pending_call_t, link);
|
---|
| 91 |
|
---|
| 92 | return *call_id == hs->call_hash;
|
---|
[9a1b20c] | 93 | }
|
---|
| 94 |
|
---|
[062d900] | 95 | static hash_table_ops_t pending_call_ops = {
|
---|
| 96 | .hash = pending_call_hash,
|
---|
| 97 | .key_hash = pending_call_key_hash,
|
---|
| 98 | .key_equal = pending_call_key_equal,
|
---|
[4e00f87] | 99 | .equal = NULL,
|
---|
| 100 | .remove_callback = NULL
|
---|
[062d900] | 101 | };
|
---|
| 102 |
|
---|
[9a1b20c] | 103 |
|
---|
| 104 | void ipcp_connection_set(int phone, int server, proto_t *proto)
|
---|
| 105 | {
|
---|
| 106 | if (phone <0 || phone >= MAX_PHONE) return;
|
---|
| 107 | connections[phone].server = server;
|
---|
| 108 | connections[phone].proto = proto;
|
---|
| 109 | have_conn[phone] = 1;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | void ipcp_connection_clear(int phone)
|
---|
| 113 | {
|
---|
| 114 | have_conn[phone] = 0;
|
---|
| 115 | connections[phone].server = 0;
|
---|
| 116 | connections[phone].proto = NULL;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[96b02eb9] | 119 | static void ipc_m_print(proto_t *proto, sysarg_t method)
|
---|
[9a1b20c] | 120 | {
|
---|
| 121 | oper_t *oper;
|
---|
| 122 |
|
---|
[8c125ad] | 123 | /* Try system methods first */
|
---|
| 124 | oper = proto_get_oper(proto_system, method);
|
---|
[9a1b20c] | 125 |
|
---|
[8c125ad] | 126 | if (oper == NULL && proto != NULL) {
|
---|
| 127 | /* Not a system method, try the user protocol. */
|
---|
| 128 | oper = proto_get_oper(proto, method);
|
---|
[9a1b20c] | 129 | }
|
---|
| 130 |
|
---|
[8c125ad] | 131 | if (oper != NULL) {
|
---|
[7e752b2] | 132 | printf("%s (%" PRIun ")", oper->name, method);
|
---|
[8c125ad] | 133 | return;
|
---|
[9a1b20c] | 134 | }
|
---|
| 135 |
|
---|
[7e752b2] | 136 | printf("%" PRIun, method);
|
---|
[9a1b20c] | 137 | }
|
---|
| 138 |
|
---|
| 139 | void ipcp_init(void)
|
---|
| 140 | {
|
---|
[abf3564] | 141 | val_type_t arg_def[OPER_MAX_ARGS] = {
|
---|
| 142 | V_INTEGER,
|
---|
| 143 | V_INTEGER,
|
---|
| 144 | V_INTEGER,
|
---|
| 145 | V_INTEGER,
|
---|
[40fd6f0] | 146 | V_INTEGER
|
---|
[abf3564] | 147 | };
|
---|
| 148 |
|
---|
[8c125ad] | 149 | /*
|
---|
| 150 | * Create a pseudo-protocol 'unknown' that has no known methods.
|
---|
| 151 | */
|
---|
| 152 | proto_unknown = proto_new("unknown");
|
---|
| 153 |
|
---|
| 154 | /*
|
---|
| 155 | * Create a pseudo-protocol 'system' defining names of system IPC
|
---|
| 156 | * methods.
|
---|
| 157 | */
|
---|
| 158 | proto_system = proto_new("system");
|
---|
| 159 |
|
---|
[a3b339b4] | 160 | for (size_t i = 0; i < ipc_methods_len; i++) {
|
---|
| 161 | oper_t *oper = oper_new(ipc_methods[i].name, OPER_MAX_ARGS,
|
---|
| 162 | arg_def, V_INTEGER, OPER_MAX_ARGS, arg_def);
|
---|
| 163 | proto_add_oper(proto_system, ipc_methods[i].number, oper);
|
---|
[8c125ad] | 164 | }
|
---|
| 165 |
|
---|
[062d900] | 166 | bool ok = hash_table_create(&pending_calls, 0, 0, &pending_call_ops);
|
---|
| 167 | assert(ok);
|
---|
[9a1b20c] | 168 | }
|
---|
| 169 |
|
---|
| 170 | void ipcp_cleanup(void)
|
---|
| 171 | {
|
---|
[8c125ad] | 172 | proto_delete(proto_system);
|
---|
[9a1b20c] | 173 | hash_table_destroy(&pending_calls);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | void ipcp_call_out(int phone, ipc_call_t *call, ipc_callid_t hash)
|
---|
| 177 | {
|
---|
| 178 | pending_call_t *pcall;
|
---|
| 179 | proto_t *proto;
|
---|
[1643855] | 180 | oper_t *oper;
|
---|
[96b02eb9] | 181 | sysarg_t *args;
|
---|
[abf3564] | 182 | int i;
|
---|
[9a1b20c] | 183 |
|
---|
| 184 | if (have_conn[phone]) proto = connections[phone].proto;
|
---|
| 185 | else proto = NULL;
|
---|
| 186 |
|
---|
[abf3564] | 187 | args = call->args;
|
---|
| 188 |
|
---|
[1643855] | 189 | if ((display_mask & DM_IPC) != 0) {
|
---|
[01c3bb4] | 190 | printf("Call ID: %d, phone: %d, proto: %s, method: ",
|
---|
| 191 | hash, phone,
|
---|
[7e752b2] | 192 | (proto ? proto->name : "n/a"));
|
---|
[228e490] | 193 | ipc_m_print(proto, IPC_GET_IMETHOD(*call));
|
---|
[7e752b2] | 194 | printf(" args: (%" PRIun ", %" PRIun ", %" PRIun ", "
|
---|
| 195 | "%" PRIun ", %" PRIun ")\n",
|
---|
| 196 | args[1], args[2], args[3], args[4], args[5]);
|
---|
[1643855] | 197 | }
|
---|
| 198 |
|
---|
| 199 |
|
---|
| 200 | if ((display_mask & DM_USER) != 0) {
|
---|
| 201 |
|
---|
| 202 | if (proto != NULL) {
|
---|
[228e490] | 203 | oper = proto_get_oper(proto, IPC_GET_IMETHOD(*call));
|
---|
[1643855] | 204 | } else {
|
---|
| 205 | oper = NULL;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | if (oper != NULL) {
|
---|
| 209 |
|
---|
| 210 | printf("%s(%d).%s", (proto ? proto->name : "n/a"),
|
---|
| 211 | phone, (oper ? oper->name : "unknown"));
|
---|
| 212 |
|
---|
[abf3564] | 213 | putchar('(');
|
---|
| 214 | for (i = 1; i <= oper->argc; ++i) {
|
---|
| 215 | if (i > 1) printf(", ");
|
---|
| 216 | val_print(args[i], oper->arg_type[i - 1]);
|
---|
| 217 | }
|
---|
| 218 | putchar(')');
|
---|
| 219 |
|
---|
| 220 | if (oper->rv_type == V_VOID && oper->respc == 0) {
|
---|
| 221 | /*
|
---|
| 222 | * No response data (typically the task will
|
---|
| 223 | * not be interested in the response).
|
---|
| 224 | * We will not display response.
|
---|
| 225 | */
|
---|
| 226 | putchar('.');
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | putchar('\n');
|
---|
[1643855] | 230 | }
|
---|
[abf3564] | 231 | } else {
|
---|
| 232 | oper = NULL;
|
---|
[1643855] | 233 | }
|
---|
[9a1b20c] | 234 |
|
---|
| 235 | /* Store call in hash table for response matching */
|
---|
| 236 |
|
---|
| 237 | pcall = malloc(sizeof(pending_call_t));
|
---|
| 238 | pcall->phone_hash = phone;
|
---|
| 239 | pcall->question = *call;
|
---|
| 240 | pcall->call_hash = hash;
|
---|
[abf3564] | 241 | pcall->oper = oper;
|
---|
[9a1b20c] | 242 |
|
---|
[062d900] | 243 | hash_table_insert(&pending_calls, &pcall->link);
|
---|
[9a1b20c] | 244 | }
|
---|
| 245 |
|
---|
[1643855] | 246 | static void parse_answer(ipc_callid_t hash, pending_call_t *pcall,
|
---|
| 247 | ipc_call_t *answer)
|
---|
[9a1b20c] | 248 | {
|
---|
[96b02eb9] | 249 | sysarg_t phone;
|
---|
| 250 | sysarg_t method;
|
---|
| 251 | sysarg_t service;
|
---|
[b7fd2a0] | 252 | errno_t retval;
|
---|
[9a1b20c] | 253 | proto_t *proto;
|
---|
| 254 | int cphone;
|
---|
[a35b458] | 255 |
|
---|
[96b02eb9] | 256 | sysarg_t *resp;
|
---|
[abf3564] | 257 | oper_t *oper;
|
---|
| 258 | int i;
|
---|
[a35b458] | 259 |
|
---|
[9a1b20c] | 260 | phone = pcall->phone_hash;
|
---|
[228e490] | 261 | method = IPC_GET_IMETHOD(pcall->question);
|
---|
[9a1b20c] | 262 | retval = IPC_GET_RETVAL(*answer);
|
---|
[a35b458] | 263 |
|
---|
[abf3564] | 264 | resp = answer->args;
|
---|
[a35b458] | 265 |
|
---|
[1643855] | 266 | if ((display_mask & DM_IPC) != 0) {
|
---|
[8610c2c] | 267 | printf("Response to %d: retval=%s, args = (%" PRIun ", "
|
---|
[7e752b2] | 268 | "%" PRIun ", %" PRIun ", %" PRIun ", %" PRIun ")\n",
|
---|
[8610c2c] | 269 | hash, str_error_name(retval), IPC_GET_ARG1(*answer),
|
---|
[1ccafee] | 270 | IPC_GET_ARG2(*answer), IPC_GET_ARG3(*answer),
|
---|
| 271 | IPC_GET_ARG4(*answer), IPC_GET_ARG5(*answer));
|
---|
[1643855] | 272 | }
|
---|
[a35b458] | 273 |
|
---|
[1643855] | 274 | if ((display_mask & DM_USER) != 0) {
|
---|
[abf3564] | 275 | oper = pcall->oper;
|
---|
[a35b458] | 276 |
|
---|
[79ae36dd] | 277 | if ((oper != NULL) &&
|
---|
| 278 | ((oper->rv_type != V_VOID) || (oper->respc > 0))) {
|
---|
[abf3564] | 279 | printf("->");
|
---|
[a35b458] | 280 |
|
---|
[abf3564] | 281 | if (oper->rv_type != V_VOID) {
|
---|
| 282 | putchar(' ');
|
---|
[1569a9b] | 283 | val_print((sysarg_t) retval, oper->rv_type);
|
---|
[abf3564] | 284 | }
|
---|
[a35b458] | 285 |
|
---|
[abf3564] | 286 | if (oper->respc > 0) {
|
---|
| 287 | putchar(' ');
|
---|
| 288 | putchar('(');
|
---|
| 289 | for (i = 1; i <= oper->respc; ++i) {
|
---|
[79ae36dd] | 290 | if (i > 1)
|
---|
| 291 | printf(", ");
|
---|
[abf3564] | 292 | val_print(resp[i], oper->resp_type[i - 1]);
|
---|
| 293 | }
|
---|
| 294 | putchar(')');
|
---|
| 295 | }
|
---|
[a35b458] | 296 |
|
---|
[abf3564] | 297 | putchar('\n');
|
---|
| 298 | }
|
---|
[1643855] | 299 | }
|
---|
[a35b458] | 300 |
|
---|
[79ae36dd] | 301 | if ((phone == PHONE_NS) && (method == IPC_M_CONNECT_ME_TO) &&
|
---|
| 302 | (retval == 0)) {
|
---|
[9a1b20c] | 303 | /* Connected to a service (through NS) */
|
---|
[2133e02] | 304 | service = IPC_GET_ARG2(pcall->question);
|
---|
[9a1b20c] | 305 | proto = proto_get_by_srv(service);
|
---|
[79ae36dd] | 306 | if (proto == NULL)
|
---|
| 307 | proto = proto_unknown;
|
---|
[a35b458] | 308 |
|
---|
[9a1b20c] | 309 | cphone = IPC_GET_ARG5(*answer);
|
---|
[1643855] | 310 | if ((display_mask & DM_SYSTEM) != 0) {
|
---|
| 311 | printf("Registering connection (phone %d, protocol: %s)\n", cphone,
|
---|
[79ae36dd] | 312 | proto->name);
|
---|
[1643855] | 313 | }
|
---|
[a35b458] | 314 |
|
---|
[9a1b20c] | 315 | ipcp_connection_set(cphone, 0, proto);
|
---|
| 316 | }
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | void ipcp_call_in(ipc_call_t *call, ipc_callid_t hash)
|
---|
| 320 | {
|
---|
[062d900] | 321 | ht_link_t *item;
|
---|
[9a1b20c] | 322 | pending_call_t *pcall;
|
---|
[a35b458] | 323 |
|
---|
[40fd6f0] | 324 | if ((call->flags & IPC_CALL_ANSWERED) == 0) {
|
---|
[9a1b20c] | 325 | /* Not a response */
|
---|
[1643855] | 326 | if ((display_mask & DM_IPC) != 0) {
|
---|
[01c3bb4] | 327 | printf("Not a response (hash %d)\n", hash);
|
---|
[1643855] | 328 | }
|
---|
[9a1b20c] | 329 | return;
|
---|
| 330 | }
|
---|
[a35b458] | 331 |
|
---|
[062d900] | 332 | item = hash_table_find(&pending_calls, &hash);
|
---|
[79ae36dd] | 333 | if (item == NULL)
|
---|
| 334 | return; /* No matching question found */
|
---|
[a35b458] | 335 |
|
---|
[1643855] | 336 | /*
|
---|
| 337 | * Response matched to question.
|
---|
| 338 | */
|
---|
[a35b458] | 339 |
|
---|
[062d900] | 340 | pcall = hash_table_get_inst(item, pending_call_t, link);
|
---|
| 341 | hash_table_remove(&pending_calls, &hash);
|
---|
[a35b458] | 342 |
|
---|
[1643855] | 343 | parse_answer(hash, pcall, call);
|
---|
[9a1b20c] | 344 | free(pcall);
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[b7fd2a0] | 347 | void ipcp_hangup(int phone, errno_t rc)
|
---|
[9a1b20c] | 348 | {
|
---|
[1643855] | 349 | if ((display_mask & DM_SYSTEM) != 0) {
|
---|
[c1694b6b] | 350 | printf("Hang phone %d up -> %s\n", phone, str_error_name(rc));
|
---|
[1643855] | 351 | ipcp_connection_clear(phone);
|
---|
| 352 | }
|
---|
[9a1b20c] | 353 | }
|
---|
| 354 |
|
---|
| 355 | /** @}
|
---|
| 356 | */
|
---|