bio_local.h 5.6 KB

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