bio_local.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #include "internal/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. # ifdef AI_PASSIVE
  32. /*
  33. * There's a bug in VMS C header file netdb.h, where struct addrinfo
  34. * always is the P32 variant, but the functions that handle that structure,
  35. * such as getaddrinfo() and freeaddrinfo() adapt to the initial pointer
  36. * size. The easiest workaround is to force struct addrinfo to be the
  37. * 64-bit variant when compiling in P64 mode.
  38. */
  39. # if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE == 64
  40. # define addrinfo __addrinfo64
  41. # endif
  42. # define bio_addrinfo_st addrinfo
  43. # define bai_family ai_family
  44. # define bai_socktype ai_socktype
  45. # define bai_protocol ai_protocol
  46. # define bai_addrlen ai_addrlen
  47. # define bai_addr ai_addr
  48. # define bai_next ai_next
  49. # else
  50. struct bio_addrinfo_st {
  51. int bai_family;
  52. int bai_socktype;
  53. int bai_protocol;
  54. size_t bai_addrlen;
  55. struct sockaddr *bai_addr;
  56. struct bio_addrinfo_st *bai_next;
  57. };
  58. # endif
  59. union bio_addr_st {
  60. struct sockaddr sa;
  61. # if OPENSSL_USE_IPV6
  62. struct sockaddr_in6 s_in6;
  63. # endif
  64. struct sockaddr_in s_in;
  65. # ifndef OPENSSL_NO_UNIX_SOCK
  66. struct sockaddr_un s_un;
  67. # endif
  68. };
  69. #endif
  70. /* END BIO_ADDRINFO/BIO_ADDR stuff. */
  71. #include "internal/cryptlib.h"
  72. #include "internal/bio.h"
  73. #include "internal/refcount.h"
  74. typedef struct bio_f_buffer_ctx_struct {
  75. /*-
  76. * Buffers are setup like this:
  77. *
  78. * <---------------------- size ----------------------->
  79. * +---------------------------------------------------+
  80. * | consumed | remaining | free space |
  81. * +---------------------------------------------------+
  82. * <-- off --><------- len ------->
  83. */
  84. /*- BIO *bio; *//*
  85. * this is now in the BIO struct
  86. */
  87. int ibuf_size; /* how big is the input buffer */
  88. int obuf_size; /* how big is the output buffer */
  89. char *ibuf; /* the char array */
  90. int ibuf_len; /* how many bytes are in it */
  91. int ibuf_off; /* write/read offset */
  92. char *obuf; /* the char array */
  93. int obuf_len; /* how many bytes are in it */
  94. int obuf_off; /* write/read offset */
  95. } BIO_F_BUFFER_CTX;
  96. struct bio_st {
  97. OSSL_LIB_CTX *libctx;
  98. const BIO_METHOD *method;
  99. /* bio, mode, argp, argi, argl, ret */
  100. #ifndef OPENSSL_NO_DEPRECATED_3_0
  101. BIO_callback_fn callback;
  102. #endif
  103. BIO_callback_fn_ex callback_ex;
  104. char *cb_arg; /* first argument for the callback */
  105. int init;
  106. int shutdown;
  107. int flags; /* extra storage */
  108. int retry_reason;
  109. int num;
  110. void *ptr;
  111. struct bio_st *next_bio; /* used by filter BIOs */
  112. struct bio_st *prev_bio; /* used by filter BIOs */
  113. CRYPTO_REF_COUNT references;
  114. uint64_t num_read;
  115. uint64_t num_write;
  116. CRYPTO_EX_DATA ex_data;
  117. CRYPTO_RWLOCK *lock;
  118. };
  119. #ifndef OPENSSL_NO_SOCK
  120. # ifdef OPENSSL_SYS_VMS
  121. typedef unsigned int socklen_t;
  122. # endif
  123. extern CRYPTO_RWLOCK *bio_lookup_lock;
  124. int BIO_ADDR_make(BIO_ADDR *ap, const struct sockaddr *sa);
  125. const struct sockaddr *BIO_ADDR_sockaddr(const BIO_ADDR *ap);
  126. struct sockaddr *BIO_ADDR_sockaddr_noconst(BIO_ADDR *ap);
  127. socklen_t BIO_ADDR_sockaddr_size(const BIO_ADDR *ap);
  128. socklen_t BIO_ADDRINFO_sockaddr_size(const BIO_ADDRINFO *bai);
  129. const struct sockaddr *BIO_ADDRINFO_sockaddr(const BIO_ADDRINFO *bai);
  130. #endif
  131. extern CRYPTO_RWLOCK *bio_type_lock;
  132. void bio_sock_cleanup_int(void);
  133. #if BIO_FLAGS_UPLINK_INTERNAL==0
  134. /* Shortcut UPLINK calls on most platforms... */
  135. # define UP_stdin stdin
  136. # define UP_stdout stdout
  137. # define UP_stderr stderr
  138. # define UP_fprintf fprintf
  139. # define UP_fgets fgets
  140. # define UP_fread fread
  141. # define UP_fwrite fwrite
  142. # undef UP_fsetmod
  143. # define UP_feof feof
  144. # define UP_fclose fclose
  145. # define UP_fopen fopen
  146. # define UP_fseek fseek
  147. # define UP_ftell ftell
  148. # define UP_fflush fflush
  149. # define UP_ferror ferror
  150. # ifdef _WIN32
  151. # define UP_fileno _fileno
  152. # define UP_open _open
  153. # define UP_read _read
  154. # define UP_write _write
  155. # define UP_lseek _lseek
  156. # define UP_close _close
  157. # else
  158. # define UP_fileno fileno
  159. # define UP_open open
  160. # define UP_read read
  161. # define UP_write write
  162. # define UP_lseek lseek
  163. # define UP_close close
  164. # endif
  165. #endif