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