[331] | 1 | /*
|
---|
| 2 | * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the OpenSSL license (the "License"). You may not use
|
---|
| 5 | * this file except in compliance with the License. You can obtain a copy
|
---|
| 6 | * in the file LICENSE in the source distribution or at
|
---|
| 7 | * https://www.openssl.org/source/license.html
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | /* This must be the first #include file */
|
---|
| 11 | #include "async_locl.h"
|
---|
| 12 |
|
---|
| 13 | #include <openssl/err.h>
|
---|
| 14 |
|
---|
| 15 | ASYNC_WAIT_CTX *ASYNC_WAIT_CTX_new(void)
|
---|
| 16 | {
|
---|
| 17 | return OPENSSL_zalloc(sizeof(ASYNC_WAIT_CTX));
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | void ASYNC_WAIT_CTX_free(ASYNC_WAIT_CTX *ctx)
|
---|
| 21 | {
|
---|
| 22 | struct fd_lookup_st *curr;
|
---|
| 23 | struct fd_lookup_st *next;
|
---|
| 24 |
|
---|
| 25 | if (ctx == NULL)
|
---|
| 26 | return;
|
---|
| 27 |
|
---|
| 28 | curr = ctx->fds;
|
---|
| 29 | while (curr != NULL) {
|
---|
| 30 | if (!curr->del) {
|
---|
| 31 | /* Only try and cleanup if it hasn't been marked deleted */
|
---|
| 32 | if (curr->cleanup != NULL)
|
---|
| 33 | curr->cleanup(ctx, curr->key, curr->fd, curr->custom_data);
|
---|
| 34 | }
|
---|
| 35 | /* Always free the fd_lookup_st */
|
---|
| 36 | next = curr->next;
|
---|
| 37 | OPENSSL_free(curr);
|
---|
| 38 | curr = next;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | OPENSSL_free(ctx);
|
---|
| 42 | }
|
---|
| 43 | int ASYNC_WAIT_CTX_set_wait_fd(ASYNC_WAIT_CTX *ctx, const void *key,
|
---|
| 44 | OSSL_ASYNC_FD fd, void *custom_data,
|
---|
| 45 | void (*cleanup)(ASYNC_WAIT_CTX *, const void *,
|
---|
| 46 | OSSL_ASYNC_FD, void *))
|
---|
| 47 | {
|
---|
| 48 | struct fd_lookup_st *fdlookup;
|
---|
| 49 |
|
---|
| 50 | fdlookup = OPENSSL_zalloc(sizeof *fdlookup);
|
---|
| 51 | if (fdlookup == NULL)
|
---|
| 52 | return 0;
|
---|
| 53 |
|
---|
| 54 | fdlookup->key = key;
|
---|
| 55 | fdlookup->fd = fd;
|
---|
| 56 | fdlookup->custom_data = custom_data;
|
---|
| 57 | fdlookup->cleanup = cleanup;
|
---|
| 58 | fdlookup->add = 1;
|
---|
| 59 | fdlookup->next = ctx->fds;
|
---|
| 60 | ctx->fds = fdlookup;
|
---|
| 61 | ctx->numadd++;
|
---|
| 62 | return 1;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | int ASYNC_WAIT_CTX_get_fd(ASYNC_WAIT_CTX *ctx, const void *key,
|
---|
| 66 | OSSL_ASYNC_FD *fd, void **custom_data)
|
---|
| 67 | {
|
---|
| 68 | struct fd_lookup_st *curr;
|
---|
| 69 |
|
---|
| 70 | curr = ctx->fds;
|
---|
| 71 | while (curr != NULL) {
|
---|
| 72 | if (curr->del) {
|
---|
| 73 | /* This one has been marked deleted so do nothing */
|
---|
| 74 | curr = curr->next;
|
---|
| 75 | continue;
|
---|
| 76 | }
|
---|
| 77 | if (curr->key == key) {
|
---|
| 78 | *fd = curr->fd;
|
---|
| 79 | *custom_data = curr->custom_data;
|
---|
| 80 | return 1;
|
---|
| 81 | }
|
---|
| 82 | curr = curr->next;
|
---|
| 83 | }
|
---|
| 84 | return 0;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | int ASYNC_WAIT_CTX_get_all_fds(ASYNC_WAIT_CTX *ctx, OSSL_ASYNC_FD *fd,
|
---|
| 88 | size_t *numfds)
|
---|
| 89 | {
|
---|
| 90 | struct fd_lookup_st *curr;
|
---|
| 91 |
|
---|
| 92 | curr = ctx->fds;
|
---|
| 93 | *numfds = 0;
|
---|
| 94 | while (curr != NULL) {
|
---|
| 95 | if (curr->del) {
|
---|
| 96 | /* This one has been marked deleted so do nothing */
|
---|
| 97 | curr = curr->next;
|
---|
| 98 | continue;
|
---|
| 99 | }
|
---|
| 100 | if (fd != NULL) {
|
---|
| 101 | *fd = curr->fd;
|
---|
| 102 | fd++;
|
---|
| 103 | }
|
---|
| 104 | (*numfds)++;
|
---|
| 105 | curr = curr->next;
|
---|
| 106 | }
|
---|
| 107 | return 1;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | int ASYNC_WAIT_CTX_get_changed_fds(ASYNC_WAIT_CTX *ctx, OSSL_ASYNC_FD *addfd,
|
---|
| 111 | size_t *numaddfds, OSSL_ASYNC_FD *delfd,
|
---|
| 112 | size_t *numdelfds)
|
---|
| 113 | {
|
---|
| 114 | struct fd_lookup_st *curr;
|
---|
| 115 |
|
---|
| 116 | *numaddfds = ctx->numadd;
|
---|
| 117 | *numdelfds = ctx->numdel;
|
---|
| 118 | if (addfd == NULL && delfd == NULL)
|
---|
| 119 | return 1;
|
---|
| 120 |
|
---|
| 121 | curr = ctx->fds;
|
---|
| 122 |
|
---|
| 123 | while (curr != NULL) {
|
---|
| 124 | /* We ignore fds that have been marked as both added and deleted */
|
---|
| 125 | if (curr->del && !curr->add && (delfd != NULL)) {
|
---|
| 126 | *delfd = curr->fd;
|
---|
| 127 | delfd++;
|
---|
| 128 | }
|
---|
| 129 | if (curr->add && !curr->del && (addfd != NULL)) {
|
---|
| 130 | *addfd = curr->fd;
|
---|
| 131 | addfd++;
|
---|
| 132 | }
|
---|
| 133 | curr = curr->next;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | return 1;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | int ASYNC_WAIT_CTX_clear_fd(ASYNC_WAIT_CTX *ctx, const void *key)
|
---|
| 140 | {
|
---|
| 141 | struct fd_lookup_st *curr, *prev;
|
---|
| 142 |
|
---|
| 143 | curr = ctx->fds;
|
---|
| 144 | prev = NULL;
|
---|
| 145 | while (curr != NULL) {
|
---|
| 146 | if (curr->del == 1) {
|
---|
| 147 | /* This one has been marked deleted already so do nothing */
|
---|
| 148 | curr = curr->next;
|
---|
| 149 | continue;
|
---|
| 150 | }
|
---|
| 151 | if (curr->key == key) {
|
---|
| 152 | /* If fd has just been added, remove it from the list */
|
---|
| 153 | if (curr->add == 1) {
|
---|
| 154 | if (ctx->fds == curr) {
|
---|
| 155 | ctx->fds = curr->next;
|
---|
| 156 | } else {
|
---|
| 157 | prev->next = curr->next;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | /* It is responsibility of the caller to cleanup before calling
|
---|
| 161 | * ASYNC_WAIT_CTX_clear_fd
|
---|
| 162 | */
|
---|
| 163 | OPENSSL_free(curr);
|
---|
| 164 | ctx->numadd--;
|
---|
| 165 | return 1;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | /*
|
---|
| 169 | * Mark it as deleted. We don't call cleanup if explicitly asked
|
---|
| 170 | * to clear an fd. We assume the caller is going to do that (if
|
---|
| 171 | * appropriate).
|
---|
| 172 | */
|
---|
| 173 | curr->del = 1;
|
---|
| 174 | ctx->numdel++;
|
---|
| 175 | return 1;
|
---|
| 176 | }
|
---|
| 177 | prev = curr;
|
---|
| 178 | curr = curr->next;
|
---|
| 179 | }
|
---|
| 180 | return 0;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | void async_wait_ctx_reset_counts(ASYNC_WAIT_CTX *ctx)
|
---|
| 184 | {
|
---|
| 185 | struct fd_lookup_st *curr, *prev = NULL;
|
---|
| 186 |
|
---|
| 187 | ctx->numadd = 0;
|
---|
| 188 | ctx->numdel = 0;
|
---|
| 189 |
|
---|
| 190 | curr = ctx->fds;
|
---|
| 191 |
|
---|
| 192 | while (curr != NULL) {
|
---|
| 193 | if (curr->del) {
|
---|
| 194 | if (prev == NULL)
|
---|
| 195 | ctx->fds = curr->next;
|
---|
| 196 | else
|
---|
| 197 | prev->next = curr->next;
|
---|
| 198 | OPENSSL_free(curr);
|
---|
| 199 | if (prev == NULL)
|
---|
| 200 | curr = ctx->fds;
|
---|
| 201 | else
|
---|
| 202 | curr = prev->next;
|
---|
| 203 | continue;
|
---|
| 204 | }
|
---|
| 205 | if (curr->add) {
|
---|
| 206 | curr->add = 0;
|
---|
| 207 | }
|
---|
| 208 | prev = curr;
|
---|
| 209 | curr = curr->next;
|
---|
| 210 | }
|
---|
| 211 | }
|
---|