BIO range checking.
[openssl.git] / crypto / bio / bss_sock.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 #define USE_SOCKETS
13 #include "bio_lcl.h"
14 #include "internal/cryptlib.h"
15
16 #ifndef OPENSSL_NO_SOCK
17
18 # include <openssl/bio.h>
19
20 # ifdef WATT32
21 /* Watt-32 uses same names */
22 #  undef sock_write
23 #  undef sock_read
24 #  undef sock_puts
25 #  define sock_write SockWrite
26 #  define sock_read  SockRead
27 #  define sock_puts  SockPuts
28 # endif
29
30 static int sock_write(BIO *h, const char *buf, int num);
31 static int sock_read(BIO *h, char *buf, int size);
32 static int sock_puts(BIO *h, const char *str);
33 static long sock_ctrl(BIO *h, int cmd, long arg1, void *arg2);
34 static int sock_new(BIO *h);
35 static int sock_free(BIO *data);
36 int BIO_sock_should_retry(int s);
37
38 static const BIO_METHOD methods_sockp = {
39     BIO_TYPE_SOCKET,
40     "socket",
41     /* TODO: Convert to new style write function */
42     bwrite_conv,
43     sock_write,
44     /* TODO: Convert to new style read function */
45     bread_conv,
46     sock_read,
47     sock_puts,
48     NULL,                       /* sock_gets, */
49     sock_ctrl,
50     sock_new,
51     sock_free,
52     NULL,
53 };
54
55 const BIO_METHOD *BIO_s_socket(void)
56 {
57     return (&methods_sockp);
58 }
59
60 BIO *BIO_new_socket(int fd, int close_flag)
61 {
62     BIO *ret;
63
64     ret = BIO_new(BIO_s_socket());
65     if (ret == NULL)
66         return (NULL);
67     BIO_set_fd(ret, fd, close_flag);
68     return (ret);
69 }
70
71 static int sock_new(BIO *bi)
72 {
73     bi->init = 0;
74     bi->num = 0;
75     bi->ptr = NULL;
76     bi->flags = 0;
77     return (1);
78 }
79
80 static int sock_free(BIO *a)
81 {
82     if (a == NULL)
83         return (0);
84     if (a->shutdown) {
85         if (a->init) {
86             BIO_closesocket(a->num);
87         }
88         a->init = 0;
89         a->flags = 0;
90     }
91     return (1);
92 }
93
94 static int sock_read(BIO *b, char *out, int outl)
95 {
96     int ret = 0;
97
98     if (out != NULL) {
99         clear_socket_error();
100         ret = readsocket(b->num, out, outl);
101         BIO_clear_retry_flags(b);
102         if (ret <= 0) {
103             if (BIO_sock_should_retry(ret))
104                 BIO_set_retry_read(b);
105         }
106     }
107     return (ret);
108 }
109
110 static int sock_write(BIO *b, const char *in, int inl)
111 {
112     int ret;
113
114     clear_socket_error();
115     ret = writesocket(b->num, in, inl);
116     BIO_clear_retry_flags(b);
117     if (ret <= 0) {
118         if (BIO_sock_should_retry(ret))
119             BIO_set_retry_write(b);
120     }
121     return (ret);
122 }
123
124 static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
125 {
126     long ret = 1;
127     int *ip;
128
129     switch (cmd) {
130     case BIO_C_SET_FD:
131         sock_free(b);
132         b->num = *((int *)ptr);
133         b->shutdown = (int)num;
134         b->init = 1;
135         break;
136     case BIO_C_GET_FD:
137         if (b->init) {
138             ip = (int *)ptr;
139             if (ip != NULL)
140                 *ip = b->num;
141             ret = b->num;
142         } else
143             ret = -1;
144         break;
145     case BIO_CTRL_GET_CLOSE:
146         ret = b->shutdown;
147         break;
148     case BIO_CTRL_SET_CLOSE:
149         b->shutdown = (int)num;
150         break;
151     case BIO_CTRL_DUP:
152     case BIO_CTRL_FLUSH:
153         ret = 1;
154         break;
155     default:
156         ret = 0;
157         break;
158     }
159     return (ret);
160 }
161
162 static int sock_puts(BIO *bp, const char *str)
163 {
164     int n, ret;
165
166     n = strlen(str);
167     ret = sock_write(bp, str, n);
168     return (ret);
169 }
170
171 int BIO_sock_should_retry(int i)
172 {
173     int err;
174
175     if ((i == 0) || (i == -1)) {
176         err = get_last_socket_error();
177
178         return (BIO_sock_non_fatal_error(err));
179     }
180     return (0);
181 }
182
183 int BIO_sock_non_fatal_error(int err)
184 {
185     switch (err) {
186 # if defined(OPENSSL_SYS_WINDOWS)
187 #  if defined(WSAEWOULDBLOCK)
188     case WSAEWOULDBLOCK:
189 #  endif
190 # endif
191
192 # ifdef EWOULDBLOCK
193 #  ifdef WSAEWOULDBLOCK
194 #   if WSAEWOULDBLOCK != EWOULDBLOCK
195     case EWOULDBLOCK:
196 #   endif
197 #  else
198     case EWOULDBLOCK:
199 #  endif
200 # endif
201
202 # if defined(ENOTCONN)
203     case ENOTCONN:
204 # endif
205
206 # ifdef EINTR
207     case EINTR:
208 # endif
209
210 # ifdef EAGAIN
211 #  if EWOULDBLOCK != EAGAIN
212     case EAGAIN:
213 #  endif
214 # endif
215
216 # ifdef EPROTO
217     case EPROTO:
218 # endif
219
220 # ifdef EINPROGRESS
221     case EINPROGRESS:
222 # endif
223
224 # ifdef EALREADY
225     case EALREADY:
226 # endif
227         return (1);
228     default:
229         break;
230     }
231     return (0);
232 }
233
234 #endif                          /* #ifndef OPENSSL_NO_SOCK */