commit ed1e2c6b87f0d3334511ae6c446ddab15a728a54 Author: David Goulet dgoulet@ev0ke.net Date: Tue May 12 13:56:40 2015 -0400
Move SOCKS5 auth in a seperate function
Too many places were using the same code so create auth_socks5() to do that using a given connection object.
Signed-off-by: David Goulet dgoulet@ev0ke.net --- src/lib/torsocks.c | 60 +++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 26 deletions(-)
diff --git a/src/lib/torsocks.c b/src/lib/torsocks.c index 5836c31..70014ce 100644 --- a/src/lib/torsocks.c +++ b/src/lib/torsocks.c @@ -408,6 +408,35 @@ error: }
/* + * Using the given connection, do a SOCKS5 authentication with the + * username/password in the global configuration. + * + * Return 0 on success else a negative value on error. + */ +static int +auth_socks5(struct connection *conn) +{ + int ret; + + assert(conn); + + ret = socks5_send_user_pass_request(conn, + tsocks_config.conf_file.socks5_username, + tsocks_config.conf_file.socks5_password); + if (ret < 0) { + goto error; + } + + ret = socks5_recv_user_pass_reply(conn); + if (ret < 0) { + goto error; + } + +error: + return ret; +} + +/* * Initiate a SOCK5 connection to the Tor network using the given connection. * The socks5 API will use the torsocks configuration object to find the tor * daemon. If a username/password has been set use that method for the SOCKS5 @@ -439,14 +468,7 @@ int tsocks_connect_to_tor(struct connection *conn)
/* For the user/pass method, send the request before connect. */ if (socks5_method == SOCKS5_USER_PASS_METHOD) { - ret = socks5_send_user_pass_request(conn, - tsocks_config.conf_file.socks5_username, - tsocks_config.conf_file.socks5_password); - if (ret < 0) { - goto error; - } - - ret = socks5_recv_user_pass_reply(conn); + ret = auth_socks5(conn); if (ret < 0) { goto error; } @@ -541,16 +563,9 @@ int tsocks_tor_resolve(int af, const char *hostname, void *ip_addr)
/* For the user/pass method, send the request before resolve. */ if (socks5_method == SOCKS5_USER_PASS_METHOD) { - ret = socks5_send_user_pass_request(&conn, - tsocks_config.conf_file.socks5_username, - tsocks_config.conf_file.socks5_password); + ret = auth_socks5(&conn); if (ret < 0) { - goto end_close; - } - - ret = socks5_recv_user_pass_reply(&conn); - if (ret < 0) { - goto end_close; + goto error; } }
@@ -612,16 +627,9 @@ int tsocks_tor_resolve_ptr(const char *addr, char **ip, int af)
/* For the user/pass method, send the request before resolve ptr. */ if (socks5_method == SOCKS5_USER_PASS_METHOD) { - ret = socks5_send_user_pass_request(&conn, - tsocks_config.conf_file.socks5_username, - tsocks_config.conf_file.socks5_password); + ret = auth_socks5(&conn); if (ret < 0) { - goto end_close; - } - - ret = socks5_recv_user_pass_reply(&conn); - if (ret < 0) { - goto end_close; + goto error; } }
tor-commits@lists.torproject.org