Browse Source

Clean up

pull/542/head
Max Lv 8 years ago
parent
commit
da06378915
6 changed files with 36 additions and 29 deletions
  1. 4
      src/android.c
  2. 17
      src/encrypt.c
  3. 17
      src/local.c
  4. 3
      src/redir.c
  5. 22
      src/server.c
  6. 2
      src/udprelay.c

4
src/android.c

@ -73,7 +73,7 @@ int protect_socket(int fd)
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
LOGE("[android] connect() failed: %s (socket fd = %d), path: %s\n",
strerror(errno), sock, path);
strerror(errno), sock, path);
close(sock);
return -1;
}
@ -122,7 +122,7 @@ int send_traffic_stat(uint64_t tx, uint64_t rx)
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
LOGE("[android] connect() failed: %s (socket fd = %d), path: %s\n",
strerror(errno), sock, path);
strerror(errno), sock, path);
close(sock);
return -1;
}

17
src/encrypt.c

@ -203,8 +203,9 @@ static int safe_memcmp(const void *s1, const void *s2, size_t n)
{
const unsigned char *_s1 = (const unsigned char *)s1;
const unsigned char *_s2 = (const unsigned char *)s2;
int ret = 0, i;
for (i = 0; i < n; i++) ret |= _s1[i] ^ _s2[i];
int ret = 0, i;
for (i = 0; i < n; i++)
ret |= _s1[i] ^ _s2[i];
return !!ret;
}
@ -220,7 +221,7 @@ int brealloc(buffer_t *ptr, size_t len, size_t capacity)
{
int real_capacity = max(len, capacity);
if (ptr->capacity < real_capacity) {
ptr->array = realloc(ptr->array, real_capacity);
ptr->array = realloc(ptr->array, real_capacity);
ptr->capacity = real_capacity;
}
return real_capacity;
@ -1076,7 +1077,9 @@ int ss_onetimeauth(buffer_t *buf, uint8_t *iv, size_t capacity)
#if defined(USE_CRYPTO_OPENSSL)
HMAC(EVP_sha1(), auth_key, enc_iv_len + enc_key_len, (uint8_t *)buf->array, buf->len, (uint8_t *)hash, NULL);
#elif defined(USE_CRYPTO_MBEDTLS)
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1), auth_key, enc_iv_len + enc_key_len, (uint8_t *)buf->array, buf->len, (uint8_t *)hash);
mbedtls_md_hmac(mbedtls_md_info_from_type(
MBEDTLS_MD_SHA1), auth_key, enc_iv_len + enc_key_len, (uint8_t *)buf->array, buf->len,
(uint8_t *)hash);
#else
sha1_hmac(auth_key, enc_iv_len + enc_key_len, (uint8_t *)buf->array, buf->len, (uint8_t *)hash);
#endif
@ -1098,7 +1101,8 @@ int ss_onetimeauth_verify(buffer_t *buf, uint8_t *iv)
#if defined(USE_CRYPTO_OPENSSL)
HMAC(EVP_sha1(), auth_key, enc_iv_len + enc_key_len, (uint8_t *)buf->array, len, hash, NULL);
#elif defined(USE_CRYPTO_MBEDTLS)
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1), auth_key, enc_iv_len + enc_key_len, (uint8_t *)buf->array, len, hash);
mbedtls_md_hmac(mbedtls_md_info_from_type(
MBEDTLS_MD_SHA1), auth_key, enc_iv_len + enc_key_len, (uint8_t *)buf->array, len, hash);
#else
sha1_hmac(auth_key, enc_iv_len + enc_key_len, (uint8_t *)buf->array, len, hash);
#endif
@ -1592,7 +1596,8 @@ int ss_gen_hash(buffer_t *buf, uint32_t *counter, enc_ctx_t *ctx, size_t capacit
#if defined(USE_CRYPTO_OPENSSL)
HMAC(EVP_sha1(), key, enc_iv_len + sizeof(uint32_t), (uint8_t *)buf->array, blen, hash, NULL);
#elif defined(USE_CRYPTO_MBEDTLS)
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1), key, enc_iv_len + sizeof(uint32_t), (uint8_t *)buf->array, blen, hash);
mbedtls_md_hmac(mbedtls_md_info_from_type(
MBEDTLS_MD_SHA1), key, enc_iv_len + sizeof(uint32_t), (uint8_t *)buf->array, blen, hash);
#else
sha1_hmac(key, enc_iv_len + sizeof(uint32_t), (uint8_t *)buf->array, blen, hash);
#endif

17
src/local.c

