]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Rework socks_request_set_socks5_error() with trunnel
authorrl1987 <rl1987@sdf.lonestar.org>
Wed, 23 May 2018 12:38:13 +0000 (14:38 +0200)
committerNick Mathewson <nickm@torproject.org>
Thu, 12 Jul 2018 15:41:44 +0000 (11:41 -0400)
src/or/proto_socks.c

index dab349bbeadfffc923fd56ff64ea835fbb9bfc92..9dbd688b752cb933d9bc9c329509d93520eb2ac9 100644 (file)
@@ -858,12 +858,31 @@ static void
 socks_request_set_socks5_error(socks_request_t *req,
                   socks5_reply_status_t reason)
 {
-   req->replylen = 10;
-   memset(req->reply,0,10);
+  socks5_server_reply_t *trunnel_resp = socks5_server_reply_new();
 
-   req->reply[0] = 0x05;   // VER field.
-   req->reply[1] = reason; // REP field.
-   req->reply[3] = 0x01;   // ATYP field.
+  socks5_server_reply_set_version(trunnel_resp, 0x05);
+  socks5_server_reply_set_reply(trunnel_resp, reason);
+  socks5_server_reply_set_atype(trunnel_resp, 0x01);
+
+  const char *errmsg = socks5_server_reply_check(trunnel_resp);
+  if (errmsg) {
+    log_warn(LD_APP, "socks5: reply validation failed: %s",
+             errmsg);
+    goto end;
+  }
+
+  ssize_t encoded = socks5_server_reply_encode(req->reply,
+                                               sizeof(req->reply),
+                                               trunnel_resp);
+  if (encoded < 0) {
+    log_warn(LD_APP, "socks5: reply encoding failed: %d",
+             (int)encoded);
+  } else {
+    req->replylen = (size_t)encoded;
+  }
+
+  end:
+  socks5_server_reply_free(trunnel_resp);
 }
 
 static const char SOCKS_PROXY_IS_NOT_AN_HTTP_PROXY_MSG[] =