diff --git a/src/local.c b/src/local.c index 1f9d3bc0..5c818393 100644 --- a/src/local.c +++ b/src/local.c @@ -191,7 +191,6 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) { // get remote addr and port if (request->atyp == 1) { - // IP V4 size_t in_addr_len = sizeof(struct in_addr); memcpy(addr_to_send + addr_len, server->buf + 4, in_addr_len + 2); @@ -207,6 +206,13 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) { // get port addr_to_send[addr_len++] = *(uint8_t *)(server->buf + 4 + 1 + name_len); addr_to_send[addr_len++] = *(uint8_t *)(server->buf + 4 + 1 + name_len + 1); + + } else if (request->atyp == 4) { + // IP V6 + size_t in6_addr_len = sizeof(struct in6_addr); + memcpy(addr_to_send + addr_len, server->buf + 4, in6_addr_len + 2); + addr_len += in6_addr_len + 2; + } else { LOGE("unsupported addrtype: %d", request->atyp); close_and_free_remote(EV_A_ remote); diff --git a/src/server.c b/src/server.c index d7dc5fcc..de909e4e 100644 --- a/src/server.c +++ b/src/server.c @@ -212,9 +212,11 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) { if (atyp == 1) { // IP V4 size_t in_addr_len = sizeof(struct in_addr); - char *a = inet_ntoa(*(struct in_addr*)(server->buf + offset)); - memcpy(host, a, strlen(a)); - offset += in_addr_len; + if (r > in_addr_len) { + char *a = inet_ntoa(*(struct in_addr*)(server->buf + offset)); + memcpy(host, a, strlen(a)); + offset += in_addr_len; + } } else if (atyp == 3) { // Domain name @@ -222,7 +224,20 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) { memcpy(host, server->buf + offset + 1, name_len); offset += name_len + 1; - } else { + } else if (atyp == 4) { + // IP V6 + size_t in6_addr_len = sizeof(struct in6_addr); + if (r > in6_addr_len) { + char a[INET6_ADDRSTRLEN]; + inet_ntop(AF_INET6, (const void*)(server->buf + offset), + a, sizeof(a)); + memcpy(host, a, strlen(a)); + offset += in6_addr_len; + } + + } + + if (offset == 0) { LOGE("unsupported addrtype: %d", atyp); close_and_free_remote(EV_A_ remote); close_and_free_server(EV_A_ server);