From 14c8fe6a99897dd79721f7d60cbac0cbe8bc876b Mon Sep 17 00:00:00 2001 From: Max Lv Date: Sun, 11 May 2014 09:09:24 +0800 Subject: [PATCH] fix a connection issue --- src/local.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/local.c b/src/local.c index 9f17f6a1..317946dc 100644 --- a/src/local.c +++ b/src/local.c @@ -187,15 +187,19 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) // insert shadowsocks header if (!remote->direct) { - char *tmp = malloc(r + server->addr_len); + if (!remote->send_ctx->connected) + { + char *tmp = malloc(r + server->addr_len); - memcpy(tmp, server->addr_to_send, server->addr_len); - memcpy(tmp + server->addr_len, buf, r); - r += server->addr_len; + memcpy(tmp, server->addr_to_send, server->addr_len); + memcpy(tmp + server->addr_len, buf, r); + r += server->addr_len; - free(remote->buf); + free(remote->buf); + + remote->buf = tmp; + } - remote->buf = tmp; remote->buf = ss_encrypt(BUF_SIZE, remote->buf, &r, server->e_ctx); if (remote->buf == NULL) @@ -850,15 +854,17 @@ static struct remote* connect_to_remote(struct listen_ctx *listener, hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; int index = rand() % listener->remote_num; - if (verbose) - { - LOGD("connect to server: %s:%s", listener->remote_addr[index].host, - listener->remote_addr[index].port); - } int err; if (host == NULL || port == NULL) + { + if (verbose) + { + LOGD("connect to server: %s:%s", listener->remote_addr[index].host, + listener->remote_addr[index].port); + } err = getaddrinfo(listener->remote_addr[index].host, listener->remote_addr[index].port, &hints, &remote_res); + } else err = getaddrinfo(host, port, &hints, &remote_res); @@ -963,6 +969,7 @@ int main (int argc, char **argv) } else if (option_index == 1) { + LOGD("initialize acl..."); acl = 1; init_acl(optarg); }