Connection (Connect) type BIO
--- Translation according to OpenSSL DOC / CRYPTO / BIO_S_CONNECT.POD and its own understanding
(Author: DragonKing, Mail: wzhah@263.net, Posted: httpgdwzh.126.com the openssl professional forum)
This type of BIO encapsulates the Socket's Connect method that allows you to use a unified BIO rule to send acceptance and data transmission accepted without care for the difference between the Connect method of the concrete platform. Some of the functions of its related definitions are as follows (OpenSSL / BIO.H):
BIO_METHOD * BIO_S_CONNECT (VOID);
#define bio_set_conn_hostname (b, name) Bio_Ctrl (B, Bio_C_SET_CONNECT, 0, (CHAR *) NAME)
#define bio_set_conn_port (b, port) Bio_ctrl (b, bio_c_set_connect, 1, (char *) port)
#define bio_set_conn_ip (b, ip) BIO_CTRL (B, BIO_C_SET_CONNECT, 2, (CHAR *) IP)
#define bio_set_conn_int_port (b, port) BIO_CTRL (B, BIO_C_SET_CONNECT, 3, (CHAR *) Port)
#define bio_get_conn_hostname (b) BIO_PTR_CTRL (B, BIO_C_GET_CONNECT, 0)
#define bio_get_conn_port (b) bio_ptr_ctrl (B, BIO_C_GET_CONNECT, 1)
#define bio_get_conn_ip (b, ip) BIO_PTR_CTRL (B, BIO_C_SET_CONNECT, 2)
#define bio_GET_CONN_INT_PORT (B, Port) BIO_INT_CTRL (B, BIO_C_SET_CONNECT, 3, PORT)
#define bio_set_nbio (b, n) Bio_ctrl (B, Bio_C_SET_NBIO, (N), NULL)
#define bio_do_connect (b) BIO_DO_HANDSHAKE (B)
BIO * BIO_NEW_CONNECT (Char * STR)
[BIO_S_CONNECT]
This function returns a CONNECT type BIO_METHOD structure, which is defined as follows:
Static Bio_Method Methods_ConnectP =
{
BIO_TYPE_CONNECT,
"Socket Connect",
CONN_WRITE,
CONN_READ,
CONN_PUTS,
NULL, / * Connect_gets, * /
CONN_CTRL,
CONN_NEW,
CONN_FREE,
CONN_CALLBACK_CTRL,
}
In fact, in order to maintain a socket structure, OpenSSL also defines a BIO_CONNECT structure to maintain the address information of the underlying socket and status information. However, by packaging, we generally do not have to touch the structure directly, there is no more To introduce, you can see the definitions and functions inside the file BSS_CONN.C.
The operation of BIO_READ and BIO_WRITE calls the IO operation of the underlying connection. If the server address and port are set correctly, the read and write operation function is called when the connection is not established, then the connection operation will be performed first, and then read and write operation.
The BIO_PUTS operation is supported, but the BIO_GETS operation does not support, which can be seen in the definition of this type BIO_Method structure. If the closing flag is set, any activity connection and socket will be turned off when the Bio is released.
When the BIO_RESET method is called, any active connection of the Connect type BIO will be turned off, thereby returning to the status of the same host to establish a connection.
The BIO_GET_FD function returns the bottom Socket of the BIO of the connection type. When the parameter C is not null, the Socket assigns C, of course, the socket also acts as a return value. The C parameter should be an int * type. Returns -1 if the BIO is not initialized.
[BIO_SET_CONN_HOSTNAME]
This function uses a string to set the host name, which can also be in the form of an IP address, as well as port numbers, such as Hostname: port, hostname / any / other / path and hostname: port / any / other / path can also of. Returns 1.
[BIO_SET_CONN_PORT]
This function sets the port number of the host. The port number can be in the form of a number or a string similar to "http". If you use a string form, you will first use the GetServByname function to search its related port. If you have not searched, then a default name port interpretation table is used, and the string listed in the table is: http, telnet, SOCKS, HTTPS, SSL, FTP, GOPHER and WAIS. Returns 1.
It should be noted that if the port name has been set as part of the host name, then it will override the port value set by the BIO_SET_CONN_PORT function. Sometimes (if some applications may not want to use a fixed port connection) may be inconvenient, this time you can avoid this by detecting the ":" character, error or intercept string in the string of the hostname.
[BIO_SET_CONN_IP]
This function sets the IP address using a binary mode. Returns 1.
[BIO_SET_CONN_INT_PORT]
This function sets the host port number in an integer, and the parameters should be in the form of int *. Returns 1.
[BIO_GET_CONN_HOSTNAME]
This function returns the host name of the connection type BIO, if BIO, and initialization, but no host name is set, then return NULL. The return value is because it is an internal pointer, all cannot change its value.
[BIO_GET_CONN_PORT]
This function returns port information for string type. Returns NULL if not setup.
[BIO_GET_CONN_IP]
This function returns the IP address of the binary form. If not set, return to all 0.
[BIO_GET_CONN_INT_PORT]
This function returns the port number in the form of an integer. If it is not set, it returns 0.
The return value of the above four functions will be updated after the connection operation is completed. And before this, the return value is the application itself.
[BIO_SET_NBIO]
Set I / O Non-Block Sign. If the parameter n is 0, the I / O is set to block mode; if n is 1, I / O is set to a non-blocking mode. The default mode is blocking mode. This function should be called before the connection is established because the I / O of the non-blocking mode is set during the connection. Return to 1.
note:
If it is an I / O of the blocking mode, when performing IO operation (such as reading), if a negative value is returned, the description has generated an error, and if the return value is 0, it is generally turned off.
If set to non-blocking mode, the request to make a retry is normal. [BIO_DO_CONNECT]
This function performs a given BIO connection operation, if the connection is successful, returns 1, otherwise returns 0 or negative values. When the non-blocking mode, if the call fails, you can call the BIO_SHOULD_RETRY function to determine if you need to try again.
In general, the application does not need to call this function, and this function needs to be called only when it is desired to independently with other IO processing.
When the process of initializing the connection, if the return value fails is BIO_RR_CONNECT, calling the BIO_SHOULD_IO_SPECIAL return value may also be True. If this happens, the connection process is blocked, and the application should be processed using the normal method until the bottom Socket connection is retest.
[BIO_NEW_CONNECT]
This function creates and returns a connection type Bio, in fact, it calls BIO_S_CONNECT, BIO_NEW already bio_set_conn_hostname function completes the entire action. Success returns a Bio, otherwise returns NULL.
【example】
This is an example of connecting to a local web server, returning to a page and copys the information to a standard output device.
BIO * CBIO, * OUT
Int Len;
Char tmpbuf [1024];
ERR_LOAD_CRYPTO_STRINGS ();
CBIO = BIO_NEW_CONNECT ("LocalHost: http");
OUT = BIO_NEW_FP (stdout, bio_noclose);
IF (BIO_DO_CONNECT (CBIO) <= 0) {
FPRINTF (stderr, "error connecting to server / n");
ERR_PRINT_ERRORS_FP (STDERR);
/ * whatver ... * /
}
Bio_PUTS (CBIO, "Get / http / 1.0 / n / n");
For (;;) {
Len = BIO_READ (CBIO, TMPBUF, 1024);
IF (len <= 0) Break;
BIO_WRITE (OUT, TMPBUF, LEN);
}
BIO_FREE (CBI);
BIO_FREE (OUT);