Make BIO_METHOD struct definitions consistent
[oweals/openssl.git] / crypto / bio / bss_fd.c
1 /*
2  * Copyright 1995-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 #include <stdio.h>
11 #include <errno.h>
12
13 #include "bio_lcl.h"
14
15 #if defined(OPENSSL_NO_POSIX_IO)
16 /*
17  * Dummy placeholder for BIO_s_fd...
18  */
19 BIO *BIO_new_fd(int fd, int close_flag)
20 {
21     return NULL;
22 }
23
24 int BIO_fd_non_fatal_error(int err)
25 {
26     return 0;
27 }
28
29 int BIO_fd_should_retry(int i)
30 {
31     return 0;
32 }
33
34 const BIO_METHOD *BIO_s_fd(void)
35 {
36     return NULL;
37 }
38 #else
39 /*
40  * As for unconditional usage of "UPLINK" interface in this module.
41  * Trouble is that unlike Unix file descriptors [which are indexes
42  * in kernel-side per-process table], corresponding descriptors on
43  * platforms which require "UPLINK" interface seem to be indexes
44  * in a user-land, non-global table. Well, in fact they are indexes
45  * in stdio _iob[], and recall that _iob[] was the very reason why
46  * "UPLINK" interface was introduced in first place. But one way on
47  * another. Neither libcrypto or libssl use this BIO meaning that
48  * file descriptors can only be provided by application. Therefore
49  * "UPLINK" calls are due...
50  */
51 static int fd_write(BIO *h, const char *buf, int num);
52 static int fd_read(BIO *h, char *buf, int size);
53 static int fd_puts(BIO *h, const char *str);
54 static int fd_gets(BIO *h, char *buf, int size);
55 static long fd_ctrl(BIO *h, int cmd, long arg1, void *arg2);
56 static int fd_new(BIO *h);
57 static int fd_free(BIO *data);
58 int BIO_fd_should_retry(int s);
59
60 static const BIO_METHOD methods_fdp = {
61     BIO_TYPE_FD,
62     "file descriptor",
63     fd_write,
64     fd_read,
65     fd_puts,
66     fd_gets,
67     fd_ctrl,
68     fd_new,
69     fd_free,
70     NULL,
71 };
72
73 const BIO_METHOD *BIO_s_fd(void)
74 {
75     return (&methods_fdp);
76 }
77
78 BIO *BIO_new_fd(int fd, int close_flag)
79 {
80     BIO *ret;
81     ret = BIO_new(BIO_s_fd());
82     if (ret == NULL)
83         return (NULL);
84     BIO_set_fd(ret, fd, close_flag);
85     return (ret);
86 }
87
88 static int fd_new(BIO *bi)
89 {
90     bi->init = 0;
91     bi->num = -1;
92     bi->ptr = NULL;
93     bi->flags = BIO_FLAGS_UPLINK; /* essentially redundant */
94     return (1);
95 }
96
97 static int fd_free(BIO *a)
98 {
99     if (a == NULL)
100         return (0);
101     if (a->shutdown) {
102         if (a->init) {
103             UP_close(a->num);
104         }
105         a->init = 0;
106         a->flags = BIO_FLAGS_UPLINK;
107     }
108     return (1);
109 }
110
111 static int fd_read(BIO *b, char *out, int outl)
112 {
113     int ret = 0;
114
115     if (out != NULL) {
116         clear_sys_error();
117         ret = UP_read(b->num, out, outl);
118         BIO_clear_retry_flags(b);
119         if (ret <= 0) {
120             if (BIO_fd_should_retry(ret))
121                 BIO_set_retry_read(b);
122         }
123     }
124     return (ret);
125 }
126
127 static int fd_write(BIO *b, const char *in, int inl)
128 {
129     int ret;
130     clear_sys_error();
131     ret = UP_write(b->num, in, inl);
132     BIO_clear_retry_flags(b);
133     if (ret <= 0) {
134         if (BIO_fd_should_retry(ret))
135             BIO_set_retry_write(b);
136     }
137     return (ret);
138 }
139
140 static long fd_ctrl(BIO *b, int cmd, long num, void *ptr)
141 {
142     long ret = 1;
143     int *ip;
144
145     switch (cmd) {
146     case BIO_CTRL_RESET:
147         num = 0;
148         /* fall thru */
149     case BIO_C_FILE_SEEK:
150         ret = (long)UP_lseek(b->num, num, 0);
151         break;
152     case BIO_C_FILE_TELL:
153     case BIO_CTRL_INFO:
154         ret = (long)UP_lseek(b->num, 0, 1);
155         break;
156     case BIO_C_SET_FD:
157         fd_free(b);
158         b->num = *((int *)ptr);
159         b->shutdown = (int)num;
160         b->init = 1;
161         break;
162     case BIO_C_GET_FD:
163         if (b->init) {
164             ip = (int *)ptr;
165             if (ip != NULL)
166                 *ip = b->num;
167             ret = b->num;
168         } else
169             ret = -1;
170         break;
171     case BIO_CTRL_GET_CLOSE:
172         ret = b->shutdown;
173         break;
174     case BIO_CTRL_SET_CLOSE:
175         b->shutdown = (int)num;
176         break;
177     case BIO_CTRL_PENDING:
178     case BIO_CTRL_WPENDING:
179         ret = 0;
180         break;
181     case BIO_CTRL_DUP:
182     case BIO_CTRL_FLUSH:
183         ret = 1;
184         break;
185     default:
186         ret = 0;
187         break;
188     }
189     return (ret);
190 }
191
192 static int fd_puts(BIO *bp, const char *str)
193 {
194     int n, ret;
195
196     n = strlen(str);
197     ret = fd_write(bp, str, n);
198     return (ret);
199 }
200
201 static int fd_gets(BIO *bp, char *buf, int size)
202 {
203     int ret = 0;
204     char *ptr = buf;
205     char *end = buf + size - 1;
206
207     while ((ptr < end) && (fd_read(bp, ptr, 1) > 0) && (ptr[0] != '\n'))
208         ptr++;
209
210     ptr[0] = '\0';
211
212     if (buf[0] != '\0')
213         ret = strlen(buf);
214     return (ret);
215 }
216
217 int BIO_fd_should_retry(int i)
218 {
219     int err;
220
221     if ((i == 0) || (i == -1)) {
222         err = get_last_sys_error();
223
224         return (BIO_fd_non_fatal_error(err));
225     }
226     return (0);
227 }
228
229 int BIO_fd_non_fatal_error(int err)
230 {
231     switch (err) {
232
233 # ifdef EWOULDBLOCK
234 #  ifdef WSAEWOULDBLOCK
235 #   if WSAEWOULDBLOCK != EWOULDBLOCK
236     case EWOULDBLOCK:
237 #   endif
238 #  else
239     case EWOULDBLOCK:
240 #  endif
241 # endif
242
243 # if defined(ENOTCONN)
244     case ENOTCONN:
245 # endif
246
247 # ifdef EINTR
248     case EINTR:
249 # endif
250
251 # ifdef EAGAIN
252 #  if EWOULDBLOCK != EAGAIN
253     case EAGAIN:
254 #  endif
255 # endif
256
257 # ifdef EPROTO
258     case EPROTO:
259 # endif
260
261 # ifdef EINPROGRESS
262     case EINPROGRESS:
263 # endif
264
265 # ifdef EALREADY
266     case EALREADY:
267 # endif
268         return (1);
269         /* break; */
270     default:
271         break;
272     }
273     return (0);
274 }
275 #endif