/src/openssl/crypto/async/async_wait.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  *  | 
4  |  |  * Licensed under the Apache License 2.0 (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_local.h"  | 
12  |  |  | 
13  |  | #include <openssl/err.h>  | 
14  |  |  | 
15  |  | ASYNC_WAIT_CTX *ASYNC_WAIT_CTX_new(void)  | 
16  | 0  | { | 
17  | 0  |     return OPENSSL_zalloc(sizeof(ASYNC_WAIT_CTX));  | 
18  | 0  | }  | 
19  |  |  | 
20  |  | void ASYNC_WAIT_CTX_free(ASYNC_WAIT_CTX *ctx)  | 
21  | 0  | { | 
22  | 0  |     struct fd_lookup_st *curr;  | 
23  | 0  |     struct fd_lookup_st *next;  | 
24  |  | 
  | 
25  | 0  |     if (ctx == NULL)  | 
26  | 0  |         return;  | 
27  |  |  | 
28  | 0  |     curr = ctx->fds;  | 
29  | 0  |     while (curr != NULL) { | 
30  | 0  |         if (!curr->del) { | 
31  |  |             /* Only try and cleanup if it hasn't been marked deleted */  | 
32  | 0  |             if (curr->cleanup != NULL)  | 
33  | 0  |                 curr->cleanup(ctx, curr->key, curr->fd, curr->custom_data);  | 
34  | 0  |         }  | 
35  |  |         /* Always free the fd_lookup_st */  | 
36  | 0  |         next = curr->next;  | 
37  | 0  |         OPENSSL_free(curr);  | 
38  | 0  |         curr = next;  | 
39  | 0  |     }  | 
40  |  | 
  | 
41  | 0  |     OPENSSL_free(ctx);  | 
42  | 0  | }  | 
43  |  |  | 
44  |  | int ASYNC_WAIT_CTX_set_wait_fd(ASYNC_WAIT_CTX *ctx, const void *key,  | 
45  |  |                                OSSL_ASYNC_FD fd, void *custom_data,  | 
46  |  |                                void (*cleanup)(ASYNC_WAIT_CTX *, const void *,  | 
47  |  |                                                OSSL_ASYNC_FD, void *))  | 
48  | 0  | { | 
49  | 0  |     struct fd_lookup_st *fdlookup;  | 
50  |  | 
  | 
51  | 0  |     if ((fdlookup = OPENSSL_zalloc(sizeof(*fdlookup))) == NULL)  | 
52  | 0  |         return 0;  | 
53  |  |  | 
54  | 0  |     fdlookup->key = key;  | 
55  | 0  |     fdlookup->fd = fd;  | 
56  | 0  |     fdlookup->custom_data = custom_data;  | 
57  | 0  |     fdlookup->cleanup = cleanup;  | 
58  | 0  |     fdlookup->add = 1;  | 
59  | 0  |     fdlookup->next = ctx->fds;  | 
60  | 0  |     ctx->fds = fdlookup;  | 
61  | 0  |     ctx->numadd++;  | 
62  | 0  |     return 1;  | 
63  | 0  | }  | 
64  |  |  | 
65  |  | int ASYNC_WAIT_CTX_get_fd(ASYNC_WAIT_CTX *ctx, const void *key,  | 
66  |  |                           OSSL_ASYNC_FD *fd, void **custom_data)  | 
67  | 0  | { | 
68  | 0  |     struct fd_lookup_st *curr;  | 
69  |  | 
  | 
70  | 0  |     curr = ctx->fds;  | 
71  | 0  |     while (curr != NULL) { | 
72  | 0  |         if (curr->del) { | 
73  |  |             /* This one has been marked deleted so do nothing */  | 
74  | 0  |             curr = curr->next;  | 
75  | 0  |             continue;  | 
76  | 0  |         }  | 
77  | 0  |         if (curr->key == key) { | 
78  | 0  |             *fd = curr->fd;  | 
79  | 0  |             *custom_data = curr->custom_data;  | 
80  | 0  |             return 1;  | 
81  | 0  |         }  | 
82  | 0  |         curr = curr->next;  | 
83  | 0  |     }  | 
84  | 0  |     return 0;  | 
85  | 0  | }  | 
86  |  |  | 
87  |  | int ASYNC_WAIT_CTX_get_all_fds(ASYNC_WAIT_CTX *ctx, OSSL_ASYNC_FD *fd,  | 
88  |  |                                size_t *numfds)  | 
89  | 0  | { | 
90  | 0  |     struct fd_lookup_st *curr;  | 
91  |  | 
  | 
92  | 0  |     curr = ctx->fds;  | 
93  | 0  |     *numfds = 0;  | 
94  | 0  |     while (curr != NULL) { | 
95  | 0  |         if (curr->del) { | 
96  |  |             /* This one has been marked deleted so do nothing */  | 
97  | 0  |             curr = curr->next;  | 
98  | 0  |             continue;  | 
99  | 0  |         }  | 
100  | 0  |         if (fd != NULL) { | 
101  | 0  |             *fd = curr->fd;  | 
102  | 0  |             fd++;  | 
103  | 0  |         }  | 
104  | 0  |         (*numfds)++;  | 
105  | 0  |         curr = curr->next;  | 
106  | 0  |     }  | 
107  | 0  |     return 1;  | 
108  | 0  | }  | 
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  | 0  | { | 
114  | 0  |     struct fd_lookup_st *curr;  | 
115  |  | 
  | 
116  | 0  |     *numaddfds = ctx->numadd;  | 
117  | 0  |     *numdelfds = ctx->numdel;  | 
118  | 0  |     if (addfd == NULL && delfd == NULL)  | 
119  | 0  |         return 1;  | 
120  |  |  | 
121  | 0  |     curr = ctx->fds;  | 
122  |  | 
  | 
123  | 0  |     while (curr != NULL) { | 
124  |  |         /* We ignore fds that have been marked as both added and deleted */  | 
125  | 0  |         if (curr->del && !curr->add && (delfd != NULL)) { | 
126  | 0  |             *delfd = curr->fd;  | 
127  | 0  |             delfd++;  | 
128  | 0  |         }  | 
129  | 0  |         if (curr->add && !curr->del && (addfd != NULL)) { | 
130  | 0  |             *addfd = curr->fd;  | 
131  | 0  |             addfd++;  | 
132  | 0  |         }  | 
133  | 0  |         curr = curr->next;  | 
134  | 0  |     }  | 
135  |  | 
  | 
136  | 0  |     return 1;  | 
137  | 0  | }  | 
138  |  |  | 
139  |  | int ASYNC_WAIT_CTX_clear_fd(ASYNC_WAIT_CTX *ctx, const void *key)  | 
140  | 0  | { | 
141  | 0  |     struct fd_lookup_st *curr, *prev;  | 
142  |  | 
  | 
143  | 0  |     curr = ctx->fds;  | 
144  | 0  |     prev = NULL;  | 
145  | 0  |     while (curr != NULL) { | 
146  | 0  |         if (curr->del == 1) { | 
147  |  |             /* This one has been marked deleted already so do nothing */  | 
148  | 0  |             prev = curr;  | 
149  | 0  |             curr = curr->next;  | 
150  | 0  |             continue;  | 
151  | 0  |         }  | 
152  | 0  |         if (curr->key == key) { | 
153  |  |             /* If fd has just been added, remove it from the list */  | 
154  | 0  |             if (curr->add == 1) { | 
155  | 0  |                 if (ctx->fds == curr) { | 
156  | 0  |                     ctx->fds = curr->next;  | 
157  | 0  |                 } else { | 
158  | 0  |                     prev->next = curr->next;  | 
159  | 0  |                 }  | 
160  |  |  | 
161  |  |                 /* It is responsibility of the caller to cleanup before calling  | 
162  |  |                  * ASYNC_WAIT_CTX_clear_fd  | 
163  |  |                  */  | 
164  | 0  |                 OPENSSL_free(curr);  | 
165  | 0  |                 ctx->numadd--;  | 
166  | 0  |                 return 1;  | 
167  | 0  |             }  | 
168  |  |  | 
169  |  |             /*  | 
170  |  |              * Mark it as deleted. We don't call cleanup if explicitly asked  | 
171  |  |              * to clear an fd. We assume the caller is going to do that (if  | 
172  |  |              * appropriate).  | 
173  |  |              */  | 
174  | 0  |             curr->del = 1;  | 
175  | 0  |             ctx->numdel++;  | 
176  | 0  |             return 1;  | 
177  | 0  |         }  | 
178  | 0  |         prev = curr;  | 
179  | 0  |         curr = curr->next;  | 
180  | 0  |     }  | 
181  | 0  |     return 0;  | 
182  | 0  | }  | 
183  |  |  | 
184  |  | int ASYNC_WAIT_CTX_set_callback(ASYNC_WAIT_CTX *ctx,  | 
185  |  |                                 ASYNC_callback_fn callback,  | 
186  |  |                                 void *callback_arg)  | 
187  | 0  | { | 
188  | 0  |       if (ctx == NULL)  | 
189  | 0  |           return 0;  | 
190  |  |  | 
191  | 0  |       ctx->callback = callback;  | 
192  | 0  |       ctx->callback_arg = callback_arg;  | 
193  | 0  |       return 1;  | 
194  | 0  | }  | 
195  |  |  | 
196  |  | int ASYNC_WAIT_CTX_get_callback(ASYNC_WAIT_CTX *ctx,  | 
197  |  |                                 ASYNC_callback_fn *callback,  | 
198  |  |                                 void **callback_arg)  | 
199  | 0  | { | 
200  | 0  |       if (ctx->callback == NULL)  | 
201  | 0  |           return 0;  | 
202  |  |  | 
203  | 0  |       *callback = ctx->callback;  | 
204  | 0  |       *callback_arg = ctx->callback_arg;  | 
205  | 0  |       return 1;  | 
206  | 0  | }  | 
207  |  |  | 
208  |  | int ASYNC_WAIT_CTX_set_status(ASYNC_WAIT_CTX *ctx, int status)  | 
209  | 0  | { | 
210  | 0  |       ctx->status = status;  | 
211  | 0  |       return 1;  | 
212  | 0  | }  | 
213  |  |  | 
214  |  | int ASYNC_WAIT_CTX_get_status(ASYNC_WAIT_CTX *ctx)  | 
215  | 0  | { | 
216  | 0  |       return ctx->status;  | 
217  | 0  | }  | 
218  |  |  | 
219  |  | void async_wait_ctx_reset_counts(ASYNC_WAIT_CTX *ctx)  | 
220  | 0  | { | 
221  | 0  |     struct fd_lookup_st *curr, *prev = NULL;  | 
222  |  | 
  | 
223  | 0  |     ctx->numadd = 0;  | 
224  | 0  |     ctx->numdel = 0;  | 
225  |  | 
  | 
226  | 0  |     curr = ctx->fds;  | 
227  |  | 
  | 
228  | 0  |     while (curr != NULL) { | 
229  | 0  |         if (curr->del) { | 
230  | 0  |             if (prev == NULL)  | 
231  | 0  |                 ctx->fds = curr->next;  | 
232  | 0  |             else  | 
233  | 0  |                 prev->next = curr->next;  | 
234  | 0  |             OPENSSL_free(curr);  | 
235  | 0  |             if (prev == NULL)  | 
236  | 0  |                 curr = ctx->fds;  | 
237  | 0  |             else  | 
238  | 0  |                 curr = prev->next;  | 
239  | 0  |             continue;  | 
240  | 0  |         }  | 
241  | 0  |         if (curr->add) { | 
242  | 0  |             curr->add = 0;  | 
243  | 0  |         }  | 
244  | 0  |         prev = curr;  | 
245  | 0  |         curr = curr->next;  | 
246  | 0  |     }  | 
247  | 0  | }  |