| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/bio.h> |
| 16 |
| 17 | #if !defined(OPENSSL_NO_SOCK) |
| 18 |
| 19 | #include <fcntl.h> |
| 20 | #include <string.h> |
| 21 |
| 22 | #if !defined(OPENSSL_WINDOWS) |
| 23 | #include <unistd.h> |
| 24 | #else |
| 25 | #include <winsock2.h> |
| 26 | OPENSSL_MSVC_PRAGMA(comment(lib, "Ws2_32.lib")) |
| 27 | #endif |
| 28 |
| 29 | #include "internal.h" |
| 30 |
| 31 |
| 32 | #if !defined(OPENSSL_WINDOWS) |
| 33 | static int closesocket(int sock) { |
| 34 | return close(sock); |
| 35 | } |
| 36 | #endif |
| 37 |
| 38 | static int sock_free(BIO *bio) { |
| 39 | if (bio->shutdown) { |
| 40 | if (bio->init) { |
| 41 | closesocket(bio->num); |
| 42 | } |
| 43 | bio->init = 0; |
| 44 | bio->flags = 0; |
| 45 | } |
| 46 | return 1; |
| 47 | } |
| 48 |
| 49 | static int sock_read(BIO *b, char *out, int outl) { |
| 50 | if (out == NULL) { |
| 51 | return 0; |
| 52 | } |
| 53 |
| 54 | bio_clear_socket_error(); |
| 55 | #if defined(OPENSSL_WINDOWS) |
| 56 | int ret = recv(b->num, out, outl, 0); |
| 57 | #else |
| 58 | int ret = (int)read(b->num, out, outl); |
| 59 | #endif |
| 60 | BIO_clear_retry_flags(b); |
| 61 | if (ret <= 0) { |
| 62 | if (bio_socket_should_retry(ret)) { |
| 63 | BIO_set_retry_read(b); |
| 64 | } |
| 65 | } |
| 66 | return ret; |
| 67 | } |
| 68 |
| 69 | static int sock_write(BIO *b, const char *in, int inl) { |
| 70 | bio_clear_socket_error(); |
| 71 | #if defined(OPENSSL_WINDOWS) |
| 72 | int ret = send(b->num, in, inl, 0); |
| 73 | #else |
| 74 | int ret = (int)write(b->num, in, inl); |
| 75 | #endif |
| 76 | BIO_clear_retry_flags(b); |
| 77 | if (ret <= 0) { |
| 78 | if (bio_socket_should_retry(ret)) { |
| 79 | BIO_set_retry_write(b); |
| 80 | } |
| 81 | } |
| 82 | return ret; |
| 83 | } |
| 84 |
| 85 | static long sock_ctrl(BIO *b, int cmd, long num, void *ptr) { |
| 86 | long ret = 1; |
| 87 | int *ip; |
| 88 |
| 89 | switch (cmd) { |
| 90 | case BIO_C_SET_FD: |
| 91 | sock_free(b); |
| 92 | b->num = *((int *)ptr); |
| 93 | b->shutdown = (int)num; |
| 94 | b->init = 1; |
| 95 | break; |
| 96 | case BIO_C_GET_FD: |
| 97 | if (b->init) { |
| 98 | ip = (int *)ptr; |
| 99 | if (ip != NULL) { |
| 100 | *ip = b->num; |
| 101 | } |
| 102 | ret = b->num; |
| 103 | } else { |
| 104 | ret = -1; |
| 105 | } |
| 106 | break; |
| 107 | case BIO_CTRL_GET_CLOSE: |
| 108 | ret = b->shutdown; |
| 109 | break; |
| 110 | case BIO_CTRL_SET_CLOSE: |
| 111 | b->shutdown = (int)num; |
| 112 | break; |
| 113 | case BIO_CTRL_FLUSH: |
| 114 | ret = 1; |
| 115 | break; |
| 116 | default: |
| 117 | ret = 0; |
| 118 | break; |
| 119 | } |
| 120 | return ret; |
| 121 | } |
| 122 |
| 123 | static const BIO_METHOD methods_sockp = { |
| 124 | BIO_TYPE_SOCKET, "socket", |
| 125 | sock_write, sock_read, |
| 126 | NULL /* puts */, NULL /* gets, */, |
| 127 | sock_ctrl, NULL /* create */, |
| 128 | sock_free, NULL /* callback_ctrl */, |
| 129 | }; |
| 130 |
| 131 | const BIO_METHOD *BIO_s_socket(void) { return &methods_sockp; } |
| 132 |
| 133 | BIO *BIO_new_socket(int fd, int close_flag) { |
| 134 | BIO *ret; |
| 135 |
| 136 | ret = BIO_new(BIO_s_socket()); |
| 137 | if (ret == NULL) { |
| 138 | return NULL; |
| 139 | } |
| 140 | BIO_set_fd(ret, fd, close_flag); |
| 141 | return ret; |
| 142 | } |
| 143 |
| 144 | #endif // OPENSSL_NO_SOCK |
| 145 |