Read and write error control
--- Translation according to OpenSSL DOC / CRYPTO / BIO / BIO_SHOULD_RETRY.POD translation and its own understanding
(Author: DragonKing Mail: wzhah@263.net Posted: http: //gdwzh.126.com of openssl professional forum)
When the Bio_Read or Bio_Write function calls an error, BIO itself provides a set of diagnostic functions for an error reason, which is as follows (openssl / bio.h):
#define bio_SHOULD_READ (A) ((a) -> Flags & Bio_Flags_Read)
#define bio_should_write (a) ((a) -> Flags & Bio_Flags_Write)
#define bio_should_io_special (a) ((a) -> Flags & Bio_Flags_io_Special)
#define bio_retry_type (a) ((a) -> flags & bio_flags_rws)
#define bio_should_retry (a) ((a) -> Flags & Bio_Flags_Shold_Retry)
#define bio_flags_read 0x01
#define bio_flags_write 0x02
#define bio_flags_io_special 0x04
#define bio_flags_rws (Bio_Flags_Read | BIO_FLAGS_WRITE | BIO_FLAGS_IO_SPECIAL)
#define bio_flags_should_retry 0x08
BIO * BIO_GET_RETRY_BIO (BIO * BIO, INT * REASON);
INT BIO_GET_RETRY_REASON (BIO * BIO);
Since these functions are used to determine why BIO cannot read or write data when reading and writing data, they are typically called after executing BIO_READ or BIO_WRITE operations.
[BIO_SHOULD_RETRY]
If the situation of reading and writing is required to try again later, then the function returns true. If the function returns false, it is determined that the error is determined according to the BIO's type and the return value of the BIO operation. For example, if the BIO_READ operation is called for the socket type, the return value is 0, and the BIO_SHOULD_RETRY returns FALSE indicates that the socket connection has been turned off. And if it is a File type Bio, the description is to read the file EOF. Some types of BIO also provide more error information, and details are given.
If the underlying I / O structure of the BIO is blocking mode, almost all (SSL type BIO exceptions) BIO type does not return to retry, it does not return to TRUE, because this time I / O The call is not taken at all. So it is recommended that if your application can determine that the type BIO does not have a retry when performing the IO operation, do not call the BIO_SHOULD_RETRY function. File type BIO is a typical example of this.
The SSL type BIO is the only exception to the above rules, that is, it is in the blocking I / O structure, if the process of shaking hands when calling Bio_read, it also returns a retry request (call BIO_SHOULD_RETRY Returns true). In this case, the application can re-execute the failing I / O operation, or set to SSL_MODE_AUTO_RETRY in the I / O structure of the underlying, then this failure can be avoided. If the application retry after the IO operation fails in the non-blocking BIO, it may result in a low efficiency, because the call repeatedly returned the failure result before the data allowed read or valid. Therefore, the normal application should be after the required conditions, the program performs related calls, as for how to do it, it is related to the underlying IO structure. For example, if a base layer IO is a SOKET, and Bio_Shold_Retry returns True, you can call SELECT () to wait for the data to be validated and then retry the IO operation. In a thread, you can use a select () to handle multiple non-blocking BIOs, however, the performance efficiency may occur very low, such as if one of the delayed SSL type BIO is in handshake This will result in this situation.
In blocking type IO structures, the reading operation of the data may result in an indefinite blocking, which is related to the IO structure function of the system. We certainly do not expect this situation, one of the solutions is to set the waiting time using the non-blocking IO structure and use the SELECT function (or equivalent).
[BIO_SHOULD_READ]
This function returns True If the reason for the failure of the IO operation is BIO to read data at this time.
[BIO_SHOULD_WRITE]
This function returns True if the reason for the failure of the IO operation is BIO to write data.
[BIO_SHOULD_IO_SPECIAL]
This function returns True If the reason for the failure of the IO operation is special (that is, the reason for reading and writing)
[BIO_GET_RETRY_REASON]
Returns the reason why the code includes BIO_FLAGS_READ, BIO_FLAGS_WRITE, and BIO_FLAGS_IO_SPECIAL. The current BIO type only returns one of them. If the input BIO is a Bio that generates a special error condition, then the function returns the cause of the error, just like the REAON returned by Bio_GET_RETRY_BIO ().
[BIO_GET_RETRY_BIO]
This function gives a short reason for special circumstances. It returns an error Bio. If REASON is not set to NULL, it will contain error code, error code, and the processing measures that the next step should take should be in this case. Type of Bio.