bio_lcl.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright 2005-2016 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. #include "internal/refcount.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 HEADER_CRYPTLIB_H
  27. # error internal/cryptlib.h included before bio_lcl.h
  28. # endif
  29. # ifdef HEADER_BIO_H
  30. # error openssl/bio.h included before bio_lcl.h
  31. # endif
  32. /*
  33. * Undefine AF_UNIX on systems that define it but don't support it.
  34. */
  35. # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_VMS)
  36. # undef AF_UNIX
  37. # endif
  38. # ifdef AI_PASSIVE
  39. /*
  40. * There's a bug in VMS C header file netdb.h, where struct addrinfo
  41. * always is the P32 variant, but the functions that handle that structure,
  42. * such as getaddrinfo() and freeaddrinfo() adapt to the initial pointer
  43. * size. The easiest workaround is to force struct addrinfo to be the
  44. * 64-bit variant when compiling in P64 mode.
  45. */
  46. # if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE == 64
  47. # define addrinfo __addrinfo64
  48. # endif
  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. union bio_addr_st {
  67. struct sockaddr sa;
  68. # ifdef AF_INET6
  69. struct sockaddr_in6 s_in6;
  70. # endif
  71. struct sockaddr_in s_in;
  72. # ifdef AF_UNIX
  73. struct sockaddr_un s_un;
  74. # endif
  75. };
  76. #endif
  77. /* END BIO_ADDRINFO/BIO_ADDR stuff. */
  78. #include "internal/cryptlib.h"
  79. #include "internal/bio.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. const BIO_METHOD *method;
  104. /* bio, mode, argp, argi, argl, ret */
  105. BIO_callback_fn callback;
  106. BIO_callback_fn_ex callback_ex;
  107. char *cb_arg; /* first argument for the callback */
  108. int init;
  109. int shutdown;
  110. int flags; /* extra storage */
  111. int retry_reason;
  112. int num;
  113. void *ptr;
  114. struct bio_st *next_bio; /* used by filter BIOs */
  115. struct bio_st *prev_bio; /* used by filter BIOs */
  116. CRYPTO_REF_COUNT references;
  117. uint64_t num_read;
  118. uint64_t num_write;
  119. CRYPTO_EX_DATA ex_data;
  120. CRYPTO_RWLOCK *lock;
  121. };
  122. #ifndef OPENSSL_NO_SOCK
  123. # ifdef OPENSSL_SYS_VMS
  124. typedef unsigned int socklen_t;
  125. # endif
  126. extern CRYPTO_RWLOCK *bio_lookup_lock;
  127. int BIO_ADDR_make(BIO_ADDR *ap, const struct sockaddr *sa);
  128. const struct sockaddr *BIO_ADDR_sockaddr(const BIO_ADDR *ap);
  129. struct sockaddr *BIO_ADDR_sockaddr_noconst(BIO_ADDR *ap);
  130. socklen_t BIO_ADDR_sockaddr_size(const BIO_ADDR *ap);
  131. socklen_t BIO_ADDRINFO_sockaddr_size(const BIO_ADDRINFO *bai);
  132. const struct sockaddr *BIO_ADDRINFO_sockaddr(const BIO_ADDRINFO *bai);
  133. #endif
  134. extern CRYPTO_RWLOCK *bio_type_lock;
  135. void bio_sock_cleanup_int(void);
  136. #if BIO_FLAGS_UPLINK==0
  137. /* Shortcut UPLINK calls on most platforms... */
  138. # define UP_stdin stdin
  139. # define UP_stdout stdout
  140. # define UP_stderr stderr
  141. # define UP_fprintf fprintf
  142. # define UP_fgets fgets
  143. # define UP_fread fread
  144. # define UP_fwrite fwrite
  145. # undef UP_fsetmod
  146. # define UP_feof feof
  147. # define UP_fclose fclose
  148. # define UP_fopen fopen
  149. # define UP_fseek fseek
  150. # define UP_ftell ftell
  151. # define UP_fflush fflush
  152. # define UP_ferror ferror
  153. # ifdef _WIN32
  154. # define UP_fileno _fileno
  155. # define UP_open _open
  156. # define UP_read _read
  157. # define UP_write _write
  158. # define UP_lseek _lseek
  159. # define UP_close _close
  160. # else
  161. # define UP_fileno fileno
  162. # define UP_open open
  163. # define UP_read read
  164. # define UP_write write
  165. # define UP_lseek lseek
  166. # define UP_close close
  167. # endif
  168. #endif