@ -200,7 +200,7 @@ int create_and_bind(const char *addr, const char *port)
static void free_connections(struct ev_loop *loop)
{
struct cork_dllist_item *curr, *next;
cork_dllist_foreach_void (&connections, curr, next) {
cork_dllist_foreach_void(&connections, curr, next) {
server_t *server = cork_container_of(curr, server_t, entries);
remote_t *remote = server->remote;
close_and_free_server(loop, server);
@ -236,7 +236,8 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents)
// continue to wait for recv
return;
} else {
if (verbose) ERROR("server_recv_cb_recv");
if (verbose)
ERROR("server_recv_cb_recv");
close_and_free_remote(EV_A_ remote);
close_and_free_server(EV_A_ server);
return;
@ -298,15 +299,15 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents)
} else {
#ifdef TCP_FASTOPEN
#ifdef __APPLE__
((struct sockaddr_in*)&(remote->addr))->sin_len = sizeof(struct sockaddr_in);
((struct sockaddr_in *)&(remote->addr))->sin_len = sizeof(struct sockaddr_in);
sa_endpoints_t endpoints;
bzero((char*)&endpoints, sizeof(endpoints));
endpoints.sae_dstaddr = (struct sockaddr*)&(remote->addr);
bzero((char *)&endpoints, sizeof(endpoints));
endpoints.sae_dstaddr = (struct sockaddr *)&(remote->addr);
endpoints.sae_dstaddrlen = remote->addr_len;
int s = connectx(remote->fd, &endpoints, SAE_ASSOCID_ANY,
CONNECT_RESUME_ON_READ_WRITE | CONNECT_DATA_IDEMPOTENT,
NULL, 0, NULL, NULL);
CONNECT_RESUME_ON_READ_WRITE | CONNECT_DATA_IDEMPOTENT,
NULL, 0, NULL, NULL);
if (s == 0) {
s = send(remote->fd, remote->buf->array, remote->buf->len, 0);
}
@ -324,7 +325,7 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents)
} else {
ERROR("sendto");
if (errno == ENOTCONN) {
LOGE( "fast open is not supported on this platform");
LOGE("fast open is not supported on this platform");
// just turn it off
fast_open = 0;
}

3
src/redir.c

@ -408,7 +408,6 @@ static void remote_send_cb(EV_P_ ev_io *w, int revents)
}
ev_io_start(EV_A_ & remote->recv_ctx->io);
} else {
ERROR("getpeername");
// not connected
@ -426,7 +425,7 @@ static void remote_send_cb(EV_P_ ev_io *w, int revents)
} else {
// has data to send
ssize_t s = send(remote->fd, remote->buf->array + remote->buf->idx,
remote->buf->len, 0);
remote->buf->len, 0);
if (s < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
ERROR("send");

22
src/server.c

@ -109,9 +109,9 @@ static size_t parse_header_len(const char atyp, const char *data, size_t offset)
int verbose = 0;
static int white_list = 0;
static int acl = 0;
static int mode = TCP_ONLY;
static int auth = 0;
static int acl = 0;
static int mode = TCP_ONLY;
static int auth = 0;
static int fast_open = 0;
#ifdef HAVE_SETRLIMIT
@ -206,7 +206,7 @@ static void stat_update_cb(EV_P_ ev_timer *watcher, int revents)
static void free_connections(struct ev_loop *loop)
{
struct cork_dllist_item *curr, *next;
cork_dllist_foreach_void (&connections, curr, next) {
cork_dllist_foreach_void(&connections, curr, next) {
server_t *server = cork_container_of(curr, server_t, entries);
remote_t *remote = server->remote;
close_and_free_server(loop, server);
@ -285,6 +285,7 @@ int setfastopen(int fd)
#endif
return s;
}
#ifndef __MINGW32__
int setnonblocking(int fd)
{
@ -435,15 +436,15 @@ static remote_t *connect_to_remote(struct addrinfo *res,
#ifdef TCP_FASTOPEN
if (fast_open) {
#ifdef __APPLE__
((struct sockaddr_in*)(res->ai_addr))->sin_len = sizeof(struct sockaddr_in);
((struct sockaddr_in *)(res->ai_addr))->sin_len = sizeof(struct sockaddr_in);
sa_endpoints_t endpoints;
bzero((char*)&endpoints, sizeof(endpoints));
endpoints.sae_dstaddr = res->ai_addr;
bzero((char *)&endpoints, sizeof(endpoints));
endpoints.sae_dstaddr = res->ai_addr;
endpoints.sae_dstaddrlen = res->ai_addrlen;
struct iovec iov;
iov.iov_base = server->buf->array + server->buf->idx;
iov.iov_len = server->buf->len;
iov.iov_len = server->buf->len;
size_t len;
int s = connectx(sockfd, &endpoints, SAE_ASSOCID_ANY, CONNECT_DATA_IDEMPOTENT,
&iov, 1, &len, NULL);
@ -1274,7 +1275,8 @@ static void accept_cb(EV_P_ ev_io *w, int revents)
if (acl) {
char *peer_name = get_peer_name(serverfd);
if (peer_name != NULL && acl_match_ip(peer_name)) {
if (verbose) LOGI("Access denied from %s", peer_name);
if (verbose)
LOGI("Access denied from %s", peer_name);
close(serverfd);
return;
}
@ -1335,7 +1337,7 @@ int main(int argc, char **argv)
fast_open = 1;
} else if (option_index == 1) {
LOGI("initialize acl...");
acl = 1;
acl = 1;
acl_path = optarg;
} else if (option_index == 2) {
manager_address = optarg;

2
src/udprelay.c

@ -1256,7 +1256,7 @@ int init_udprelay(const char *server_host, const char *server_port,
struct cache *conn_cache;
cache_create(&conn_cache, MAX_UDP_CONN_NUM, free_cb);
//////////////////////////////////////////////////
// ////////////////////////////////////////////////
// Setup server context
// Bind to port

Loading…
Cancel
Save