blob: 6d393a22e11c86782c04d6bafaa6bf21dadf64b7 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* crypto/bio/bss_sock.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.] */
57
58#include <openssl/bio.h>
59
60#include <fcntl.h>
61
62#if !defined(OPENSSL_WINDOWS)
63#include <unistd.h>
Brian Smithdc94b542015-01-27 23:06:00 -080064#else
65#define WIN32_LEAN_AND_MEAN
66#include <WinSock2.h>
Adam Langley95c29f32014-06-20 12:00:00 -070067#endif
68
69#include "internal.h"
70
71
David Benjamin04284b82014-09-30 20:51:57 -040072#if !defined(OPENSSL_WINDOWS)
73static int closesocket(int sock) {
74 return close(sock);
75}
76#endif
77
Adam Langley95c29f32014-06-20 12:00:00 -070078static int sock_new(BIO *bio) {
79 bio->init = 0;
80 bio->num = 0;
81 bio->ptr = NULL;
82 bio->flags = 0;
83 return 1;
84}
85
86static int sock_free(BIO *bio) {
87 if (bio == NULL) {
88 return 0;
89 }
90
91 if (bio->shutdown) {
92 if (bio->init) {
David Benjamin04284b82014-09-30 20:51:57 -040093 closesocket(bio->num);
Adam Langley95c29f32014-06-20 12:00:00 -070094 }
95 bio->init = 0;
96 bio->flags = 0;
97 }
98 return 1;
99}
100
101static int sock_read(BIO *b, char *out, int outl) {
102 int ret = 0;
103
104 if (out == NULL) {
105 return 0;
106 }
107
108 bio_clear_socket_error();
109 ret = recv(b->num, out, outl, 0);
110 BIO_clear_retry_flags(b);
111 if (ret <= 0) {
112 if (bio_fd_should_retry(ret)) {
113 BIO_set_retry_read(b);
114 }
115 }
116 return ret;
117}
118
119static int sock_write(BIO *b, const char *in, int inl) {
120 int ret;
121
122 bio_clear_socket_error();
123 ret = send(b->num, in, inl, 0);
124 BIO_clear_retry_flags(b);
125 if (ret <= 0) {
126 if (bio_fd_should_retry(ret)) {
127 BIO_set_retry_write(b);
128 }
129 }
130 return ret;
131}
132
133static int sock_puts(BIO *bp, const char *str) {
134 return sock_write(bp, str, strlen(str));
135}
136
137static long sock_ctrl(BIO *b, int cmd, long num, void *ptr) {
138 long ret = 1;
139 int *ip;
140
141 switch (cmd) {
142 case BIO_C_SET_FD:
143 sock_free(b);
144 b->num = *((int *)ptr);
145 b->shutdown = (int)num;
146 b->init = 1;
147 break;
148 case BIO_C_GET_FD:
149 if (b->init) {
150 ip = (int *)ptr;
151 if (ip != NULL)
152 *ip = b->num;
153 ret = b->num;
154 } else
155 ret = -1;
156 break;
157 case BIO_CTRL_GET_CLOSE:
158 ret = b->shutdown;
159 break;
160 case BIO_CTRL_SET_CLOSE:
161 b->shutdown = (int)num;
162 break;
163 case BIO_CTRL_FLUSH:
164 ret = 1;
165 break;
166 default:
167 ret = 0;
168 break;
169 }
170 return ret;
171}
172
173static const BIO_METHOD methods_sockp = {
174 BIO_TYPE_SOCKET, "socket", sock_write, sock_read, sock_puts,
175 NULL /* gets, */, sock_ctrl, sock_new, sock_free, NULL,
176};
177
178const BIO_METHOD *BIO_s_socket(void) { return &methods_sockp; }
179
180BIO *BIO_new_socket(int fd, int close_flag) {
181 BIO *ret;
182
183 ret = BIO_new(BIO_s_socket());
184 if (ret == NULL) {
185 return NULL;
186 }
187 BIO_set_fd(ret, fd, close_flag);
188 return ret;
189}