OpenSSL BIO Series 18 --- Accept (Accept) Type BIO

zhaozj2021-02-16  49

Acceity (Accept) type BIO

--- Translation according to OpenSSL DOC / CRYPTO / BIO_S_ACCEPT.POD and its understanding

(Author: DragonKing, Mail: wzhah@263.net, Posted on: http: //openssl.126.com of openssl professional forum)

Accept (Accept) type BIO The connection (Connect) type BIO is corresponding, it encapsulates the Accept method of the socket and some of its related operations, enabling the same function to use the same function to different platforms. The relevant functions of its definition are as follows (OpenSSL / BIO.H):

BIO_METHOD * BIO_S_ACCEPT (VOID);

#define bio_set_accept_port (b, name) BIO_CTRL (B, BIO_C_SET_AMCEPT, 0, (CHAR *) NAME)

#define bio_get_accept_port (b) Bio_Ptr_Ctrl (B, BIO_C_GET_ACCEPT, 0)

BIO * BIO_NEW_ACCEPT (Char * Host_Port);

#define bio_set_nbio_accept (b, n) Bio_ctrl (B, BIO_C_SET_ACCEPT, 1, (N)? "a": null)

#define bio_set_accept_bios (b, bio) BIO_CTRL (B, BIO_C_SET_ACCEPT, 2, (CHAR *) BIO)

#define bio_set_bind_mode (b, mode) BIO_CTRL (B, BIO_C_SET_BIND_MODE, MODE, NULL)

#define bio_get_bind_mode (b, mode) BIO_CTRL (B, BIO_C_GET_BIND_MODE, 0, NULL)

#define bio_bind_normal 0

#define bioad_reuseaddr_if_unused 1

#define bioad_reuseaddr 2

#define bio_do_accept (b) BIO_DO_HANDSHAKE (B)

[BIO_S_ACCEPT]

This function returns an accept type Bio_Method structure that is defined as follows:

Static Bio_Method Methods_Acceptp =

{

BIO_TYPE_ACCEPT,

"Socket Accept",

ACPT_WRITE,

ACPT_READ,

ACPT_PUTS,

NULL, / * Connect_gets, * /

ACPT_CTRL,

ACPT_NEW,

ACPT_FREE,

NULL,

}

Through this structure, we can know what I / O operations can be supported at a glance. In this type, the operation of Bio_GETS is not supported. Like the connection (Connect) type BIO, OpenSSL also defines a structure that maintains the Socket information, and I will not introduce this structure in detail, and everyone refers to the BSS_ACPT.c file.

This type of BIO is packaged for the TCP / IP of various platforms, so you can operate the same BIO rule when you use, without worrying about the work of procedures or increasing transplant because of different platforms. Quantity, this is also a very important purpose of BIO.

The BIO_READ and BIO_WRITE functions call the I / O related operation of the underlying platform, if there is no connection to establish, the port is set correctly, then the BIO will wait for the establishment of the connection. In fact, when a connection is established, a new Socket type BIO will be created and attached to the BIO chain, forming the BIO structure of Accept-> Socket, so this time the IO operation of the initialized Accepting Socket Resulting in a state in which it is waiting to be established. When a receiving type BIO is at the end of the Bio chain, it will wait for a connection to create before processing I / O call; if it is not at the end, it simply transmits I / O call to the next Bio. If the closing flag of the Accept type BIO is set, any activity of the BIO chain is turned off when the BIO is released.

BIO_GET_FD and BIO_SET_FD can be used to obtain the Socket descriptor of the connection, detailed, "12 --- Descriptor (FD) type BIO" in the BIO series.

[BIO_SET_ACCEPT_PORT]

This function uses the string name to set the accept port. The form is "Host: Port", "Host" is the interface to use, "Port" is the port. Both parts can be "*", which indicates that any interface and port can be used. The string meaning of the port here is the same as the Connect type BIO, which can be in digital form. You can use the getServByName function to match, you can also use the table of the string, see the relevant part of the connection BIO instructions. .

[BIO_NEW_ACCEPT]

This function places the BIO_NEW and BIO_SET_ACCEPT_PORT functions in a function to create a new Accept type Bio.

[BIO_SET_NBIO_ACCEPT]

This function is set to accept whether the socket is blocking mode (default), if the parameter N is 0, is a blocking mode, and N is a non-blocking mode.

[BIO_SET_ACCEPT_BIOS]

This function is used to set a BIO chain. When a connection is accepted, this setup Bio chain will be copied or attached to the entire BIO chain. Sometimes this treatment method is very good, for example, if each connection requires a buffer or SSL type BIO, this time is much troublesome. It should be noted that after calling this function, the BIO on this setting cannot be released by its own, and after accepting the accept BIO, they will be released automatically.

When the function is called, the BIO chain set is between the BIOs that accept the BIO and socket types, that is, the new Bio chain of the ACCEPT-> Otherbios-> Socket.

[BIO_SET_BIND_MODE and BIO_GET_BIND_MODE]

These two functions are used to set and achieve the current binding mode. If set to bio_bind_normal (default), another Socket cannot be bound to the same port. If set to bio_bind_reuseaddr, the additional socket can be bound to the same port. If set to bio_bind_reuseaddr_if_unused, then you will try to bind to ports with Bio_bind_normal mode, and if the port is not used, then Bio_Bind_reuseADDR mode is bound to port.

[BIO_DO_ACCEPT]

This function has two functions, when it is called after accepting (Accept) BIO, it will create a socket and bind it to the address; when it is called, it will wait The arrival arrival. 【note】

If the server is desired to handle multiple connections (usually this), then accepting the BIO must be able to handle the following connection, this time you can do this:

Waiting for a connection, call

Connection = BIO_POP (Accept)

Thus, the CNNECTION will contain a recently established Bio, and Accept once again becomes a separate Bio, which can be used to handle other connections. If there is no other connection to establish, you can use Bio_Free to release the BIO.

Of course, if only one connection needs to be processed, you can use the connection BIO itself to perform I / O operation. However, it is generally not recommended because this reception BIO is still in a state in which other connection is established. This can be solved using the above method.

【example】

This example accepts two connections at the 4444 port, sending information to each connection and release them.

BIO * ABIO, * CBIO, * CBIO2;

ERR_LOAD_CRYPTO_STRINGS ();

ABIO = BIO_NEW_ACCEPT ("4444");

/ * First call BIO_ACCEPT start-up accepting BIO * /

IF (Bio_DO_ACCEPT (ABIO) <= 0) {

FPRINTF (stderr, "error setting up accept / n");

ERR_PRINT_ERRORS_FP (STDERR);

exit (0);

}

/ * Wait to establish * /

IF (Bio_DO_ACCEPT (ABIO) <= 0) {

FPRINTF (stderr, "error accepting connection / n");

ERR_PRINT_ERRORS_FP (STDERR);

exit (0);

}

FPRINTF (stderr, "connection 1 establish / n");

/ * Return to the BIO * /

CBIO = BIO_POP (ABIO);

Bio_Puts (CBIO, "Connection 1: Sending Out Data On Initial Connection / N));

FPRINTF (stderr, "Sent Out Data On Connection 1 / N");

/ * Wait for another connection to establish * /

IF (Bio_DO_ACCEPT (ABIO) <= 0) {

FPRINTF (stderr, "error accepting connection / n");

ERR_PRINT_ERRORS_FP (STDERR);

exit (0);

}

FPRINTF (stderr, "connection 2 establish / n");

/ * Close the connection BIO, no longer accept the establishment of the connection * /

CBIO2 = BIO_POP (ABIO);

BIO_FREE (abio);

BIO_PUTS (CBIO2, "Connection 2: Sending Out Data On Second / N);

FPRINTF (stderr, "Sent Out Data On Connection 2 / N");

Bio_puts (CBIO, "Connection 1: Second Connection Establish / N");

/ * Close these two connects * /

BIO_FREE (CBIO); BIO_FREE (CBIO2);

转载请注明原文地址:https://www.9cbs.com/read-24007.html

New Post(0)