Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/bio/socket.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/bio.h>
5
6 #if !defined(OPENSSL_NO_SOCK)
7
8 #include <fcntl.h>
9 #include <string.h>
10
11 #if !defined(OPENSSL_WINDOWS)
12 #include <unistd.h>
13 #else
14 OPENSSL_MSVC_PRAGMA(warning(push, 3))
15 #include <winsock2.h>
16 OPENSSL_MSVC_PRAGMA(warning(pop))
17 #endif
18
19 #include "internal.h"
20
21
22 #if !defined(OPENSSL_WINDOWS)
23 static int closesocket(int sock) {
24 return close(sock);
25 }
26 #endif
27
28 static int sock_free(BIO *bio) {
29 if (bio->shutdown) {
30 if (bio->init) {
31 closesocket(bio->num);
32 }
33 bio->init = 0;
34 bio->flags = 0;
35 }
36 return 1;
37 }
38
39 static int sock_read(BIO *b, char *out, int outl) {
40 if (out == NULL) {
41 return 0;
42 }
43
44 bio_clear_socket_error(b->num);
45 #if defined(OPENSSL_WINDOWS)
46 int ret = recv(b->num, out, outl, 0);
47 #else
48 int ret = (int)read(b->num, out, outl);
49 #endif
50 BIO_clear_retry_flags(b);
51 if (ret <= 0) {
52 if (bio_socket_should_retry(ret)) {
53 BIO_set_retry_read(b);
54 }
55 }
56 return ret;
57 }
58
59 static int sock_write(BIO *b, const char *in, int inl) {
60 bio_clear_socket_error(b->num);
61 #if defined(OPENSSL_WINDOWS)
62 int ret = send(b->num, in, inl, 0);
63 #else
64 int ret = (int)write(b->num, in, inl);
65 #endif
66 BIO_clear_retry_flags(b);
67 if (ret <= 0) {
68 if (bio_socket_should_retry(ret)) {
69 BIO_set_retry_write(b);
70 }
71 }
72 return ret;
73 }
74
75 static long sock_ctrl(BIO *b, int cmd, long num, void *ptr) {
76 long ret = 1;
77 int *ip;
78
79 switch (cmd) {
80 case BIO_C_SET_FD:
81 sock_free(b);
82 b->num = *((int *)ptr);
83 b->shutdown = (int)num;
84 b->init = 1;
85 break;
86 case BIO_C_GET_FD:
87 if (b->init) {
88 ip = (int *)ptr;
89 if (ip != NULL) {
90 *ip = b->num;
91 }
92 ret = b->num;
93 } else {
94 ret = -1;
95 }
96 break;
97 case BIO_CTRL_GET_CLOSE:
98 ret = b->shutdown;
99 break;
100 case BIO_CTRL_SET_CLOSE:
101 b->shutdown = (int)num;
102 break;
103 case BIO_CTRL_FLUSH:
104 ret = 1;
105 break;
106 default:
107 ret = 0;
108 break;
109 }
110 return ret;
111 }
112
113 static const BIO_METHOD methods_sockp = {
114 BIO_TYPE_SOCKET, "socket",
115 sock_write, sock_read,
116 NULL /* puts */, NULL /* gets, */,
117 sock_ctrl, NULL /* create */,
118 sock_free, NULL /* callback_ctrl */,
119 };
120
121 const BIO_METHOD *BIO_s_socket(void) { return &methods_sockp; }
122
123 BIO *BIO_new_socket(int fd, int close_flag) {
124 BIO *ret;
125
126 ret = BIO_new(BIO_s_socket());
127 if (ret == NULL) {
128 return NULL;
129 }
130 BIO_set_fd(ret, fd, close_flag);
131 return ret;
132 }
133
134 #endif // OPENSSL_NO_SOCK