yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005-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 "e_os.h" |
| 11 | #include "internal/sockets.h" |
| 12 | #include "internal/refcount.h" |
| 13 | |
| 14 | /* BEGIN BIO_ADDRINFO/BIO_ADDR stuff. */ |
| 15 | |
| 16 | #ifndef OPENSSL_NO_SOCK |
| 17 | /* |
| 18 | * Throughout this file and b_addr.c, the existence of the macro |
| 19 | * AI_PASSIVE is used to detect the availability of struct addrinfo, |
| 20 | * getnameinfo() and getaddrinfo(). If that macro doesn't exist, |
| 21 | * we use our own implementation instead. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * It's imperative that these macros get defined before openssl/bio.h gets |
| 26 | * included. Otherwise, the AI_PASSIVE hack will not work properly. |
| 27 | * For clarity, we check for internal/cryptlib.h since it's a common header |
| 28 | * that also includes bio.h. |
| 29 | */ |
| 30 | # ifdef OSSL_INTERNAL_CRYPTLIB_H |
| 31 | # error internal/cryptlib.h included before bio_local.h |
| 32 | # endif |
| 33 | # ifdef HEADER_BIO_H |
| 34 | # error openssl/bio.h included before bio_local.h |
| 35 | # endif |
| 36 | |
| 37 | /* |
| 38 | * Undefine AF_UNIX on systems that define it but don't support it. |
| 39 | */ |
| 40 | # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_VMS) |
| 41 | # undef AF_UNIX |
| 42 | # endif |
| 43 | |
| 44 | # ifdef AI_PASSIVE |
| 45 | |
| 46 | /* |
| 47 | * There's a bug in VMS C header file netdb.h, where struct addrinfo |
| 48 | * always is the P32 variant, but the functions that handle that structure, |
| 49 | * such as getaddrinfo() and freeaddrinfo() adapt to the initial pointer |
| 50 | * size. The easiest workaround is to force struct addrinfo to be the |
| 51 | * 64-bit variant when compiling in P64 mode. |
| 52 | */ |
| 53 | # if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE == 64 |
| 54 | # define addrinfo __addrinfo64 |
| 55 | # endif |
| 56 | |
| 57 | # define bio_addrinfo_st addrinfo |
| 58 | # define bai_family ai_family |
| 59 | # define bai_socktype ai_socktype |
| 60 | # define bai_protocol ai_protocol |
| 61 | # define bai_addrlen ai_addrlen |
| 62 | # define bai_addr ai_addr |
| 63 | # define bai_next ai_next |
| 64 | # else |
| 65 | struct bio_addrinfo_st { |
| 66 | int bai_family; |
| 67 | int bai_socktype; |
| 68 | int bai_protocol; |
| 69 | size_t bai_addrlen; |
| 70 | struct sockaddr *bai_addr; |
| 71 | struct bio_addrinfo_st *bai_next; |
| 72 | }; |
| 73 | # endif |
| 74 | |
| 75 | union bio_addr_st { |
| 76 | struct sockaddr sa; |
| 77 | # ifdef AF_INET6 |
| 78 | struct sockaddr_in6 s_in6; |
| 79 | # endif |
| 80 | struct sockaddr_in s_in; |
| 81 | # ifdef AF_UNIX |
| 82 | struct sockaddr_un s_un; |
| 83 | # endif |
| 84 | }; |
| 85 | #endif |
| 86 | |
| 87 | /* END BIO_ADDRINFO/BIO_ADDR stuff. */ |
| 88 | |
| 89 | #include "internal/cryptlib.h" |
| 90 | #include "internal/bio.h" |
| 91 | |
| 92 | typedef struct bio_f_buffer_ctx_struct { |
| 93 | /*- |
| 94 | * Buffers are setup like this: |
| 95 | * |
| 96 | * <---------------------- size -----------------------> |
| 97 | * +---------------------------------------------------+ |
| 98 | * | consumed | remaining | free space | |
| 99 | * +---------------------------------------------------+ |
| 100 | * <-- off --><------- len -------> |
| 101 | */ |
| 102 | /*- BIO *bio; *//* |
| 103 | * this is now in the BIO struct |
| 104 | */ |
| 105 | int ibuf_size; /* how big is the input buffer */ |
| 106 | int obuf_size; /* how big is the output buffer */ |
| 107 | char *ibuf; /* the char array */ |
| 108 | int ibuf_len; /* how many bytes are in it */ |
| 109 | int ibuf_off; /* write/read offset */ |
| 110 | char *obuf; /* the char array */ |
| 111 | int obuf_len; /* how many bytes are in it */ |
| 112 | int obuf_off; /* write/read offset */ |
| 113 | } BIO_F_BUFFER_CTX; |
| 114 | |
| 115 | struct bio_st { |
| 116 | const BIO_METHOD *method; |
| 117 | /* bio, mode, argp, argi, argl, ret */ |
| 118 | BIO_callback_fn callback; |
| 119 | BIO_callback_fn_ex callback_ex; |
| 120 | char *cb_arg; /* first argument for the callback */ |
| 121 | int init; |
| 122 | int shutdown; |
| 123 | int flags; /* extra storage */ |
| 124 | int retry_reason; |
| 125 | int num; |
| 126 | void *ptr; |
| 127 | struct bio_st *next_bio; /* used by filter BIOs */ |
| 128 | struct bio_st *prev_bio; /* used by filter BIOs */ |
| 129 | CRYPTO_REF_COUNT references; |
| 130 | uint64_t num_read; |
| 131 | uint64_t num_write; |
| 132 | CRYPTO_EX_DATA ex_data; |
| 133 | CRYPTO_RWLOCK *lock; |
| 134 | }; |
| 135 | |
| 136 | #ifndef OPENSSL_NO_SOCK |
| 137 | # ifdef OPENSSL_SYS_VMS |
| 138 | typedef unsigned int socklen_t; |
| 139 | # endif |
| 140 | |
| 141 | extern CRYPTO_RWLOCK *bio_lookup_lock; |
| 142 | |
| 143 | int BIO_ADDR_make(BIO_ADDR *ap, const struct sockaddr *sa); |
| 144 | const struct sockaddr *BIO_ADDR_sockaddr(const BIO_ADDR *ap); |
| 145 | struct sockaddr *BIO_ADDR_sockaddr_noconst(BIO_ADDR *ap); |
| 146 | socklen_t BIO_ADDR_sockaddr_size(const BIO_ADDR *ap); |
| 147 | socklen_t BIO_ADDRINFO_sockaddr_size(const BIO_ADDRINFO *bai); |
| 148 | const struct sockaddr *BIO_ADDRINFO_sockaddr(const BIO_ADDRINFO *bai); |
| 149 | #endif |
| 150 | |
| 151 | extern CRYPTO_RWLOCK *bio_type_lock; |
| 152 | |
| 153 | void bio_sock_cleanup_int(void); |
| 154 | |
| 155 | #if BIO_FLAGS_UPLINK==0 |
| 156 | /* Shortcut UPLINK calls on most platforms... */ |
| 157 | # define UP_stdin stdin |
| 158 | # define UP_stdout stdout |
| 159 | # define UP_stderr stderr |
| 160 | # define UP_fprintf fprintf |
| 161 | # define UP_fgets fgets |
| 162 | # define UP_fread fread |
| 163 | # define UP_fwrite fwrite |
| 164 | # undef UP_fsetmod |
| 165 | # define UP_feof feof |
| 166 | # define UP_fclose fclose |
| 167 | |
| 168 | # define UP_fopen fopen |
| 169 | # define UP_fseek fseek |
| 170 | # define UP_ftell ftell |
| 171 | # define UP_fflush fflush |
| 172 | # define UP_ferror ferror |
| 173 | # ifdef _WIN32 |
| 174 | # define UP_fileno _fileno |
| 175 | # define UP_open _open |
| 176 | # define UP_read _read |
| 177 | # define UP_write _write |
| 178 | # define UP_lseek _lseek |
| 179 | # define UP_close _close |
| 180 | # else |
| 181 | # define UP_fileno fileno |
| 182 | # define UP_open open |
| 183 | # define UP_read read |
| 184 | # define UP_write write |
| 185 | # define UP_lseek lseek |
| 186 | # define UP_close close |
| 187 | # endif |
| 188 | |
| 189 | #endif |
| 190 | |