A connect request is the most common operation in SOCKS clients. This functionality is provided by the following functions:
Sync |
Async |
|
---|---|---|
SOCKS5 |
||
SOCKS4 |
Once a socket is connected to the proxy server, a single connect request is made to establish a connection to the application server:
error_code ec; endpoint bound_ep = connect( socket, target_ep, auth_options::none{}, ec);
When the request is successful, the SOCKS server starts relaying traffic on both directions. Thus, the client can now perform I/O operations on the same connection as if it were directly connected to the application server.
The function returns the endpoint with the address and port the SOCKS proxy assigned to connect to the target host. This is part of the connect reply message.
Because SOCKS servers might be multi-homed, this endpoint might be different from the address the client used to reach the SOCKS server.
An asynchronous request is a composed operation that follows the usual pattern of Asio async functions:
async_connect( socket, target_ep, auth_options::none{}, [&](error_code ec, endpoint bound_ep) { // Continuation if (!ec.failed()) do_write(bound_ep); });
The asynchronous functions support any Asio continuation token, as other Asio functionalities. After the connect operation, the client can perform I/O on the socket as if connected to the application server.
All connect
functions include
a parameter for authentication.
SOCKS authentication happens through a composed operation that performs sub-negotiation of authentication parameters before the final connect request is sent.
An object of type auth_options
can be used to specify
the parameters of the authentication:
error_code ec; endpoint bound_ep = connect( socket, target_ep, auth_options::userpass{ "user_id", "password"}, ec);
In SOCKS4 (connect_v4
/ async_connect_v4
) operations, authentication
happens via an string_view
describing the user
identity. According to the protocol, this field is mandatory, although it
can be empty.
error_code ec; endpoint bound_ep = connect_v4( socket, target_ep, "user_id", ec);