| 1 |
/* |
| 2 |
* Copyright(c) 2006 to 2020 ZettaScale Technology and others |
| 3 |
* |
| 4 |
* This program and the accompanying materials are made available under the |
| 5 |
* terms of the Eclipse Public License v. 2.0 which is available at |
| 6 |
* http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License |
| 7 |
* v. 1.0 which is available at |
| 8 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 9 |
* |
| 10 |
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
| 11 |
*/ |
| 12 |
#include <string.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include "dds/dds.h" |
| 15 |
#include "dds/ddsrt/heap.h" |
| 16 |
#include "dds/ddsrt/sync.h" |
| 17 |
#include "dds/ddsrt/string.h" |
| 18 |
#include "dds/ddsi/ddsi_domaingv.h" |
| 19 |
#include "dds/security/dds_security_api.h" |
| 20 |
#include "dds/security/core/dds_security_utils.h" |
| 21 |
#include "plugin_wrapper_msg_q.h" |
| 22 |
|
| 23 |
void insert_message(struct message_queue *queue, struct message *msg) |
| 24 |
{ |
| 25 |
ddsrt_mutex_lock(&queue->lock); |
| 26 |
if (!queue->head) |
| 27 |
queue->head = msg; |
| 28 |
else |
| 29 |
queue->tail->next = msg; |
| 30 |
queue->tail = msg; |
| 31 |
|
| 32 |
ddsrt_cond_signal(&queue->cond); |
| 33 |
ddsrt_mutex_unlock(&queue->lock); |
| 34 |
} |
| 35 |
|
| 36 |
void add_message(struct message_queue *queue, message_kind_t kind, DDS_Security_IdentityHandle lidHandle, DDS_Security_IdentityHandle ridHandle, DDS_Security_IdentityHandle hsHandle, |
| 37 |
const DDS_Security_GUID_t *lguid, const DDS_Security_GUID_t *rguid, DDS_Security_ValidationResult_t result, const char * err_msg, |
| 38 |
const DDS_Security_DataHolder *token, void *instance) |
| 39 |
{ |
| 40 |
struct message *msg = ddsrt_malloc(sizeof(*msg)); |
| 41 |
memset(msg, 0, sizeof(*msg)); |
| 42 |
msg->kind = kind; |
| 43 |
msg->lidHandle = lidHandle; |
| 44 |
msg->ridHandle = ridHandle; |
| 45 |
msg->hsHandle = hsHandle; |
| 46 |
msg->result = result; |
| 47 |
msg->err_msg = ddsrt_strdup (err_msg ? err_msg : ""); |
| 48 |
if (lguid) |
| 49 |
memcpy(&msg->lguid, lguid, sizeof(msg->lguid)); |
| 50 |
if (rguid) |
| 51 |
memcpy(&msg->rguid, rguid, sizeof(msg->rguid)); |
| 52 |
if (token) |
| 53 |
DDS_Security_DataHolder_copy(&msg->token, token); |
| 54 |
msg->instance = instance; |
| 55 |
|
| 56 |
insert_message(queue, msg); |
| 57 |
} |
| 58 |
|
| 59 |
void delete_message(struct message *msg) |
| 60 |
{ |
| 61 |
if (msg) |
| 62 |
{ |
| 63 |
DDS_Security_DataHolder_deinit(&msg->token); |
| 64 |
ddsrt_free(msg->err_msg); |
| 65 |
ddsrt_free(msg); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
void init_message_queue(struct message_queue *queue) |
| 70 |
{ |
| 71 |
queue->head = NULL; |
| 72 |
queue->tail = NULL; |
| 73 |
ddsrt_mutex_init(&queue->lock); |
| 74 |
ddsrt_cond_init(&queue->cond); |
| 75 |
} |
| 76 |
|
| 77 |
void deinit_message_queue(struct message_queue *queue) |
| 78 |
{ |
| 79 |
struct message *msg = queue->head; |
| 80 |
while (msg) |
| 81 |
{ |
| 82 |
queue->head = msg->next; |
| 83 |
delete_message(msg); |
| 84 |
msg = queue->head; |
| 85 |
} |
| 86 |
ddsrt_cond_destroy(&queue->cond); |
| 87 |
ddsrt_mutex_destroy(&queue->lock); |
| 88 |
} |
| 89 |
|
| 90 |
int message_matched(struct message *msg, message_kind_t kind, DDS_Security_IdentityHandle lidHandle, DDS_Security_IdentityHandle ridHandle, DDS_Security_IdentityHandle hsHandle) |
| 91 |
{ |
| 92 |
return msg->kind == kind && |
| 93 |
(!lidHandle || msg->lidHandle == lidHandle) && |
| 94 |
(!ridHandle || msg->ridHandle == ridHandle) && |
| 95 |
(!hsHandle || msg->hsHandle == hsHandle); |
| 96 |
} |
| 97 |
|
| 98 |
enum take_message_result take_message(struct message_queue *queue, message_kind_t kind, DDS_Security_IdentityHandle lidHandle, DDS_Security_IdentityHandle ridHandle, DDS_Security_IdentityHandle hsHandle, dds_time_t abstimeout, struct message **msg) |
| 99 |
{ |
| 100 |
struct message *cur, *prev; |
| 101 |
enum take_message_result ret = TAKE_MESSAGE_OK; |
| 102 |
*msg = NULL; |
| 103 |
ddsrt_mutex_lock(&queue->lock); |
| 104 |
do |
| 105 |
{ |
| 106 |
cur = queue->head; |
| 107 |
prev = NULL; |
| 108 |
while (cur && *msg == NULL) |
| 109 |
{ |
| 110 |
if (message_matched(cur, kind, lidHandle, ridHandle, hsHandle)) |
| 111 |
{ |
| 112 |
*msg = cur; |
| 113 |
if (prev) |
| 114 |
prev->next = cur->next; |
| 115 |
else |
| 116 |
queue->head = cur->next; |
| 117 |
if (queue->tail == cur) |
| 118 |
queue->tail = prev; |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
prev = cur; |
| 123 |
cur = cur->next; |
| 124 |
} |
| 125 |
} |
| 126 |
if (*msg == NULL) |
| 127 |
{ |
| 128 |
if (!ddsrt_cond_waituntil(&queue->cond, &queue->lock, abstimeout)) |
| 129 |
ret = queue->head ? TAKE_MESSAGE_TIMEOUT_NONEMPTY : TAKE_MESSAGE_TIMEOUT_EMPTY; |
| 130 |
} |
| 131 |
} while (ret == TAKE_MESSAGE_OK && *msg == NULL); |
| 132 |
|
| 133 |
ddsrt_mutex_unlock(&queue->lock); |
| 134 |
return ret; |
| 135 |
} |