| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "asterisk.h" |
| 20 |
|
| 21 |
#include <pthread.h> |
| 22 |
|
| 23 |
#include "asterisk/astobj2.h" |
| 24 |
#include "asterisk/strings.h" |
| 25 |
|
| 26 |
#include "asterisk/res_aeap.h" |
| 27 |
#include "asterisk/res_aeap_message.h" |
| 28 |
|
| 29 |
#include "logger.h" |
| 30 |
#include "transaction.h" |
| 31 |
#include "transport.h" |
| 32 |
|
| 33 |
#define AEAP_RECV_SIZE 32768 |
| 34 |
|
| 35 |
struct aeap_user_data { |
| 36 |
|
| 37 |
void *obj; |
| 38 |
|
| 39 |
char id[0]; |
| 40 |
}; |
| 41 |
|
| 42 |
AO2_STRING_FIELD_HASH_FN(aeap_user_data, id); |
| 43 |
AO2_STRING_FIELD_CMP_FN(aeap_user_data, id); |
| 44 |
|
| 45 |
#define USER_DATA_BUCKETS 11 |
| 46 |
|
| 47 |
struct ast_aeap { |
| 48 |
|
| 49 |
const struct ast_aeap_params *params; |
| 50 |
|
| 51 |
struct ao2_container *user_data; |
| 52 |
|
| 53 |
struct ao2_container *transactions; |
| 54 |
|
| 55 |
struct aeap_transport *transport; |
| 56 |
|
| 57 |
pthread_t read_thread_id; |
| 58 |
}; |
| 59 |
|
| 60 |
static int tsx_end(void *obj, void *arg, int flags) |
| 61 |
{ |
| 62 |
aeap_transaction_end(obj, -1); |
| 63 |
|
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
static void aeap_destructor(void *obj) |
| 68 |
{ |
| 69 |
struct ast_aeap *aeap = obj; |
| 70 |
|
| 71 |
|
| 72 |
ast_aeap_disconnect(aeap); |
| 73 |
|
| 74 |
aeap_transport_destroy(aeap->transport); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
ao2_callback(aeap->transactions, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, |
| 82 |
tsx_end, NULL); |
| 83 |
ao2_cleanup(aeap->transactions); |
| 84 |
|
| 85 |
ao2_cleanup(aeap->user_data); |
| 86 |
} |
| 87 |
|
| 88 |
struct ast_aeap *ast_aeap_create(const char *transport_type, |
| 89 |
const struct ast_aeap_params *params) |
| 90 |
{ |
| 91 |
struct ast_aeap *aeap; |
| 92 |
|
| 93 |
aeap = ao2_alloc(sizeof(*aeap), aeap_destructor); |
| 94 |
if (!aeap) { |
| 95 |
ast_log(LOG_ERROR, "AEAP: unable to create"); |
| 96 |
return NULL; |
| 97 |
} |
| 98 |
|
| 99 |
aeap->params = params; |
| 100 |
aeap->read_thread_id = AST_PTHREADT_NULL; |
| 101 |
|
| 102 |
aeap->user_data = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, USER_DATA_BUCKETS, |
| 103 |
aeap_user_data_hash_fn, NULL, aeap_user_data_cmp_fn); |
| 104 |
if (!aeap->user_data) { |
| 105 |
aeap_error(aeap, NULL, "unable to create user data container"); |
| 106 |
ao2_ref(aeap, -1); |
| 107 |
return NULL; |
| 108 |
} |
| 109 |
|
| 110 |
aeap->transactions = aeap_transactions_create(); |
| 111 |
if (!aeap->transactions) { |
| 112 |
aeap_error(aeap, NULL, "unable to create transactions container"); |
| 113 |
ao2_ref(aeap, -1); |
| 114 |
return NULL; |
| 115 |
} |
| 116 |
|
| 117 |
aeap->transport = aeap_transport_create(transport_type); |
| 118 |
if (!aeap->transport) { |
| 119 |
aeap_error(aeap, NULL, "unable to create transport"); |
| 120 |
ao2_ref(aeap, -1); |
| 121 |
return NULL; |
| 122 |
} |
| 123 |
|
| 124 |
return aeap; |
| 125 |
} |
| 126 |
|
| 127 |
static struct aeap_user_data *aeap_user_data_create(const char *id, void *obj, |
| 128 |
ast_aeap_user_obj_cleanup cleanup) |
| 129 |
{ |
| 130 |
struct aeap_user_data *data; |
| 131 |
|
| 132 |
ast_assert(id != NULL); |
| 133 |
|
| 134 |
data = ao2_t_alloc_options(sizeof(*data) + strlen(id) + 1, cleanup, |
| 135 |
AO2_ALLOC_OPT_LOCK_NOLOCK, ""); |
| 136 |
if (!data) { |
| 137 |
if (cleanup) { |
| 138 |
cleanup(obj); |
| 139 |
} |
| 140 |
|
| 141 |
return NULL; |
| 142 |
} |
| 143 |
|
| 144 |
strcpy(data->id, id); |
| 145 |
data->obj = obj; |
| 146 |
|
| 147 |
return data; |
| 148 |
} |
| 149 |
|
| 150 |
int ast_aeap_user_data_register(struct ast_aeap *aeap, const char *id, void *obj, |
| 151 |
ast_aeap_user_obj_cleanup cleanup) |
| 152 |
{ |
| 153 |
struct aeap_user_data *data; |
| 154 |
|
| 155 |
data = aeap_user_data_create(id, obj, cleanup); |
| 156 |
if (!data) { |
| 157 |
return -1; |
| 158 |
} |
| 159 |
|
| 160 |
if (!ao2_link(aeap->user_data, data)) { |
| 161 |
ao2_ref(data, -1); |
| 162 |
return -1; |
| 163 |
} |
| 164 |
|
| 165 |
ao2_ref(data, -1); |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
void ast_aeap_user_data_unregister(struct ast_aeap *aeap, const char *id) |
| 170 |
{ |
| 171 |
ao2_find(aeap->user_data, id, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA); |
| 172 |
} |
| 173 |
|
| 174 |
void *ast_aeap_user_data_object_by_id(struct ast_aeap *aeap, const char *id) |
| 175 |
{ |
| 176 |
struct aeap_user_data *data; |
| 177 |
void *obj; |
| 178 |
|
| 179 |
data = ao2_find(aeap->user_data, id, OBJ_SEARCH_KEY); |
| 180 |
if (!data) { |
| 181 |
return NULL; |
| 182 |
} |
| 183 |
|
| 184 |
obj = data->obj; |
| 185 |
ao2_ref(data, -1); |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
return obj; |
| 192 |
} |
| 193 |
|
| 194 |
static int raise_msg_handler(struct ast_aeap *aeap, const struct ast_aeap_message_handler *handlers, |
| 195 |
size_t size, struct ast_aeap_message *msg, void *data) |
| 196 |
{ |
| 197 |
ast_aeap_on_message on_message = NULL; |
| 198 |
size_t i; |
| 199 |
|
| 200 |
if (!aeap->params->emit_error) { |
| 201 |
const char *error_msg = ast_aeap_message_error_msg(msg); |
| 202 |
|
| 203 |
if (error_msg) { |
| 204 |
aeap_error(aeap, NULL, "%s", error_msg); |
| 205 |
return -1; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
} |
| 210 |
|
| 211 |
for (i = 0; i < size; ++i) { |
| 212 |
if (ast_strlen_zero(handlers[i].name)) { |
| 213 |
|
| 214 |
on_message = handlers[i].on_message; |
| 215 |
continue; |
| 216 |
} |
| 217 |
|
| 218 |
if (ast_aeap_message_is_named(msg, handlers[i].name)) { |
| 219 |
on_message = handlers[i].on_message; |
| 220 |
break; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
if (on_message) { |
| 225 |
return on_message(aeap, msg, data); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
ast_aeap_send_msg(aeap, ast_aeap_message_create_error(aeap->params->msg_type, |
| 230 |
ast_aeap_message_name(msg), ast_aeap_message_id(msg), |
| 231 |
"Unsupported and/or un-handled message")); |
| 232 |
|
| 233 |
return 0; |
| 234 |
} |
| 235 |
|
| 236 |
static void raise_msg(struct ast_aeap *aeap, const void *buf, intmax_t size, |
| 237 |
enum AST_AEAP_DATA_TYPE serial_type) |
| 238 |
{ |
| 239 |
struct ast_aeap_message *msg; |
| 240 |
struct aeap_transaction *tsx; |
| 241 |
int res = 0; |
| 242 |
|
| 243 |
if (!aeap->params || !aeap->params->msg_type || |
| 244 |
ast_aeap_message_serial_type(aeap->params->msg_type) != serial_type || |
| 245 |
!(msg = ast_aeap_message_deserialize(aeap->params->msg_type, buf, size))) { |
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
tsx = aeap_transaction_get(aeap->transactions, ast_aeap_message_id(msg)); |
| 251 |
|
| 252 |
|
| 253 |
aeap_transaction_cancel_timer(tsx); |
| 254 |
|
| 255 |
if (aeap->params->request_handlers && ast_aeap_message_is_request(msg)) { |
| 256 |
res = raise_msg_handler(aeap, aeap->params->request_handlers, aeap->params->request_handlers_size, |
| 257 |
msg, tsx ? aeap_transaction_user_obj(tsx) : NULL); |
| 258 |
} else if (aeap->params->response_handlers && ast_aeap_message_is_response(msg)) { |
| 259 |
res = raise_msg_handler(aeap, aeap->params->response_handlers, aeap->params->response_handlers_size, |
| 260 |
msg, tsx ? aeap_transaction_user_obj(tsx) : NULL); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
aeap_transaction_end(tsx, res); |
| 265 |
|
| 266 |
ao2_ref(msg, -1); |
| 267 |
} |
| 268 |
|
| 269 |
static void *aeap_receive(void *data) |
| 270 |
{ |
| 271 |
struct ast_aeap *aeap = data; |
| 272 |
void *buf; |
| 273 |
|
| 274 |
buf = ast_calloc(1, AEAP_RECV_SIZE); |
| 275 |
if (!buf) { |
| 276 |
aeap_error(aeap, NULL, "unable to create read buffer"); |
| 277 |
goto aeap_receive_error; |
| 278 |
} |
| 279 |
|
| 280 |
while (aeap_transport_is_connected(aeap->transport)) { |
| 281 |
enum AST_AEAP_DATA_TYPE rtype; |
| 282 |
intmax_t size; |
| 283 |
|
| 284 |
size = aeap_transport_read(aeap->transport, buf, AEAP_RECV_SIZE, &rtype); |
| 285 |
if (size < 0) { |
| 286 |
goto aeap_receive_error; |
| 287 |
} |
| 288 |
|
| 289 |
if (!size) { |
| 290 |
continue; |
| 291 |
} |
| 292 |
|
| 293 |
switch (rtype) { |
| 294 |
case AST_AEAP_DATA_TYPE_BINARY: |
| 295 |
if (aeap->params && aeap->params->on_binary) { |
| 296 |
aeap->params->on_binary(aeap, buf, size); |
| 297 |
} |
| 298 |
break; |
| 299 |
case AST_AEAP_DATA_TYPE_STRING: |
| 300 |
ast_debug(3, "AEAP: received message: %s\n", (char *)buf); |
| 301 |
if (aeap->params && aeap->params->on_string) { |
| 302 |
aeap->params->on_string(aeap, (const char *)buf, size - 1); |
| 303 |
} |
| 304 |
break; |
| 305 |
default: |
| 306 |
break; |
| 307 |
} |
| 308 |
|
| 309 |
raise_msg(aeap, buf, size, rtype); |
| 310 |
}; |
| 311 |
|
| 312 |
ast_free(buf); |
| 313 |
return NULL; |
| 314 |
|
| 315 |
aeap_receive_error: |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
aeap_error(aeap, NULL, "unrecoverable read error, disconnecting"); |
| 322 |
|
| 323 |
ao2_lock(aeap); |
| 324 |
aeap->read_thread_id = AST_PTHREADT_NULL; |
| 325 |
ao2_unlock(aeap); |
| 326 |
|
| 327 |
ast_aeap_disconnect(aeap); |
| 328 |
|
| 329 |
ast_free(buf); |
| 330 |
|
| 331 |
if (aeap->params && aeap->params->on_error) { |
| 332 |
aeap->params->on_error(aeap); |
| 333 |
} |
| 334 |
|
| 335 |
return NULL; |
| 336 |
} |
| 337 |
|
| 338 |
int ast_aeap_connect(struct ast_aeap *aeap, const char *url, const char *protocol, int timeout) |
| 339 |
{ |
| 340 |
SCOPED_AO2LOCK(lock, aeap); |
| 341 |
|
| 342 |
if (aeap_transport_is_connected(aeap->transport)) { |
| 343 |
|
| 344 |
return 0; |
| 345 |
} |
| 346 |
|
| 347 |
if (aeap_transport_connect(aeap->transport, url, protocol, timeout)) { |
| 348 |
aeap_error(aeap, NULL, "unable to connect transport"); |
| 349 |
return -1; |
| 350 |
} |
| 351 |
|
| 352 |
if (ast_pthread_create_background(&aeap->read_thread_id, NULL, |
| 353 |
aeap_receive, aeap)) { |
| 354 |
aeap_error(aeap, NULL, "unable to start read thread: %s", |
| 355 |
strerror(errno)); |
| 356 |
ast_aeap_disconnect(aeap); |
| 357 |
return -1; |
| 358 |
} |
| 359 |
|
| 360 |
return 0; |
| 361 |
} |
| 362 |
|
| 363 |
struct ast_aeap *ast_aeap_create_and_connect(const char *type, |
| 364 |
const struct ast_aeap_params *params, const char *url, const char *protocol, int timeout) |
| 365 |
{ |
| 366 |
struct ast_aeap *aeap; |
| 367 |
|
| 368 |
aeap = ast_aeap_create(type, params); |
| 369 |
if (!aeap) { |
| 370 |
return NULL; |
| 371 |
} |
| 372 |
|
| 373 |
if (ast_aeap_connect(aeap, url, protocol, timeout)) { |
| 374 |
ao2_ref(aeap, -1); |
| 375 |
return NULL; |
| 376 |
} |
| 377 |
|
| 378 |
return aeap; |
| 379 |
} |
| 380 |
|
| 381 |
int ast_aeap_disconnect(struct ast_aeap *aeap) |
| 382 |
{ |
| 383 |
ao2_lock(aeap); |
| 384 |
|
| 385 |
aeap_transport_disconnect(aeap->transport); |
| 386 |
|
| 387 |
if (aeap->read_thread_id != AST_PTHREADT_NULL) { |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
ao2_unlock(aeap); |
| 393 |
pthread_join(aeap->read_thread_id, NULL); |
| 394 |
ao2_lock(aeap); |
| 395 |
|
| 396 |
aeap->read_thread_id = AST_PTHREADT_NULL; |
| 397 |
} |
| 398 |
|
| 399 |
ao2_unlock(aeap); |
| 400 |
|
| 401 |
return 0; |
| 402 |
} |
| 403 |
|
| 404 |
static int aeap_send(struct ast_aeap *aeap, const void *buf, uintmax_t size, |
| 405 |
enum AST_AEAP_DATA_TYPE type) |
| 406 |
{ |
| 407 |
intmax_t num; |
| 408 |
|
| 409 |
num = aeap_transport_write(aeap->transport, buf, size, type); |
| 410 |
|
| 411 |
if (num == 0) { |
| 412 |
|
| 413 |
return 0; |
| 414 |
} |
| 415 |
|
| 416 |
if (num < 0) { |
| 417 |
aeap_error(aeap, NULL, "error sending data"); |
| 418 |
return -1; |
| 419 |
} |
| 420 |
|
| 421 |
if (num < size) { |
| 422 |
aeap_error(aeap, NULL, "not all data sent"); |
| 423 |
return -1; |
| 424 |
} |
| 425 |
|
| 426 |
if (num > size) { |
| 427 |
aeap_error(aeap, NULL, "sent data truncated"); |
| 428 |
return -1; |
| 429 |
} |
| 430 |
|
| 431 |
return 0; |
| 432 |
} |
| 433 |
|
| 434 |
int ast_aeap_send_binary(struct ast_aeap *aeap, const void *buf, uintmax_t size) |
| 435 |
{ |
| 436 |
return aeap_send(aeap, buf, size, AST_AEAP_DATA_TYPE_BINARY); |
| 437 |
} |
| 438 |
|
| 439 |
int ast_aeap_send_msg(struct ast_aeap *aeap, struct ast_aeap_message *msg) |
| 440 |
{ |
| 441 |
void *buf; |
| 442 |
intmax_t size; |
| 443 |
int res; |
| 444 |
|
| 445 |
if (!msg) { |
| 446 |
aeap_error(aeap, NULL, "no message to send"); |
| 447 |
return -1; |
| 448 |
} |
| 449 |
|
| 450 |
if (ast_aeap_message_serialize(msg, &buf, &size)) { |
| 451 |
aeap_error(aeap, NULL, "unable to serialize outgoing message"); |
| 452 |
ao2_ref(msg, -1); |
| 453 |
return -1; |
| 454 |
} |
| 455 |
|
| 456 |
res = aeap_send(aeap, buf, size, msg->type->serial_type); |
| 457 |
|
| 458 |
ast_free(buf); |
| 459 |
ao2_ref(msg, -1); |
| 460 |
|
| 461 |
return res; |
| 462 |
} |
| 463 |
|
| 464 |
int ast_aeap_send_msg_tsx(struct ast_aeap *aeap, struct ast_aeap_tsx_params *params) |
| 465 |
{ |
| 466 |
struct aeap_transaction *tsx = NULL; |
| 467 |
int res = 0; |
| 468 |
|
| 469 |
if (!params) { |
| 470 |
return -1; |
| 471 |
} |
| 472 |
|
| 473 |
if (!params->msg) { |
| 474 |
aeap_transaction_params_cleanup(params); |
| 475 |
aeap_error(aeap, NULL, "no message to send"); |
| 476 |
return -1; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
tsx = aeap_transaction_create_and_add(aeap->transactions, |
| 481 |
ast_aeap_message_id(params->msg), params, aeap); |
| 482 |
if (!tsx) { |
| 483 |
return -1; |
| 484 |
} |
| 485 |
|
| 486 |
if (ast_aeap_send_msg(aeap, ao2_bump(params->msg))) { |
| 487 |
aeap_transaction_end(tsx, -1); |
| 488 |
return -1; |
| 489 |
} |
| 490 |
|
| 491 |
if (aeap_transaction_start(tsx)) { |
| 492 |
aeap_transaction_end(tsx, -1); |
| 493 |
return -1; |
| 494 |
} |
| 495 |
|
| 496 |
res = aeap_transaction_result(tsx); |
| 497 |
|
| 498 |
ao2_ref(tsx, -1); |
| 499 |
|
| 500 |
return res; |
| 501 |
} |