bio_local.h 5.4 KB

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