Mercurial > hg > th-libs
changeset 105:ea5b1c4b3af5
Cleanups and a breaking API change.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 21 Jun 2014 06:39:04 +0300 |
parents | ec5a5e573885 |
children | 4bba5be9d3bf |
files | th_network.c th_network.h |
diffstat | 2 files changed, 90 insertions(+), 71 deletions(-) [+] |
line wrap: on
line diff
--- a/th_network.c Sat Jun 21 05:59:29 2014 +0300 +++ b/th_network.c Sat Jun 21 06:39:04 2014 +0300 @@ -185,7 +185,7 @@ } -static BOOL th_conn_proxy_wait(th_conn_t *conn) +static int th_conn_proxy_wait(th_conn_t *conn) { int status, tries; @@ -200,7 +200,7 @@ status = th_conn_pull(conn); } - if (status < 0) + if (status != TH_CONN_DATA_AVAIL) { th_conn_err(conn, status, "Proxy negotiation failed at try %d with network error: %d\n", tries, status); @@ -208,13 +208,13 @@ else th_conn_err(conn, THERR_TIMED_OUT, "Proxy negotiation timed out.\n"); - return status == FALSE; + return status; } -static BOOL th_conn_proxy_send(th_conn_t *conn, void *buf, size_t bufsiz) +static int th_conn_proxy_send(th_conn_t *conn, void *buf, size_t bufsiz) { - BOOL ret; + int ret; th_conn_reset(conn); ret = th_conn_send_buf(conn, buf, bufsiz); th_free(buf); @@ -222,6 +222,23 @@ } +static uint8_t* th_conn_bufmalloc(th_conn_t *conn, const size_t bufsiz) +{ + uint8_t *buf; + + if ((buf = th_malloc(bufsiz)) == NULL) + { + th_conn_err(conn, THERR_MALLOC, + "Could not allocate memory for SOCKS negotiation buffer, %d bytes.\n", + bufsiz); + + return NULL; + } + + return buf; +} + + static int th_conn_socks4_negotiate(th_conn_t *conn, const int port, const char *host) { struct th_socks4_res_t *sockres; @@ -238,13 +255,8 @@ if (conn->proxy.type == TH_PROXY_SOCKS4A) bufsiz += strlen(conn->host) + 1; - if ((ptr = buf = th_malloc(bufsiz)) == NULL) - { - err = THERR_MALLOC; - th_conn_err(conn, err, - "Could not allocate memory for SOCKS negotiation buffer, %d bytes.\n", bufsiz); - return err; - } + if ((ptr = buf = th_conn_bufmalloc(conn, bufsiz)) == NULL) + return conn->err; // Create SOCKS 4 handshake socksh = (struct th_socks4_t *) buf; @@ -263,20 +275,22 @@ } // Send request - if (!th_conn_proxy_send(conn, buf, bufsiz)) - return FALSE; + if ((err = th_conn_proxy_send(conn, buf, bufsiz)) != THERR_OK) + return err; // Wait for SOCKS server to reply - if (th_conn_proxy_wait(conn) != 0) - return FALSE; + if (th_conn_proxy_wait(conn) != TH_CONN_DATA_AVAIL) + return err; sockres = (struct th_socks4_res_t *) &(conn->buf); if (sockres->nb != 0) { - th_conn_err(conn, THERR_INIT_FAIL, + err = THERR_INIT_FAIL; + th_conn_err(conn, err, "Invalid SOCKS 4 server reply, does not begin with NUL byte (%d).\n", sockres->nb); - return FALSE; + return err; } + if (sockres->result != 0x5a) { const char *s = NULL; @@ -287,13 +301,15 @@ case 0x5d: s = "Request failed because client's identd could not confirm the user ID string in the request"; break; default: s = "Unknown SOCKS 4 error response"; break; } - th_conn_err(conn, THERR_INIT_FAIL, + + err = THERR_INIT_FAIL; + th_conn_err(conn, err, "SOCKS 4 setup failed, 0x%02x: %s.\n", sockres->result, s); - return FALSE; + return err; } - return TRUE; + return THERR_OK; } @@ -301,7 +317,7 @@ { size_t bufsiz, userid_len = 0, passwd_len = 0; uint8_t *ptr, *buf; - int avail, auth; + int avail, auth, err = THERR_INIT_FAIL; th_conn_msg(conn, THLOG_INFO, "Initializing SOCKS 5 proxy negotiation.\n"); @@ -315,35 +331,34 @@ avail = 2; if (conn->proxy.userid == NULL || conn->proxy.passwd == NULL) { - th_conn_err(conn, THERR_INVALID_DATA, + err = THERR_INVALID_DATA; + th_conn_err(conn, err, "SOCKS 5 user authentication chosen, but no user/pass set.\n"); - return FALSE; + return err; } userid_len = strlen(conn->proxy.userid); passwd_len = strlen(conn->proxy.passwd); if (userid_len > 255 || passwd_len > 255) { - th_conn_err(conn, THERR_INVALID_DATA, + err = THERR_INVALID_DATA; + th_conn_err(conn, err, "SOCKS 5 proxy userid or password is too long.\n"); - return FALSE; + return err; } break; default: - th_conn_err(conn, THERR_NOT_SUPPORTED, + err = THERR_NOT_SUPPORTED; + th_conn_err(conn, err, "Unsupported proxy authentication method %d.\n", conn->proxy.auth_type); - return FALSE; + return err; } bufsiz = 2 + avail; - if ((ptr = buf = th_malloc(bufsiz)) == NULL) - { - th_conn_err(conn, THERR_MALLOC, - "Could not allocate memory for SOCKS negotiation buffer, %d bytes.\n", bufsiz); - return FALSE; - } + if ((ptr = buf = th_conn_bufmalloc(conn, bufsiz)) == NULL) + return conn->err; // Form handshake packet *ptr++ = 0x05; // Protocol version @@ -359,40 +374,38 @@ } // Send request - if (!th_conn_proxy_send(conn, buf, bufsiz)) - return FALSE; + if ((err = th_conn_proxy_send(conn, buf, bufsiz)) != THERR_OK) + return err; // Wait for SOCKS server to reply - if (th_conn_proxy_wait(conn) != 0) - return FALSE; + if (th_conn_proxy_wait(conn) != TH_CONN_DATA_AVAIL) + return THERR_INIT_FAIL; ptr = (uint8_t *) conn->buf; if (*ptr != 0x05) { - th_conn_err(conn, THERR_INVALID_DATA, + err = THERR_INVALID_DATA; + th_conn_err(conn, err, "Invalid SOCKS 5 server reply, does not begin with protocol version byte (%d).\n", *ptr); - return FALSE; + return err; } ptr++; auth = *ptr; if (auth == 0xff) { - th_conn_err(conn, THERR_NOT_SUPPORTED, + err = THERR_NOT_SUPPORTED; + th_conn_err(conn, err, "No authentication method could be negotiated with the server.\n"); - return FALSE; + return err; } if (auth == TH_SOCKS5_AUTH_USER) { // Attempt user/pass authentication (RFC 1929) bufsiz = 1 + 1 + 1 + userid_len + passwd_len; - if ((ptr = buf = th_malloc(bufsiz)) == NULL) - { - th_conn_err(conn, THERR_MALLOC, - "Could not allocate memory for SOCKS negotiation buffer, %d bytes.\n", bufsiz); + if ((ptr = buf = th_conn_bufmalloc(conn, bufsiz)) == NULL) return FALSE; - } *ptr++ = 0x01; @@ -405,35 +418,38 @@ ptr += passwd_len; // Send request - if (!th_conn_proxy_send(conn, buf, bufsiz)) - return FALSE; + if ((err = th_conn_proxy_send(conn, buf, bufsiz)) != THERR_OK) + return err; // Wait for SOCKS server to reply - if (th_conn_proxy_wait(conn) != 0) - return FALSE; + if (th_conn_proxy_wait(conn) != TH_CONN_DATA_AVAIL) + return THERR_INIT_FAIL; ptr = (uint8_t *) conn->buf; if (*ptr != 0x01) { - th_conn_err(conn, THERR_INVALID_DATA, + err = THERR_INVALID_DATA; + th_conn_err(conn, err, "Invalid SOCKS 5 server reply, does not begin with protocol version byte (%d).\n", *ptr); - return FALSE; + return err; } ptr++; if (*ptr != 0) { - th_conn_err(conn, THERR_AUTH_FAILED, + err = THERR_AUTH_FAILED; + th_conn_err(conn, err, "SOCKS 5 proxy user/pass authentication failed! Code %d.\n", *ptr); - return FALSE; + return err; } } else if (auth != TH_SOCKS5_AUTH_NONE) { + err = THERR_NOT_SUPPORTED; th_conn_err(conn, THERR_NOT_SUPPORTED, "Proxy server chose an unsupported SOCKS 5 authentication method %d.\n", auth); - return FALSE; + return err; } @@ -460,11 +476,11 @@ // Send request - if (!th_conn_proxy_send(conn, buf, bufsiz)) - return FALSE; + if ((err = th_conn_proxy_send(conn, buf, bufsiz)) != THERR_OK) + return err; // Wait for SOCKS server to reply - if (th_conn_proxy_wait(conn) != 0) + if (th_conn_proxy_wait(conn) != TH_CONN_DATA_AVAIL) return FALSE; ptr = (uint8_t *) conn->buf; @@ -476,22 +492,25 @@ ptr++; if (*ptr != 0) { + int err = 0; if (*ptr >= 0 && *ptr < sizeof()) - th_conn_err(conn, "%s.\n", nn_socks5_results_msgs[*ptr]); + th_conn_err(conn, err, "%s.\n", th_socks5_results_msgs[*ptr]); else - th_conn_err(conn, ".\n"); - return FALSE; + th_conn_err(conn, err ".Unknown\n"); + + return err; } ptr++; if (*ptr != 0) { - th_conn_err(conn, ".\n"); - return FALSE; + int err = asdf; + th_conn_err(conn, err, ".\n"); + return err; } #endif - return TRUE; + return THERR_OK; } @@ -556,13 +575,13 @@ { case TH_PROXY_SOCKS4: case TH_PROXY_SOCKS4A: - if (!th_conn_socks4_negotiate(conn, port, host)) + if ((err = th_conn_socks4_negotiate(conn, port, host)) != THERR_OK) goto error; th_conn_msg(conn, THLOG_INFO, "SOCKS 4 connection established!\n"); break; case TH_PROXY_SOCKS5: - if (!th_conn_socks5_negotiate(conn, port, host)) + if ((err = th_conn_socks5_negotiate(conn, port, host)) != THERR_OK) goto error; th_conn_msg(conn, THLOG_INFO, "SOCKS 5 connection established!\n"); break; @@ -625,7 +644,7 @@ } -BOOL th_conn_send_buf(th_conn_t *conn, const char *buf, const size_t len) +int th_conn_send_buf(th_conn_t *conn, const char *buf, const size_t len) { size_t bufLeft = len; const char *bufPtr = buf; @@ -638,13 +657,13 @@ { int err = th_errno_to_error(th_get_socket_errno()); th_conn_err(conn, err, "th_conn_send_buf() failed: %s", th_error_str(err)); - return FALSE; + return err; } bufLeft -= bufSent; bufPtr += bufSent; } - return TRUE; + return THERR_OK; }
--- a/th_network.h Sat Jun 21 05:59:29 2014 +0300 +++ b/th_network.h Sat Jun 21 06:39:04 2014 +0300 @@ -136,7 +136,7 @@ void th_conn_reset(th_conn_t *); int th_conn_pull(th_conn_t *); -BOOL th_conn_send_buf(th_conn_t *, const char *buf, const size_t len); +int th_conn_send_buf(th_conn_t *, const char *buf, const size_t len); BOOL th_conn_check(th_conn_t *);