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