1 /* Copyright (c) 2014, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include "async_bio.h"
16 
17 #include <errno.h>
18 #include <string.h>
19 
20 #include <openssl/mem.h>
21 
22 
23 namespace {
24 
25 extern const BIO_METHOD g_async_bio_method;
26 
27 struct AsyncBio {
28   bool datagram;
29   size_t read_quota;
30   size_t write_quota;
31 };
32 
GetData(BIO * bio)33 AsyncBio *GetData(BIO *bio) {
34   if (bio->method != &g_async_bio_method) {
35     return NULL;
36   }
37   return (AsyncBio *)bio->ptr;
38 }
39 
AsyncWrite(BIO * bio,const char * in,int inl)40 static int AsyncWrite(BIO *bio, const char *in, int inl) {
41   AsyncBio *a = GetData(bio);
42   if (a == NULL || bio->next_bio == NULL) {
43     return 0;
44   }
45 
46   if (a->datagram) {
47     // Perform writes synchronously; the DTLS implementation drops any packets
48     // that failed to send.
49     return BIO_write(bio->next_bio, in, inl);
50   }
51 
52   BIO_clear_retry_flags(bio);
53 
54   if (a->write_quota == 0) {
55     BIO_set_retry_write(bio);
56     errno = EAGAIN;
57     return -1;
58   }
59 
60   if (!a->datagram && (size_t)inl > a->write_quota) {
61     inl = a->write_quota;
62   }
63   int ret = BIO_write(bio->next_bio, in, inl);
64   if (ret <= 0) {
65     BIO_copy_next_retry(bio);
66   } else {
67     a->write_quota -= (a->datagram ? 1 : ret);
68   }
69   return ret;
70 }
71 
AsyncRead(BIO * bio,char * out,int outl)72 static int AsyncRead(BIO *bio, char *out, int outl) {
73   AsyncBio *a = GetData(bio);
74   if (a == NULL || bio->next_bio == NULL) {
75     return 0;
76   }
77 
78   BIO_clear_retry_flags(bio);
79 
80   if (a->read_quota == 0) {
81     BIO_set_retry_read(bio);
82     errno = EAGAIN;
83     return -1;
84   }
85 
86   if (!a->datagram && (size_t)outl > a->read_quota) {
87     outl = a->read_quota;
88   }
89   int ret = BIO_read(bio->next_bio, out, outl);
90   if (ret <= 0) {
91     BIO_copy_next_retry(bio);
92   } else {
93     a->read_quota -= (a->datagram ? 1 : ret);
94   }
95   return ret;
96 }
97 
AsyncCtrl(BIO * bio,int cmd,long num,void * ptr)98 static long AsyncCtrl(BIO *bio, int cmd, long num, void *ptr) {
99   if (bio->next_bio == NULL) {
100     return 0;
101   }
102   BIO_clear_retry_flags(bio);
103   int ret = BIO_ctrl(bio->next_bio, cmd, num, ptr);
104   BIO_copy_next_retry(bio);
105   return ret;
106 }
107 
AsyncNew(BIO * bio)108 static int AsyncNew(BIO *bio) {
109   AsyncBio *a = (AsyncBio *)OPENSSL_malloc(sizeof(*a));
110   if (a == NULL) {
111     return 0;
112   }
113   memset(a, 0, sizeof(*a));
114   bio->init = 1;
115   bio->ptr = (char *)a;
116   return 1;
117 }
118 
AsyncFree(BIO * bio)119 static int AsyncFree(BIO *bio) {
120   if (bio == NULL) {
121     return 0;
122   }
123 
124   OPENSSL_free(bio->ptr);
125   bio->ptr = NULL;
126   bio->init = 0;
127   bio->flags = 0;
128   return 1;
129 }
130 
AsyncCallbackCtrl(BIO * bio,int cmd,bio_info_cb fp)131 static long AsyncCallbackCtrl(BIO *bio, int cmd, bio_info_cb fp) {
132   if (bio->next_bio == NULL) {
133     return 0;
134   }
135   return BIO_callback_ctrl(bio->next_bio, cmd, fp);
136 }
137 
138 const BIO_METHOD g_async_bio_method = {
139   BIO_TYPE_FILTER,
140   "async bio",
141   AsyncWrite,
142   AsyncRead,
143   NULL /* puts */,
144   NULL /* gets */,
145   AsyncCtrl,
146   AsyncNew,
147   AsyncFree,
148   AsyncCallbackCtrl,
149 };
150 
151 }  // namespace
152 
AsyncBioCreate()153 ScopedBIO AsyncBioCreate() {
154   return ScopedBIO(BIO_new(&g_async_bio_method));
155 }
156 
AsyncBioCreateDatagram()157 ScopedBIO AsyncBioCreateDatagram() {
158   ScopedBIO ret(BIO_new(&g_async_bio_method));
159   if (!ret) {
160     return nullptr;
161   }
162   GetData(ret.get())->datagram = true;
163   return ret;
164 }
165 
AsyncBioAllowRead(BIO * bio,size_t count)166 void AsyncBioAllowRead(BIO *bio, size_t count) {
167   AsyncBio *a = GetData(bio);
168   if (a == NULL) {
169     return;
170   }
171   a->read_quota += count;
172 }
173 
AsyncBioAllowWrite(BIO * bio,size_t count)174 void AsyncBioAllowWrite(BIO *bio, size_t count) {
175   AsyncBio *a = GetData(bio);
176   if (a == NULL) {
177     return;
178   }
179   a->write_quota += count;
180 }
181