diff options
Diffstat (limited to 'src/lib/libcrypto/bio/bss_fd.c')
-rw-r--r-- | src/lib/libcrypto/bio/bss_fd.c | 226 |
1 files changed, 223 insertions, 3 deletions
diff --git a/src/lib/libcrypto/bio/bss_fd.c b/src/lib/libcrypto/bio/bss_fd.c index 686c4909a2..5e3e187de6 100644 --- a/src/lib/libcrypto/bio/bss_fd.c +++ b/src/lib/libcrypto/bio/bss_fd.c | |||
@@ -56,7 +56,227 @@ | |||
56 | * [including the GNU Public Licence.] | 56 | * [including the GNU Public Licence.] |
57 | */ | 57 | */ |
58 | 58 | ||
59 | #define BIO_FD | 59 | #include <stdio.h> |
60 | #include "bss_sock.c" | 60 | #include <errno.h> |
61 | #undef BIO_FD | 61 | #define USE_SOCKETS |
62 | #include "cryptlib.h" | ||
63 | #include <openssl/bio.h> | ||
62 | 64 | ||
65 | static int fd_write(BIO *h, const char *buf, int num); | ||
66 | static int fd_read(BIO *h, char *buf, int size); | ||
67 | static int fd_puts(BIO *h, const char *str); | ||
68 | static long fd_ctrl(BIO *h, int cmd, long arg1, void *arg2); | ||
69 | static int fd_new(BIO *h); | ||
70 | static int fd_free(BIO *data); | ||
71 | int BIO_fd_should_retry(int s); | ||
72 | |||
73 | static BIO_METHOD methods_fdp= | ||
74 | { | ||
75 | BIO_TYPE_FD,"file descriptor", | ||
76 | fd_write, | ||
77 | fd_read, | ||
78 | fd_puts, | ||
79 | NULL, /* fd_gets, */ | ||
80 | fd_ctrl, | ||
81 | fd_new, | ||
82 | fd_free, | ||
83 | NULL, | ||
84 | }; | ||
85 | |||
86 | BIO_METHOD *BIO_s_fd(void) | ||
87 | { | ||
88 | return(&methods_fdp); | ||
89 | } | ||
90 | |||
91 | BIO *BIO_new_fd(int fd,int close_flag) | ||
92 | { | ||
93 | BIO *ret; | ||
94 | ret=BIO_new(BIO_s_fd()); | ||
95 | if (ret == NULL) return(NULL); | ||
96 | BIO_set_fd(ret,fd,close_flag); | ||
97 | return(ret); | ||
98 | } | ||
99 | |||
100 | static int fd_new(BIO *bi) | ||
101 | { | ||
102 | bi->init=0; | ||
103 | bi->num=0; | ||
104 | bi->ptr=NULL; | ||
105 | bi->flags=0; | ||
106 | return(1); | ||
107 | } | ||
108 | |||
109 | static int fd_free(BIO *a) | ||
110 | { | ||
111 | if (a == NULL) return(0); | ||
112 | if (a->shutdown) | ||
113 | { | ||
114 | if (a->init) | ||
115 | { | ||
116 | close(a->num); | ||
117 | } | ||
118 | a->init=0; | ||
119 | a->flags=0; | ||
120 | } | ||
121 | return(1); | ||
122 | } | ||
123 | |||
124 | static int fd_read(BIO *b, char *out,int outl) | ||
125 | { | ||
126 | int ret=0; | ||
127 | |||
128 | if (out != NULL) | ||
129 | { | ||
130 | clear_sys_error(); | ||
131 | ret=read(b->num,out,outl); | ||
132 | BIO_clear_retry_flags(b); | ||
133 | if (ret <= 0) | ||
134 | { | ||
135 | if (BIO_fd_should_retry(ret)) | ||
136 | BIO_set_retry_read(b); | ||
137 | } | ||
138 | } | ||
139 | return(ret); | ||
140 | } | ||
141 | |||
142 | static int fd_write(BIO *b, const char *in, int inl) | ||
143 | { | ||
144 | int ret; | ||
145 | clear_sys_error(); | ||
146 | ret=write(b->num,in,inl); | ||
147 | BIO_clear_retry_flags(b); | ||
148 | if (ret <= 0) | ||
149 | { | ||
150 | if (BIO_fd_should_retry(ret)) | ||
151 | BIO_set_retry_write(b); | ||
152 | } | ||
153 | return(ret); | ||
154 | } | ||
155 | |||
156 | static long fd_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
157 | { | ||
158 | long ret=1; | ||
159 | int *ip; | ||
160 | |||
161 | switch (cmd) | ||
162 | { | ||
163 | case BIO_CTRL_RESET: | ||
164 | num=0; | ||
165 | case BIO_C_FILE_SEEK: | ||
166 | ret=(long)lseek(b->num,num,0); | ||
167 | break; | ||
168 | case BIO_C_FILE_TELL: | ||
169 | case BIO_CTRL_INFO: | ||
170 | ret=(long)lseek(b->num,0,1); | ||
171 | break; | ||
172 | case BIO_C_SET_FD: | ||
173 | fd_free(b); | ||
174 | b->num= *((int *)ptr); | ||
175 | b->shutdown=(int)num; | ||
176 | b->init=1; | ||
177 | break; | ||
178 | case BIO_C_GET_FD: | ||
179 | if (b->init) | ||
180 | { | ||
181 | ip=(int *)ptr; | ||
182 | if (ip != NULL) *ip=b->num; | ||
183 | ret=b->num; | ||
184 | } | ||
185 | else | ||
186 | ret= -1; | ||
187 | break; | ||
188 | case BIO_CTRL_GET_CLOSE: | ||
189 | ret=b->shutdown; | ||
190 | break; | ||
191 | case BIO_CTRL_SET_CLOSE: | ||
192 | b->shutdown=(int)num; | ||
193 | break; | ||
194 | case BIO_CTRL_PENDING: | ||
195 | case BIO_CTRL_WPENDING: | ||
196 | ret=0; | ||
197 | break; | ||
198 | case BIO_CTRL_DUP: | ||
199 | case BIO_CTRL_FLUSH: | ||
200 | ret=1; | ||
201 | break; | ||
202 | default: | ||
203 | ret=0; | ||
204 | break; | ||
205 | } | ||
206 | return(ret); | ||
207 | } | ||
208 | |||
209 | static int fd_puts(BIO *bp, const char *str) | ||
210 | { | ||
211 | int n,ret; | ||
212 | |||
213 | n=strlen(str); | ||
214 | ret=fd_write(bp,str,n); | ||
215 | return(ret); | ||
216 | } | ||
217 | |||
218 | int BIO_fd_should_retry(int i) | ||
219 | { | ||
220 | int err; | ||
221 | |||
222 | if ((i == 0) || (i == -1)) | ||
223 | { | ||
224 | err=get_last_sys_error(); | ||
225 | |||
226 | #if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */ | ||
227 | if ((i == -1) && (err == 0)) | ||
228 | return(1); | ||
229 | #endif | ||
230 | |||
231 | return(BIO_fd_non_fatal_error(err)); | ||
232 | } | ||
233 | return(0); | ||
234 | } | ||
235 | |||
236 | int BIO_fd_non_fatal_error(int err) | ||
237 | { | ||
238 | switch (err) | ||
239 | { | ||
240 | |||
241 | #ifdef EWOULDBLOCK | ||
242 | # ifdef WSAEWOULDBLOCK | ||
243 | # if WSAEWOULDBLOCK != EWOULDBLOCK | ||
244 | case EWOULDBLOCK: | ||
245 | # endif | ||
246 | # else | ||
247 | case EWOULDBLOCK: | ||
248 | # endif | ||
249 | #endif | ||
250 | |||
251 | #if defined(ENOTCONN) | ||
252 | case ENOTCONN: | ||
253 | #endif | ||
254 | |||
255 | #ifdef EINTR | ||
256 | case EINTR: | ||
257 | #endif | ||
258 | |||
259 | #ifdef EAGAIN | ||
260 | #if EWOULDBLOCK != EAGAIN | ||
261 | case EAGAIN: | ||
262 | # endif | ||
263 | #endif | ||
264 | |||
265 | #ifdef EPROTO | ||
266 | case EPROTO: | ||
267 | #endif | ||
268 | |||
269 | #ifdef EINPROGRESS | ||
270 | case EINPROGRESS: | ||
271 | #endif | ||
272 | |||
273 | #ifdef EALREADY | ||
274 | case EALREADY: | ||
275 | #endif | ||
276 | return(1); | ||
277 | /* break; */ | ||
278 | default: | ||
279 | break; | ||
280 | } | ||
281 | return(0); | ||
282 | } | ||