| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/libressl/src/crypto/bio/bss_dgram.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /* $OpenBSD: bss_dgram.c,v 1.42 2018/05/12 17:47:53 tb Exp $ */ | - | ||||||||||||
| 2 | /* | - | ||||||||||||
| 3 | * DTLS implementation written by Nagendra Modadugu | - | ||||||||||||
| 4 | * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. | - | ||||||||||||
| 5 | */ | - | ||||||||||||
| 6 | /* ==================================================================== | - | ||||||||||||
| 7 | * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. | - | ||||||||||||
| 8 | * | - | ||||||||||||
| 9 | * Redistribution and use in source and binary forms, with or without | - | ||||||||||||
| 10 | * modification, are permitted provided that the following conditions | - | ||||||||||||
| 11 | * are met: | - | ||||||||||||
| 12 | * | - | ||||||||||||
| 13 | * 1. Redistributions of source code must retain the above copyright | - | ||||||||||||
| 14 | * notice, this list of conditions and the following disclaimer. | - | ||||||||||||
| 15 | * | - | ||||||||||||
| 16 | * 2. Redistributions in binary form must reproduce the above copyright | - | ||||||||||||
| 17 | * notice, this list of conditions and the following disclaimer in | - | ||||||||||||
| 18 | * the documentation and/or other materials provided with the | - | ||||||||||||
| 19 | * distribution. | - | ||||||||||||
| 20 | * | - | ||||||||||||
| 21 | * 3. All advertising materials mentioning features or use of this | - | ||||||||||||
| 22 | * software must display the following acknowledgment: | - | ||||||||||||
| 23 | * "This product includes software developed by the OpenSSL Project | - | ||||||||||||
| 24 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | - | ||||||||||||
| 25 | * | - | ||||||||||||
| 26 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | - | ||||||||||||
| 27 | * endorse or promote products derived from this software without | - | ||||||||||||
| 28 | * prior written permission. For written permission, please contact | - | ||||||||||||
| 29 | * openssl-core@OpenSSL.org. | - | ||||||||||||
| 30 | * | - | ||||||||||||
| 31 | * 5. Products derived from this software may not be called "OpenSSL" | - | ||||||||||||
| 32 | * nor may "OpenSSL" appear in their names without prior written | - | ||||||||||||
| 33 | * permission of the OpenSSL Project. | - | ||||||||||||
| 34 | * | - | ||||||||||||
| 35 | * 6. Redistributions of any form whatsoever must retain the following | - | ||||||||||||
| 36 | * acknowledgment: | - | ||||||||||||
| 37 | * "This product includes software developed by the OpenSSL Project | - | ||||||||||||
| 38 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | - | ||||||||||||
| 39 | * | - | ||||||||||||
| 40 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | - | ||||||||||||
| 41 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | - | ||||||||||||
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | - | ||||||||||||
| 43 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | - | ||||||||||||
| 44 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | - | ||||||||||||
| 45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | - | ||||||||||||
| 46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | - | ||||||||||||
| 47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | - | ||||||||||||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | - | ||||||||||||
| 49 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | - | ||||||||||||
| 50 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | - | ||||||||||||
| 51 | * OF THE POSSIBILITY OF SUCH DAMAGE. | - | ||||||||||||
| 52 | * ==================================================================== | - | ||||||||||||
| 53 | * | - | ||||||||||||
| 54 | * This product includes cryptographic software written by Eric Young | - | ||||||||||||
| 55 | * (eay@cryptsoft.com). This product includes software written by Tim | - | ||||||||||||
| 56 | * Hudson (tjh@cryptsoft.com). | - | ||||||||||||
| 57 | * | - | ||||||||||||
| 58 | */ | - | ||||||||||||
| 59 | - | |||||||||||||
| 60 | #include <sys/socket.h> | - | ||||||||||||
| 61 | #include <sys/time.h> | - | ||||||||||||
| 62 | - | |||||||||||||
| 63 | #include <netinet/in.h> | - | ||||||||||||
| 64 | - | |||||||||||||
| 65 | #include <errno.h> | - | ||||||||||||
| 66 | #include <netdb.h> | - | ||||||||||||
| 67 | #include <stdio.h> | - | ||||||||||||
| 68 | #include <string.h> | - | ||||||||||||
| 69 | #include <unistd.h> | - | ||||||||||||
| 70 | - | |||||||||||||
| 71 | #include <openssl/opensslconf.h> | - | ||||||||||||
| 72 | - | |||||||||||||
| 73 | #include <openssl/bio.h> | - | ||||||||||||
| 74 | - | |||||||||||||
| 75 | #ifndef OPENSSL_NO_DGRAM | - | ||||||||||||
| 76 | - | |||||||||||||
| 77 | - | |||||||||||||
| 78 | static int dgram_write(BIO *h, const char *buf, int num); | - | ||||||||||||
| 79 | static int dgram_read(BIO *h, char *buf, int size); | - | ||||||||||||
| 80 | static int dgram_puts(BIO *h, const char *str); | - | ||||||||||||
| 81 | static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2); | - | ||||||||||||
| 82 | static int dgram_new(BIO *h); | - | ||||||||||||
| 83 | static int dgram_free(BIO *data); | - | ||||||||||||
| 84 | static int dgram_clear(BIO *bio); | - | ||||||||||||
| 85 | - | |||||||||||||
| 86 | - | |||||||||||||
| 87 | static int BIO_dgram_should_retry(int s); | - | ||||||||||||
| 88 | - | |||||||||||||
| 89 | static const BIO_METHOD methods_dgramp = { | - | ||||||||||||
| 90 | .type = BIO_TYPE_DGRAM, | - | ||||||||||||
| 91 | .name = "datagram socket", | - | ||||||||||||
| 92 | .bwrite = dgram_write, | - | ||||||||||||
| 93 | .bread = dgram_read, | - | ||||||||||||
| 94 | .bputs = dgram_puts, | - | ||||||||||||
| 95 | .ctrl = dgram_ctrl, | - | ||||||||||||
| 96 | .create = dgram_new, | - | ||||||||||||
| 97 | .destroy = dgram_free | - | ||||||||||||
| 98 | }; | - | ||||||||||||
| 99 | - | |||||||||||||
| 100 | - | |||||||||||||
| 101 | typedef struct bio_dgram_data_st { | - | ||||||||||||
| 102 | union { | - | ||||||||||||
| 103 | struct sockaddr sa; | - | ||||||||||||
| 104 | struct sockaddr_in sa_in; | - | ||||||||||||
| 105 | struct sockaddr_in6 sa_in6; | - | ||||||||||||
| 106 | } peer; | - | ||||||||||||
| 107 | unsigned int connected; | - | ||||||||||||
| 108 | unsigned int _errno; | - | ||||||||||||
| 109 | unsigned int mtu; | - | ||||||||||||
| 110 | struct timeval next_timeout; | - | ||||||||||||
| 111 | struct timeval socket_timeout; | - | ||||||||||||
| 112 | } bio_dgram_data; | - | ||||||||||||
| 113 | - | |||||||||||||
| 114 | - | |||||||||||||
| 115 | const BIO_METHOD * | - | ||||||||||||
| 116 | BIO_s_datagram(void) | - | ||||||||||||
| 117 | { | - | ||||||||||||
| 118 | return (&methods_dgramp); never executed: return (&methods_dgramp); | 0 | ||||||||||||
| 119 | } | - | ||||||||||||
| 120 | - | |||||||||||||
| 121 | BIO * | - | ||||||||||||
| 122 | BIO_new_dgram(int fd, int close_flag) | - | ||||||||||||
| 123 | { | - | ||||||||||||
| 124 | BIO *ret; | - | ||||||||||||
| 125 | - | |||||||||||||
| 126 | ret = BIO_new(BIO_s_datagram()); | - | ||||||||||||
| 127 | if (ret == NULL)
| 0 | ||||||||||||
| 128 | return (NULL); never executed: return ( ((void *)0) ); | 0 | ||||||||||||
| 129 | BIO_set_fd(ret, fd, close_flag); | - | ||||||||||||
| 130 | return (ret); never executed: return (ret); | 0 | ||||||||||||
| 131 | } | - | ||||||||||||
| 132 | - | |||||||||||||
| 133 | static int | - | ||||||||||||
| 134 | dgram_new(BIO *bi) | - | ||||||||||||
| 135 | { | - | ||||||||||||
| 136 | bio_dgram_data *data = NULL; | - | ||||||||||||
| 137 | - | |||||||||||||
| 138 | bi->init = 0; | - | ||||||||||||
| 139 | bi->num = 0; | - | ||||||||||||
| 140 | data = calloc(1, sizeof(bio_dgram_data)); | - | ||||||||||||
| 141 | if (data == NULL)
| 0 | ||||||||||||
| 142 | return 0; never executed: return 0; | 0 | ||||||||||||
| 143 | bi->ptr = data; | - | ||||||||||||
| 144 | - | |||||||||||||
| 145 | bi->flags = 0; | - | ||||||||||||
| 146 | return (1); never executed: return (1); | 0 | ||||||||||||
| 147 | } | - | ||||||||||||
| 148 | - | |||||||||||||
| 149 | static int | - | ||||||||||||
| 150 | dgram_free(BIO *a) | - | ||||||||||||
| 151 | { | - | ||||||||||||
| 152 | bio_dgram_data *data; | - | ||||||||||||
| 153 | - | |||||||||||||
| 154 | if (a == NULL)
| 0 | ||||||||||||
| 155 | return (0); never executed: return (0); | 0 | ||||||||||||
| 156 | if (!dgram_clear(a))
| 0 | ||||||||||||
| 157 | return 0; never executed: return 0; | 0 | ||||||||||||
| 158 | - | |||||||||||||
| 159 | data = (bio_dgram_data *)a->ptr; | - | ||||||||||||
| 160 | free(data); | - | ||||||||||||
| 161 | - | |||||||||||||
| 162 | return (1); never executed: return (1); | 0 | ||||||||||||
| 163 | } | - | ||||||||||||
| 164 | - | |||||||||||||
| 165 | static int | - | ||||||||||||
| 166 | dgram_clear(BIO *a) | - | ||||||||||||
| 167 | { | - | ||||||||||||
| 168 | if (a == NULL)
| 0 | ||||||||||||
| 169 | return (0); never executed: return (0); | 0 | ||||||||||||
| 170 | if (a->shutdown) {
| 0 | ||||||||||||
| 171 | if (a->init) {
| 0 | ||||||||||||
| 172 | shutdown(a->num, SHUT_RDWR); | - | ||||||||||||
| 173 | close(a->num); | - | ||||||||||||
| 174 | } never executed: end of block | 0 | ||||||||||||
| 175 | a->init = 0; | - | ||||||||||||
| 176 | a->flags = 0; | - | ||||||||||||
| 177 | } never executed: end of block | 0 | ||||||||||||
| 178 | return (1); never executed: return (1); | 0 | ||||||||||||
| 179 | } | - | ||||||||||||
| 180 | - | |||||||||||||
| 181 | static void | - | ||||||||||||
| 182 | dgram_adjust_rcv_timeout(BIO *b) | - | ||||||||||||
| 183 | { | - | ||||||||||||
| 184 | #if defined(SO_RCVTIMEO) | - | ||||||||||||
| 185 | bio_dgram_data *data = (bio_dgram_data *)b->ptr; | - | ||||||||||||
| 186 | - | |||||||||||||
| 187 | /* Is a timer active? */ | - | ||||||||||||
| 188 | if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
| 0 | ||||||||||||
| 189 | struct timeval timenow, timeleft; | - | ||||||||||||
| 190 | - | |||||||||||||
| 191 | /* Read current socket timeout */ | - | ||||||||||||
| 192 | socklen_t sz = sizeof(data->socket_timeout); | - | ||||||||||||
| 193 | if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
| 0 | ||||||||||||
| 194 | &(data->socket_timeout), &sz) < 0) {
| 0 | ||||||||||||
| 195 | perror("getsockopt"); | - | ||||||||||||
| 196 | } never executed: end of block | 0 | ||||||||||||
| 197 | - | |||||||||||||
| 198 | /* Get current time */ | - | ||||||||||||
| 199 | gettimeofday(&timenow, NULL); | - | ||||||||||||
| 200 | - | |||||||||||||
| 201 | /* Calculate time left until timer expires */ | - | ||||||||||||
| 202 | memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval)); | - | ||||||||||||
| 203 | timeleft.tv_sec -= timenow.tv_sec; | - | ||||||||||||
| 204 | timeleft.tv_usec -= timenow.tv_usec; | - | ||||||||||||
| 205 | if (timeleft.tv_usec < 0) {
| 0 | ||||||||||||
| 206 | timeleft.tv_sec--; | - | ||||||||||||
| 207 | timeleft.tv_usec += 1000000; | - | ||||||||||||
| 208 | } never executed: end of block | 0 | ||||||||||||
| 209 | - | |||||||||||||
| 210 | if (timeleft.tv_sec < 0) {
| 0 | ||||||||||||
| 211 | timeleft.tv_sec = 0; | - | ||||||||||||
| 212 | timeleft.tv_usec = 1; | - | ||||||||||||
| 213 | } never executed: end of block | 0 | ||||||||||||
| 214 | - | |||||||||||||
| 215 | /* Adjust socket timeout if next handhake message timer | - | ||||||||||||
| 216 | * will expire earlier. | - | ||||||||||||
| 217 | */ | - | ||||||||||||
| 218 | if ((data->socket_timeout.tv_sec == 0 &&
| 0 | ||||||||||||
| 219 | data->socket_timeout.tv_usec == 0) ||
| 0 | ||||||||||||
| 220 | (data->socket_timeout.tv_sec > timeleft.tv_sec) ||
| 0 | ||||||||||||
| 221 | (data->socket_timeout.tv_sec == timeleft.tv_sec &&
| 0 | ||||||||||||
| 222 | data->socket_timeout.tv_usec >= timeleft.tv_usec)) {
| 0 | ||||||||||||
| 223 | if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
| 0 | ||||||||||||
| 224 | &timeleft, sizeof(struct timeval)) < 0) {
| 0 | ||||||||||||
| 225 | perror("setsockopt"); | - | ||||||||||||
| 226 | } never executed: end of block | 0 | ||||||||||||
| 227 | } never executed: end of block | 0 | ||||||||||||
| 228 | } never executed: end of block | 0 | ||||||||||||
| 229 | #endif | - | ||||||||||||
| 230 | } never executed: end of block | 0 | ||||||||||||
| 231 | - | |||||||||||||
| 232 | static void | - | ||||||||||||
| 233 | dgram_reset_rcv_timeout(BIO *b) | - | ||||||||||||
| 234 | { | - | ||||||||||||
| 235 | #if defined(SO_RCVTIMEO) | - | ||||||||||||
| 236 | bio_dgram_data *data = (bio_dgram_data *)b->ptr; | - | ||||||||||||
| 237 | - | |||||||||||||
| 238 | /* Is a timer active? */ | - | ||||||||||||
| 239 | if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
| 0 | ||||||||||||
| 240 | if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
| 0 | ||||||||||||
| 241 | &(data->socket_timeout), sizeof(struct timeval)) < 0) {
| 0 | ||||||||||||
| 242 | perror("setsockopt"); | - | ||||||||||||
| 243 | } never executed: end of block | 0 | ||||||||||||
| 244 | } never executed: end of block | 0 | ||||||||||||
| 245 | #endif | - | ||||||||||||
| 246 | } never executed: end of block | 0 | ||||||||||||
| 247 | - | |||||||||||||
| 248 | static int | - | ||||||||||||
| 249 | dgram_read(BIO *b, char *out, int outl) | - | ||||||||||||
| 250 | { | - | ||||||||||||
| 251 | int ret = 0; | - | ||||||||||||
| 252 | bio_dgram_data *data = (bio_dgram_data *)b->ptr; | - | ||||||||||||
| 253 | - | |||||||||||||
| 254 | struct { | - | ||||||||||||
| 255 | socklen_t len; | - | ||||||||||||
| 256 | union { | - | ||||||||||||
| 257 | struct sockaddr sa; | - | ||||||||||||
| 258 | struct sockaddr_in sa_in; | - | ||||||||||||
| 259 | struct sockaddr_in6 sa_in6; | - | ||||||||||||
| 260 | } peer; | - | ||||||||||||
| 261 | } sa; | - | ||||||||||||
| 262 | - | |||||||||||||
| 263 | sa.len = sizeof(sa.peer); | - | ||||||||||||
| 264 | - | |||||||||||||
| 265 | if (out != NULL) {
| 0 | ||||||||||||
| 266 | errno = 0; | - | ||||||||||||
| 267 | memset(&sa.peer, 0, sizeof(sa.peer)); | - | ||||||||||||
| 268 | dgram_adjust_rcv_timeout(b); | - | ||||||||||||
| 269 | ret = recvfrom(b->num, out, outl, 0, &sa.peer.sa, &sa.len); | - | ||||||||||||
| 270 | - | |||||||||||||
| 271 | if (! data->connected && ret >= 0)
| 0 | ||||||||||||
| 272 | BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &sa.peer); never executed: BIO_ctrl(b, 44, 0, &sa.peer); | 0 | ||||||||||||
| 273 | - | |||||||||||||
| 274 | BIO_clear_retry_flags(b); | - | ||||||||||||
| 275 | if (ret < 0) {
| 0 | ||||||||||||
| 276 | if (BIO_dgram_should_retry(ret)) {
| 0 | ||||||||||||
| 277 | BIO_set_retry_read(b); | - | ||||||||||||
| 278 | data->_errno = errno; | - | ||||||||||||
| 279 | } never executed: end of block | 0 | ||||||||||||
| 280 | } never executed: end of block | 0 | ||||||||||||
| 281 | - | |||||||||||||
| 282 | dgram_reset_rcv_timeout(b); | - | ||||||||||||
| 283 | } never executed: end of block | 0 | ||||||||||||
| 284 | return (ret); never executed: return (ret); | 0 | ||||||||||||
| 285 | } | - | ||||||||||||
| 286 | - | |||||||||||||
| 287 | static int | - | ||||||||||||
| 288 | dgram_write(BIO *b, const char *in, int inl) | - | ||||||||||||
| 289 | { | - | ||||||||||||
| 290 | int ret; | - | ||||||||||||
| 291 | bio_dgram_data *data = (bio_dgram_data *)b->ptr; | - | ||||||||||||
| 292 | errno = 0; | - | ||||||||||||
| 293 | - | |||||||||||||
| 294 | if (data->connected)
| 0 | ||||||||||||
| 295 | ret = write(b->num, in, inl); never executed: ret = write(b->num, in, inl); | 0 | ||||||||||||
| 296 | else { | - | ||||||||||||
| 297 | int peerlen = sizeof(data->peer); | - | ||||||||||||
| 298 | - | |||||||||||||
| 299 | if (data->peer.sa.sa_family == AF_INET)
| 0 | ||||||||||||
| 300 | peerlen = sizeof(data->peer.sa_in); never executed: peerlen = sizeof(data->peer.sa_in); | 0 | ||||||||||||
| 301 | else if (data->peer.sa.sa_family == AF_INET6)
| 0 | ||||||||||||
| 302 | peerlen = sizeof(data->peer.sa_in6); never executed: peerlen = sizeof(data->peer.sa_in6); | 0 | ||||||||||||
| 303 | ret = sendto(b->num, in, inl, 0, &data->peer.sa, peerlen); | - | ||||||||||||
| 304 | } never executed: end of block | 0 | ||||||||||||
| 305 | - | |||||||||||||
| 306 | BIO_clear_retry_flags(b); | - | ||||||||||||
| 307 | if (ret <= 0) {
| 0 | ||||||||||||
| 308 | if (BIO_dgram_should_retry(ret)) {
| 0 | ||||||||||||
| 309 | BIO_set_retry_write(b); | - | ||||||||||||
| 310 | - | |||||||||||||
| 311 | data->_errno = errno; | - | ||||||||||||
| 312 | /* | - | ||||||||||||
| 313 | * higher layers are responsible for querying MTU, | - | ||||||||||||
| 314 | * if necessary | - | ||||||||||||
| 315 | */ | - | ||||||||||||
| 316 | } never executed: end of block | 0 | ||||||||||||
| 317 | } never executed: end of block | 0 | ||||||||||||
| 318 | return (ret); never executed: return (ret); | 0 | ||||||||||||
| 319 | } | - | ||||||||||||
| 320 | - | |||||||||||||
| 321 | static long | - | ||||||||||||
| 322 | dgram_ctrl(BIO *b, int cmd, long num, void *ptr) | - | ||||||||||||
| 323 | { | - | ||||||||||||
| 324 | long ret = 1; | - | ||||||||||||
| 325 | int *ip; | - | ||||||||||||
| 326 | struct sockaddr *to = NULL; | - | ||||||||||||
| 327 | bio_dgram_data *data = NULL; | - | ||||||||||||
| 328 | #if (defined(IP_MTU_DISCOVER) || defined(IP_MTU)) | - | ||||||||||||
| 329 | int sockopt_val = 0; | - | ||||||||||||
| 330 | socklen_t sockopt_len; /* assume that system supporting IP_MTU is | - | ||||||||||||
| 331 | * modern enough to define socklen_t */ | - | ||||||||||||
| 332 | socklen_t addr_len; | - | ||||||||||||
| 333 | union { | - | ||||||||||||
| 334 | struct sockaddr sa; | - | ||||||||||||
| 335 | struct sockaddr_in s4; | - | ||||||||||||
| 336 | struct sockaddr_in6 s6; | - | ||||||||||||
| 337 | } addr; | - | ||||||||||||
| 338 | #endif | - | ||||||||||||
| 339 | - | |||||||||||||
| 340 | data = (bio_dgram_data *)b->ptr; | - | ||||||||||||
| 341 | - | |||||||||||||
| 342 | switch (cmd) { | - | ||||||||||||
| 343 | case BIO_CTRL_RESET: never executed: case 1: | 0 | ||||||||||||
| 344 | num = 0; | - | ||||||||||||
| 345 | case BIO_C_FILE_SEEK: code before this statement never executed: case 128:never executed: case 128: | 0 | ||||||||||||
| 346 | ret = 0; | - | ||||||||||||
| 347 | break; never executed: break; | 0 | ||||||||||||
| 348 | case BIO_C_FILE_TELL: never executed: case 133: | 0 | ||||||||||||
| 349 | case BIO_CTRL_INFO: never executed: case 3: | 0 | ||||||||||||
| 350 | ret = 0; | - | ||||||||||||
| 351 | break; never executed: break; | 0 | ||||||||||||
| 352 | case BIO_C_SET_FD: never executed: case 104: | 0 | ||||||||||||
| 353 | dgram_clear(b); | - | ||||||||||||
| 354 | b->num= *((int *)ptr); | - | ||||||||||||
| 355 | b->shutdown = (int)num; | - | ||||||||||||
| 356 | b->init = 1; | - | ||||||||||||
| 357 | break; never executed: break; | 0 | ||||||||||||
| 358 | case BIO_C_GET_FD: never executed: case 105: | 0 | ||||||||||||
| 359 | if (b->init) {
| 0 | ||||||||||||
| 360 | ip = (int *)ptr; | - | ||||||||||||
| 361 | if (ip != NULL)
| 0 | ||||||||||||
| 362 | *ip = b->num; never executed: *ip = b->num; | 0 | ||||||||||||
| 363 | ret = b->num; | - | ||||||||||||
| 364 | } else never executed: end of block | 0 | ||||||||||||
| 365 | ret = -1; never executed: ret = -1; | 0 | ||||||||||||
| 366 | break; never executed: break; | 0 | ||||||||||||
| 367 | case BIO_CTRL_GET_CLOSE: never executed: case 8: | 0 | ||||||||||||
| 368 | ret = b->shutdown; | - | ||||||||||||
| 369 | break; never executed: break; | 0 | ||||||||||||
| 370 | case BIO_CTRL_SET_CLOSE: never executed: case 9: | 0 | ||||||||||||
| 371 | b->shutdown = (int)num; | - | ||||||||||||
| 372 | break; never executed: break; | 0 | ||||||||||||
| 373 | case BIO_CTRL_PENDING: never executed: case 10: | 0 | ||||||||||||
| 374 | case BIO_CTRL_WPENDING: never executed: case 13: | 0 | ||||||||||||
| 375 | ret = 0; | - | ||||||||||||
| 376 | break; never executed: break; | 0 | ||||||||||||
| 377 | case BIO_CTRL_DUP: never executed: case 12: | 0 | ||||||||||||
| 378 | case BIO_CTRL_FLUSH: never executed: case 11: | 0 | ||||||||||||
| 379 | ret = 1; | - | ||||||||||||
| 380 | break; never executed: break; | 0 | ||||||||||||
| 381 | case BIO_CTRL_DGRAM_CONNECT: never executed: case 31: | 0 | ||||||||||||
| 382 | to = (struct sockaddr *)ptr; | - | ||||||||||||
| 383 | switch (to->sa_family) { | - | ||||||||||||
| 384 | case AF_INET: never executed: case 2 : | 0 | ||||||||||||
| 385 | memcpy(&data->peer, to, sizeof(data->peer.sa_in)); | - | ||||||||||||
| 386 | break; never executed: break; | 0 | ||||||||||||
| 387 | case AF_INET6: never executed: case 10 : | 0 | ||||||||||||
| 388 | memcpy(&data->peer, to, sizeof(data->peer.sa_in6)); | - | ||||||||||||
| 389 | break; never executed: break; | 0 | ||||||||||||
| 390 | default: never executed: default: | 0 | ||||||||||||
| 391 | memcpy(&data->peer, to, sizeof(data->peer.sa)); | - | ||||||||||||
| 392 | break; never executed: break; | 0 | ||||||||||||
| 393 | } | - | ||||||||||||
| 394 | break; never executed: break; | 0 | ||||||||||||
| 395 | /* (Linux)kernel sets DF bit on outgoing IP packets */ | - | ||||||||||||
| 396 | case BIO_CTRL_DGRAM_MTU_DISCOVER: never executed: case 39: | 0 | ||||||||||||
| 397 | #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) | - | ||||||||||||
| 398 | addr_len = (socklen_t)sizeof(addr); | - | ||||||||||||
| 399 | memset((void *)&addr, 0, sizeof(addr)); | - | ||||||||||||
| 400 | if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
| 0 | ||||||||||||
| 401 | ret = 0; | - | ||||||||||||
| 402 | break; never executed: break; | 0 | ||||||||||||
| 403 | } | - | ||||||||||||
| 404 | switch (addr.sa.sa_family) { | - | ||||||||||||
| 405 | case AF_INET: never executed: case 2 : | 0 | ||||||||||||
| 406 | sockopt_val = IP_PMTUDISC_DO; | - | ||||||||||||
| 407 | ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER, | - | ||||||||||||
| 408 | &sockopt_val, sizeof(sockopt_val)); | - | ||||||||||||
| 409 | if (ret < 0)
| 0 | ||||||||||||
| 410 | perror("setsockopt"); never executed: perror("setsockopt"); | 0 | ||||||||||||
| 411 | break; never executed: break; | 0 | ||||||||||||
| 412 | #if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) | - | ||||||||||||
| 413 | case AF_INET6: never executed: case 10 : | 0 | ||||||||||||
| 414 | sockopt_val = IPV6_PMTUDISC_DO; | - | ||||||||||||
| 415 | ret = setsockopt(b->num, IPPROTO_IPV6, | - | ||||||||||||
| 416 | IPV6_MTU_DISCOVER, &sockopt_val, | - | ||||||||||||
| 417 | sizeof(sockopt_val)); | - | ||||||||||||
| 418 | if (ret < 0)
| 0 | ||||||||||||
| 419 | perror("setsockopt"); never executed: perror("setsockopt"); | 0 | ||||||||||||
| 420 | break; never executed: break; | 0 | ||||||||||||
| 421 | #endif | - | ||||||||||||
| 422 | default: never executed: default: | 0 | ||||||||||||
| 423 | ret = -1; | - | ||||||||||||
| 424 | break; never executed: break; | 0 | ||||||||||||
| 425 | } | - | ||||||||||||
| 426 | #else | - | ||||||||||||
| 427 | ret = -1; | - | ||||||||||||
| 428 | #endif | - | ||||||||||||
| 429 | break; never executed: break; | 0 | ||||||||||||
| 430 | case BIO_CTRL_DGRAM_QUERY_MTU: never executed: case 40: | 0 | ||||||||||||
| 431 | #if defined(IP_MTU) | - | ||||||||||||
| 432 | addr_len = (socklen_t)sizeof(addr); | - | ||||||||||||
| 433 | memset((void *)&addr, 0, sizeof(addr)); | - | ||||||||||||
| 434 | if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
| 0 | ||||||||||||
| 435 | ret = 0; | - | ||||||||||||
| 436 | break; never executed: break; | 0 | ||||||||||||
| 437 | } | - | ||||||||||||
| 438 | sockopt_len = sizeof(sockopt_val); | - | ||||||||||||
| 439 | switch (addr.sa.sa_family) { | - | ||||||||||||
| 440 | case AF_INET: never executed: case 2 : | 0 | ||||||||||||
| 441 | ret = getsockopt(b->num, IPPROTO_IP, IP_MTU, | - | ||||||||||||
| 442 | &sockopt_val, &sockopt_len); | - | ||||||||||||
| 443 | if (ret < 0 || sockopt_val < 0) {
| 0 | ||||||||||||
| 444 | ret = 0; | - | ||||||||||||
| 445 | } else { never executed: end of block | 0 | ||||||||||||
| 446 | /* we assume that the transport protocol is UDP and no | - | ||||||||||||
| 447 | * IP options are used. | - | ||||||||||||
| 448 | */ | - | ||||||||||||
| 449 | data->mtu = sockopt_val - 8 - 20; | - | ||||||||||||
| 450 | ret = data->mtu; | - | ||||||||||||
| 451 | } never executed: end of block | 0 | ||||||||||||
| 452 | break; never executed: break; | 0 | ||||||||||||
| 453 | #if defined(IPV6_MTU) | - | ||||||||||||
| 454 | case AF_INET6: never executed: case 10 : | 0 | ||||||||||||
| 455 | ret = getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU, | - | ||||||||||||
| 456 | &sockopt_val, &sockopt_len); | - | ||||||||||||
| 457 | if (ret < 0 || sockopt_val < 0) {
| 0 | ||||||||||||
| 458 | ret = 0; | - | ||||||||||||
| 459 | } else { never executed: end of block | 0 | ||||||||||||
| 460 | /* we assume that the transport protocol is UDP and no | - | ||||||||||||
| 461 | * IPV6 options are used. | - | ||||||||||||
| 462 | */ | - | ||||||||||||
| 463 | data->mtu = sockopt_val - 8 - 40; | - | ||||||||||||
| 464 | ret = data->mtu; | - | ||||||||||||
| 465 | } never executed: end of block | 0 | ||||||||||||
| 466 | break; never executed: break; | 0 | ||||||||||||
| 467 | #endif | - | ||||||||||||
| 468 | default never executed: :default:never executed: default: | 0 | ||||||||||||
| 469 | ret = 0; | - | ||||||||||||
| 470 | break; never executed: break; | 0 | ||||||||||||
| 471 | } | - | ||||||||||||
| 472 | #else | - | ||||||||||||
| 473 | ret = 0; | - | ||||||||||||
| 474 | #endif | - | ||||||||||||
| 475 | break; never executed: break; | 0 | ||||||||||||
| 476 | case BIO_CTRL_DGRAM_GET_FALLBACK_MTU: never executed: case 47: | 0 | ||||||||||||
| 477 | switch (data->peer.sa.sa_family) { | - | ||||||||||||
| 478 | case AF_INET: never executed: case 2 : | 0 | ||||||||||||
| 479 | ret = 576 - 20 - 8; | - | ||||||||||||
| 480 | break; never executed: break; | 0 | ||||||||||||
| 481 | case AF_INET6: never executed: case 10 : | 0 | ||||||||||||
| 482 | #ifdef IN6_IS_ADDR_V4MAPPED | - | ||||||||||||
| 483 | if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
| 0 | ||||||||||||
| 484 | ret = 576 - 20 - 8; never executed: ret = 576 - 20 - 8; | 0 | ||||||||||||
| 485 | else | - | ||||||||||||
| 486 | #endif | - | ||||||||||||
| 487 | ret = 1280 - 40 - 8; never executed: ret = 1280 - 40 - 8; | 0 | ||||||||||||
| 488 | break; never executed: break; | 0 | ||||||||||||
| 489 | default: never executed: default: | 0 | ||||||||||||
| 490 | ret = 576 - 20 - 8; | - | ||||||||||||
| 491 | break; never executed: break; | 0 | ||||||||||||
| 492 | } | - | ||||||||||||
| 493 | break; never executed: break; | 0 | ||||||||||||
| 494 | case BIO_CTRL_DGRAM_GET_MTU: never executed: case 41: | 0 | ||||||||||||
| 495 | return data->mtu; never executed: return data->mtu; | 0 | ||||||||||||
| 496 | break; dead code: break; | - | ||||||||||||
| 497 | case BIO_CTRL_DGRAM_SET_MTU: never executed: case 42: | 0 | ||||||||||||
| 498 | data->mtu = num; | - | ||||||||||||
| 499 | ret = num; | - | ||||||||||||
| 500 | break; never executed: break; | 0 | ||||||||||||
| 501 | case BIO_CTRL_DGRAM_SET_CONNECTED: never executed: case 32: | 0 | ||||||||||||
| 502 | to = (struct sockaddr *)ptr; | - | ||||||||||||
| 503 | - | |||||||||||||
| 504 | if (to != NULL) {
| 0 | ||||||||||||
| 505 | data->connected = 1; | - | ||||||||||||
| 506 | switch (to->sa_family) { | - | ||||||||||||
| 507 | case AF_INET: never executed: case 2 : | 0 | ||||||||||||
| 508 | memcpy(&data->peer, to, sizeof(data->peer.sa_in)); | - | ||||||||||||
| 509 | break; never executed: break; | 0 | ||||||||||||
| 510 | case AF_INET6: never executed: case 10 : | 0 | ||||||||||||
| 511 | memcpy(&data->peer, to, sizeof(data->peer.sa_in6)); | - | ||||||||||||
| 512 | break; never executed: break; | 0 | ||||||||||||
| 513 | default: never executed: default: | 0 | ||||||||||||
| 514 | memcpy(&data->peer, to, sizeof(data->peer.sa)); | - | ||||||||||||
| 515 | break; never executed: break; | 0 | ||||||||||||
| 516 | } | - | ||||||||||||
| 517 | } else { | - | ||||||||||||
| 518 | data->connected = 0; | - | ||||||||||||
| 519 | memset(&(data->peer), 0, sizeof(data->peer)); | - | ||||||||||||
| 520 | } never executed: end of block | 0 | ||||||||||||
| 521 | break; never executed: break; | 0 | ||||||||||||
| 522 | case BIO_CTRL_DGRAM_GET_PEER: never executed: case 46: | 0 | ||||||||||||
| 523 | switch (data->peer.sa.sa_family) { | - | ||||||||||||
| 524 | case AF_INET: never executed: case 2 : | 0 | ||||||||||||
| 525 | ret = sizeof(data->peer.sa_in); | - | ||||||||||||
| 526 | break; never executed: break; | 0 | ||||||||||||
| 527 | case AF_INET6: never executed: case 10 : | 0 | ||||||||||||
| 528 | ret = sizeof(data->peer.sa_in6); | - | ||||||||||||
| 529 | break; never executed: break; | 0 | ||||||||||||
| 530 | default: never executed: default: | 0 | ||||||||||||
| 531 | ret = sizeof(data->peer.sa); | - | ||||||||||||
| 532 | break; never executed: break; | 0 | ||||||||||||
| 533 | } | - | ||||||||||||
| 534 | if (num == 0 || num > ret)
| 0 | ||||||||||||
| 535 | num = ret; never executed: num = ret; | 0 | ||||||||||||
| 536 | memcpy(ptr, &data->peer, (ret = num)); | - | ||||||||||||
| 537 | break; never executed: break; | 0 | ||||||||||||
| 538 | case BIO_CTRL_DGRAM_SET_PEER: never executed: case 44: | 0 | ||||||||||||
| 539 | to = (struct sockaddr *) ptr; | - | ||||||||||||
| 540 | switch (to->sa_family) { | - | ||||||||||||
| 541 | case AF_INET: never executed: case 2 : | 0 | ||||||||||||
| 542 | memcpy(&data->peer, to, sizeof(data->peer.sa_in)); | - | ||||||||||||
| 543 | break; never executed: break; | 0 | ||||||||||||
| 544 | case AF_INET6: never executed: case 10 : | 0 | ||||||||||||
| 545 | memcpy(&data->peer, to, sizeof(data->peer.sa_in6)); | - | ||||||||||||
| 546 | break; never executed: break; | 0 | ||||||||||||
| 547 | default: never executed: default: | 0 | ||||||||||||
| 548 | memcpy(&data->peer, to, sizeof(data->peer.sa)); | - | ||||||||||||
| 549 | break; never executed: break; | 0 | ||||||||||||
| 550 | } | - | ||||||||||||
| 551 | break; never executed: break; | 0 | ||||||||||||
| 552 | case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT: never executed: case 45: | 0 | ||||||||||||
| 553 | memcpy(&(data->next_timeout), ptr, sizeof(struct timeval)); | - | ||||||||||||
| 554 | break; never executed: break; | 0 | ||||||||||||
| 555 | #if defined(SO_RCVTIMEO) | - | ||||||||||||
| 556 | case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT: never executed: case 33: | 0 | ||||||||||||
| 557 | if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
| 0 | ||||||||||||
| 558 | sizeof(struct timeval)) < 0) {
| 0 | ||||||||||||
| 559 | perror("setsockopt"); | - | ||||||||||||
| 560 | ret = -1; | - | ||||||||||||
| 561 | } never executed: end of block | 0 | ||||||||||||
| 562 | break; never executed: break; | 0 | ||||||||||||
| 563 | case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT: never executed: case 34: | 0 | ||||||||||||
| 564 | { | - | ||||||||||||
| 565 | socklen_t sz = sizeof(struct timeval); | - | ||||||||||||
| 566 | if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
| 0 | ||||||||||||
| 567 | ptr, &sz) < 0) {
| 0 | ||||||||||||
| 568 | perror("getsockopt"); | - | ||||||||||||
| 569 | ret = -1; | - | ||||||||||||
| 570 | } else never executed: end of block | 0 | ||||||||||||
| 571 | ret = sz; never executed: ret = sz; | 0 | ||||||||||||
| 572 | } | - | ||||||||||||
| 573 | break; never executed: break; | 0 | ||||||||||||
| 574 | #endif | - | ||||||||||||
| 575 | #if defined(SO_SNDTIMEO) | - | ||||||||||||
| 576 | case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT: never executed: case 35: | 0 | ||||||||||||
| 577 | if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
| 0 | ||||||||||||
| 578 | sizeof(struct timeval)) < 0) {
| 0 | ||||||||||||
| 579 | perror("setsockopt"); | - | ||||||||||||
| 580 | ret = -1; | - | ||||||||||||
| 581 | } never executed: end of block | 0 | ||||||||||||
| 582 | break; never executed: break; | 0 | ||||||||||||
| 583 | case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT: never executed: case 36: | 0 | ||||||||||||
| 584 | { | - | ||||||||||||
| 585 | socklen_t sz = sizeof(struct timeval); | - | ||||||||||||
| 586 | if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
| 0 | ||||||||||||
| 587 | ptr, &sz) < 0) {
| 0 | ||||||||||||
| 588 | perror("getsockopt"); | - | ||||||||||||
| 589 | ret = -1; | - | ||||||||||||
| 590 | } else never executed: end of block | 0 | ||||||||||||
| 591 | ret = sz; never executed: ret = sz; | 0 | ||||||||||||
| 592 | } | - | ||||||||||||
| 593 | break; never executed: break; | 0 | ||||||||||||
| 594 | #endif | - | ||||||||||||
| 595 | case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP: never executed: case 38: | 0 | ||||||||||||
| 596 | /* fall-through */ | - | ||||||||||||
| 597 | case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP: never executed: case 37: | 0 | ||||||||||||
| 598 | if (data->_errno == EAGAIN) {
| 0 | ||||||||||||
| 599 | ret = 1; | - | ||||||||||||
| 600 | data->_errno = 0; | - | ||||||||||||
| 601 | } else never executed: end of block | 0 | ||||||||||||
| 602 | ret = 0; never executed: ret = 0; | 0 | ||||||||||||
| 603 | break; never executed: break; | 0 | ||||||||||||
| 604 | #ifdef EMSGSIZE | - | ||||||||||||
| 605 | case BIO_CTRL_DGRAM_MTU_EXCEEDED: never executed: case 43: | 0 | ||||||||||||
| 606 | if (data->_errno == EMSGSIZE) {
| 0 | ||||||||||||
| 607 | ret = 1; | - | ||||||||||||
| 608 | data->_errno = 0; | - | ||||||||||||
| 609 | } else never executed: end of block | 0 | ||||||||||||
| 610 | ret = 0; never executed: ret = 0; | 0 | ||||||||||||
| 611 | break; never executed: break; | 0 | ||||||||||||
| 612 | #endif | - | ||||||||||||
| 613 | default: never executed: default: | 0 | ||||||||||||
| 614 | ret = 0; | - | ||||||||||||
| 615 | break; never executed: break; | 0 | ||||||||||||
| 616 | } | - | ||||||||||||
| 617 | return (ret); never executed: return (ret); | 0 | ||||||||||||
| 618 | } | - | ||||||||||||
| 619 | - | |||||||||||||
| 620 | static int | - | ||||||||||||
| 621 | dgram_puts(BIO *bp, const char *str) | - | ||||||||||||
| 622 | { | - | ||||||||||||
| 623 | int n, ret; | - | ||||||||||||
| 624 | - | |||||||||||||
| 625 | n = strlen(str); | - | ||||||||||||
| 626 | ret = dgram_write(bp, str, n); | - | ||||||||||||
| 627 | return (ret); never executed: return (ret); | 0 | ||||||||||||
| 628 | } | - | ||||||||||||
| 629 | - | |||||||||||||
| 630 | - | |||||||||||||
| 631 | static int | - | ||||||||||||
| 632 | BIO_dgram_should_retry(int i) | - | ||||||||||||
| 633 | { | - | ||||||||||||
| 634 | int err; | - | ||||||||||||
| 635 | - | |||||||||||||
| 636 | if ((i == 0) || (i == -1)) {
| 0 | ||||||||||||
| 637 | err = errno; | - | ||||||||||||
| 638 | return (BIO_dgram_non_fatal_error(err)); never executed: return (BIO_dgram_non_fatal_error(err)); | 0 | ||||||||||||
| 639 | } | - | ||||||||||||
| 640 | return (0); never executed: return (0); | 0 | ||||||||||||
| 641 | } | - | ||||||||||||
| 642 | - | |||||||||||||
| 643 | int | - | ||||||||||||
| 644 | BIO_dgram_non_fatal_error(int err) | - | ||||||||||||
| 645 | { | - | ||||||||||||
| 646 | switch (err) { | - | ||||||||||||
| 647 | case EINTR: never executed: case 4 : | 0 | ||||||||||||
| 648 | case EAGAIN: never executed: case 11 : | 0 | ||||||||||||
| 649 | case EINPROGRESS: never executed: case 115 : | 0 | ||||||||||||
| 650 | case EALREADY: never executed: case 114 : | 0 | ||||||||||||
| 651 | return (1); never executed: return (1); | 0 | ||||||||||||
| 652 | default: never executed: default: | 0 | ||||||||||||
| 653 | break; never executed: break; | 0 | ||||||||||||
| 654 | } | - | ||||||||||||
| 655 | return (0); never executed: return (0); | 0 | ||||||||||||
| 656 | } | - | ||||||||||||
| 657 | - | |||||||||||||
| 658 | #endif | - | ||||||||||||
| Source code | Switch to Preprocessed file |