17 of BIO series of openssl --- Connection Type BIO and openssl17 ---
Connect Type BIO
--- Based on openssl doc \ crypto \ bio_s_connect.pod translation and your own understanding, write
(Author: DragonKing, Mail: wzhah@263.net, published on: http://gdwzh.126.com o
Penssl Professional Forum)
This type of BIO encapsulates the socket Connect method, which enables Unified BIO rules to be used during programming.
Connect the socket and send and accept the data.
The difference between connect methods. The related functions are defined 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, (c
Har *) 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, (c
Har *) 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 BIO_METHOD structure of the connect type, 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.
Maintain the address and status information of the underlying socket. However, through encapsulation, we generally do not need to directly contact
For more information about the structure, see the definition and function in bss_conn.c.
.
The BIO_read and BIO_write operations are completed by calling the underlying connection IO operations. If
If the port is set correctly, but the read/write operation function is called when the connection is not established, the connection will be established first.
And then perform read/write operations.
BIO_puts operations are supported, but BIO_gets operations are not supported.
It can be seen from the definition.
If the flag is disabled, any active connection and socket will be closed when BIO is released.
.
When the BIO_reset method is called, any active connections of BIO of the connection type will be closed.
And then return to the status where you can re-establish a connection with the same host.
BIO_get_fd function returns the underlying socket of BIO of the connection type. When the parameter c is not NULL
This socket is assigned to c. Of course, socket is also returned. The c parameter should be int * type. If BIO is not
-1 is returned.
[BIO_set_conn_hostname]
This function uses a string to set the host name. The host name can also be in the form of an IP address. It can also include a port.
Such as hostname: port, hostname/any/other/path and hostname: port/any/other/path
Yes. Returns 1.
[BIO_set_conn_port]
This function sets the host port number. The port number can be a number or a string
Similar to http. If a string is used, the getservbyname function is used to search for related terminals.
If no port is found, a default name port interpretation table is used. The characters listed in this table are currently used.
Strings include http, telnet, socks, https, ssl, ftp, gopher, and wais. 1 is returned.
Note that if the port name has been set as part of the host name, it will overwrite BIO _
The port value set by the set_conn_port function. Sometimes (for example, some applications may not want to use a fixed port to connect
) May be inconvenient. In this case, you can check the ":" character in the string that inputs the host name, and report an error or truncation character.
String to avoid this situation.
[BIO_set_conn_ip]
This function sets the IP address in binary mode. Returns 1.
[BIO_set_conn_int_port]
This function sets the host port number as an integer. The parameter 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 is initialized but the host name is not set
Returns NULL. The returned value is an internal pointer and cannot be changed.
[BIO_get_conn_port]
This function returns port information of the string type. If no value is set, NULL is returned.
[BIO_get_conn_ip]
This function returns an IP address in binary format. If no value is set, return 0.
[BIO_get_conn_int_port]
This function returns the port number in integer format. If no value is set, 0 is returned.
The return values of the above four functions will be updated after the connection operation is complete. Before that, the returned values must be
Set it by the program.
[BIO_set_nbio]
Set non-blocking flag for I/O. If n is 0, I/O is set to blocking mode. If n is 1, I/O is set
Set to non-blocking mode. The default mode is the blocking mode. This function should be called before the connection is established because it is not blocked.
The I/O of the plug-in mode is set during the connection process. The return value is always 1.
Note:
If it is an I/O in blocking mode, When I/O operations are performed (such as reading and writing), if negative values are returned, an error is generated.
If the returned value is 0, the connection is closed.
If it is set to non-blocking mode, it is normal to send a retry request.
[BIO_do_connect]
This function performs the connection operation for the given BIO. If the connection is successful, 1 is returned. Otherwise, 0 or a negative value is returned. In non-blocking
If the call fails in the congestion mode, you can call the BIO_should_retry function to determine whether to retry.
.
In general, applications do not need to call this function. They only need
This function is called only when it is independent.
When initializing the connection, if the cause of the returned value failure is BIO_RR_CONNECT, call BIO _
The returned value of should_io_special may also be true. In this case, the connection process is blocked.
, The application should use the normal method for processing until the underlying socket connection is connected and then try again.
[BIO_new_connect]
This function is created and returns a Connection Type BIO. In fact, it calls BIO_s_connect, BIO_new, and
The whole operation is completed by the BIO_set_conn_hostname function. If the operation succeeds, a BIO is returned. Otherwise, NULL is returned.
[Example]
This is an example of connecting to a local Web server. A page of information is returned and copied to the 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 );
/* Whatever ...*/
}
BIO_puts (cbio, "GET/HTTP/1.0 \ n ");
For (;;){
Len = BIO_read (cbio, tmpbuf, 1024 );
If (len <= 0) break;
BIO_write (out, tmpbuf, len );
}
BIO_free (cbio );
BIO_free (out );