2 * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
4 /* ====================================================================
5 * Copyright (c) 2016 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * licensing@OpenSSL.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
53 /* This must be the first #include file */
54 #include "async_locl.h"
56 #include <openssl/err.h>
58 ASYNC_WAIT_CTX *ASYNC_WAIT_CTX_new(void)
60 return OPENSSL_zalloc(sizeof(ASYNC_WAIT_CTX));
63 void ASYNC_WAIT_CTX_free(ASYNC_WAIT_CTX *ctx)
65 struct fd_lookup_st *curr;
66 struct fd_lookup_st *next;
72 while (curr != NULL) {
74 /* Only try and cleanup if it hasn't been marked deleted */
75 if (curr->cleanup != NULL)
76 curr->cleanup(ctx, curr->key, curr->fd, curr->custom_data);
78 /* Always free the fd_lookup_st */
86 int ASYNC_WAIT_CTX_set_wait_fd(ASYNC_WAIT_CTX *ctx, const void *key,
87 OSSL_ASYNC_FD fd, void *custom_data,
88 void (*cleanup)(ASYNC_WAIT_CTX *, const void *,
89 OSSL_ASYNC_FD, void *))
91 struct fd_lookup_st *fdlookup;
93 fdlookup = OPENSSL_zalloc(sizeof *fdlookup);
99 fdlookup->custom_data = custom_data;
100 fdlookup->cleanup = cleanup;
102 fdlookup->next = ctx->fds;
108 int ASYNC_WAIT_CTX_get_fd(ASYNC_WAIT_CTX *ctx, const void *key,
109 OSSL_ASYNC_FD *fd, void **custom_data)
111 struct fd_lookup_st *curr;
114 while (curr != NULL) {
116 /* This one has been marked deleted so do nothing */
120 if (curr->key == key) {
122 *custom_data = curr->custom_data;
130 int ASYNC_WAIT_CTX_get_all_fds(ASYNC_WAIT_CTX *ctx, OSSL_ASYNC_FD *fd,
133 struct fd_lookup_st *curr;
137 while (curr != NULL) {
139 /* This one has been marked deleted so do nothing */
153 int ASYNC_WAIT_CTX_get_changed_fds(ASYNC_WAIT_CTX *ctx, OSSL_ASYNC_FD *addfd,
154 size_t *numaddfds, OSSL_ASYNC_FD *delfd,
157 struct fd_lookup_st *curr;
159 *numaddfds = ctx->numadd;
160 *numdelfds = ctx->numdel;
161 if (addfd == NULL && delfd == NULL)
166 while (curr != NULL) {
167 /* We ignore fds that have been marked as both added and deleted */
168 if (curr->del && !curr->add && (delfd != NULL)) {
172 if (curr->add && !curr->del && (addfd != NULL)) {
182 int ASYNC_WAIT_CTX_clear_fd(ASYNC_WAIT_CTX *ctx, const void *key)
184 struct fd_lookup_st *curr;
187 while (curr != NULL) {
189 /* This one has been marked deleted already so do nothing */
193 if (curr->key == key) {
195 * Mark it as deleted. We don't call cleanup if explicitly asked
196 * to clear an fd. We assume the caller is going to do that (if
208 void async_wait_ctx_reset_counts(ASYNC_WAIT_CTX *ctx)
210 struct fd_lookup_st *curr, *prev = NULL;
217 while (curr != NULL) {
220 ctx->fds = curr->next;
222 prev->next = curr->next;