Browse Source

add ipv6 support

pull/4/merge
Max Lv 11 years ago
parent
commit
5419230004
2 changed files with 26 additions and 5 deletions
  1. 8
      src/local.c
  2. 23
      src/server.c

8
src/local.c

@ -191,7 +191,6 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) {
// get remote addr and port // get remote addr and port
if (request->atyp == 1) { if (request->atyp == 1) {
// IP V4 // IP V4
size_t in_addr_len = sizeof(struct in_addr); size_t in_addr_len = sizeof(struct in_addr);
memcpy(addr_to_send + addr_len, server->buf + 4, in_addr_len + 2); 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 // 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);
addr_to_send[addr_len++] = *(uint8_t *)(server->buf + 4 + 1 + name_len + 1); 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 { } else {
LOGE("unsupported addrtype: %d", request->atyp); LOGE("unsupported addrtype: %d", request->atyp);
close_and_free_remote(EV_A_ remote); close_and_free_remote(EV_A_ remote);

23
src/server.c

@ -212,9 +212,11 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) {
if (atyp == 1) { if (atyp == 1) {
// IP V4 // IP V4
size_t in_addr_len = sizeof(struct in_addr); 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) { } else if (atyp == 3) {
// Domain name // 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); memcpy(host, server->buf + offset + 1, name_len);
offset += name_len + 1; 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); LOGE("unsupported addrtype: %d", atyp);
close_and_free_remote(EV_A_ remote); close_and_free_remote(EV_A_ remote);
close_and_free_server(EV_A_ server); close_and_free_server(EV_A_ server);

Loading…
Cancel
Save