ithewei 3 years ago
parent
commit
bdcf796777
1 changed files with 8 additions and 4 deletions
  1. 8 4
      examples/socks5_proxy_server.c

+ 8 - 4
examples/socks5_proxy_server.c

@@ -133,7 +133,7 @@ static void on_close(hio_t* io) {
 
 static void on_recv(hio_t* io, void* buf, int readbytes) {
     socks5_conn_t* conn = (socks5_conn_t*)hevent_userdata(io);
-    uint8_t* bytes = (uint8_t*)buf;
+    const uint8_t* bytes = (uint8_t*)buf;
     switch(conn->state) {
     case s_begin:
         // printf("s_begin\n");
@@ -305,11 +305,15 @@ static void on_recv(hio_t* io, void* buf, int readbytes) {
                 conn->addr.sa.sa_family = AF_INET6;
                 memcpy(&conn->addr.sin6.sin6_addr, bytes, 16);
             } else {
-                char* host = (char*)bytes;
+                char* host = NULL;
+                STACK_OR_HEAP_ALLOC(host, readbytes + 1, 256);
+                memcpy(host, bytes, readbytes);
                 host[readbytes] = '\0';
                 // TODO: async DNS
-                if (ResolveAddr(host, &conn->addr) != 0) {
-                    fprintf(stderr, "Resovle %s failed!\n", host);
+                int ret = ResolveAddr(host, &conn->addr);
+                STACK_OR_HEAP_FREE(host);
+                if (ret != 0) {
+                    fprintf(stderr, "Resolve %.*s failed!\n", readbytes, (char*)bytes);
                     hio_close(io);
                     return;
                 }