Browse Source

Clean up

pull/2174/head
Max Lv 6 years ago
parent
commit
dd5e954ab4
16 changed files with 123 additions and 145 deletions
  1. 2
      src/cache.h
  2. 12
      src/crypto.h
  3. 44
      src/local.c
  4. 27
      src/netutils.c
  5. 8
      src/plugin.c
  6. 2
      src/protocol.h
  7. 16
      src/redir.c
  8. 13
      src/resolv.c
  9. 30
      src/server.c
  10. 2
      src/shadowsocks.h
  11. 49
      src/tunnel.c
  12. 33
      src/udprelay.c
  13. 8
      src/utils.c
  14. 1
      src/utils.h
  15. 13
      src/winsock.c
  16. 8
      src/winsock.h

2
src/cache.h

@ -52,7 +52,7 @@ struct cache_entry {
struct cache {
size_t max_entries; /**<Amount of entries this cache object can hold */
struct cache_entry *entries; /**<Head pointer for uthash */
void (*free_cb) (void *key, void *element); /**<Callback function to free cache entries */
void (*free_cb)(void *key, void *element); /**<Callback function to free cache entries */
};
int cache_create(struct cache **dst, const size_t capacity,

12
src/crypto.h

@ -115,13 +115,13 @@ typedef struct {
typedef struct crypto {
cipher_t *cipher;
int(*const encrypt_all)(buffer_t *, cipher_t *, size_t);
int(*const decrypt_all)(buffer_t *, cipher_t *, size_t);
int(*const encrypt)(buffer_t *, cipher_ctx_t *, size_t);
int(*const decrypt)(buffer_t *, cipher_ctx_t *, size_t);
int(*const encrypt_all) (buffer_t *, cipher_t *, size_t);
int(*const decrypt_all) (buffer_t *, cipher_t *, size_t);
int(*const encrypt) (buffer_t *, cipher_ctx_t *, size_t);
int(*const decrypt) (buffer_t *, cipher_ctx_t *, size_t);
void(*const ctx_init)(cipher_t *, cipher_ctx_t *, int);
void(*const ctx_release)(cipher_ctx_t *);
void(*const ctx_init) (cipher_t *, cipher_ctx_t *, int);
void(*const ctx_release) (cipher_ctx_t *);
} crypto_t;
int balloc(buffer_t *, size_t);

44
src/local.c

@ -83,9 +83,8 @@
#define BUF_SIZE 2048
#endif
int verbose = 0;
int reuse_port = 0;
int keep_resolving = 1;
int verbose = 0;
int reuse_port = 0;
#ifdef __ANDROID__
int vpn = 0;
@ -283,7 +282,8 @@ server_handshake_reply(EV_P_ ev_io *w, int udp_assc, struct socks5_response *res
server_ctx_t *server_recv_ctx = (server_ctx_t *)w;
server_t *server = server_recv_ctx->server;
remote_t *remote = server->remote;
if (server->stage != STAGE_HANDSHAKE) return 0;
if (server->stage != STAGE_HANDSHAKE)
return 0;
struct sockaddr_in sock_addr;
if (udp_assc) {
@ -296,7 +296,8 @@ server_handshake_reply(EV_P_ ev_io *w, int udp_assc, struct socks5_response *res
close_and_free_server(EV_A_ server);
return -1;
}
} else memset(&sock_addr, 0, sizeof(sock_addr));
} else
memset(&sock_addr, 0, sizeof(sock_addr));
buffer_t resp_to_send;
buffer_t *resp_buf = &resp_to_send;
@ -306,11 +307,11 @@ server_handshake_reply(EV_P_ ev_io *w, int udp_assc, struct socks5_response *res
memcpy(resp_buf->data + sizeof(struct socks5_response),
&sock_addr.sin_addr, sizeof(sock_addr.sin_addr));
memcpy(resp_buf->data + sizeof(struct socks5_response) +
sizeof(sock_addr.sin_addr),
sizeof(sock_addr.sin_addr),
&sock_addr.sin_port, sizeof(sock_addr.sin_port));
int reply_size = sizeof(struct socks5_response) +
sizeof(sock_addr.sin_addr) + sizeof(sock_addr.sin_port);
sizeof(sock_addr.sin_addr) + sizeof(sock_addr.sin_port);
int s = send(server->fd, resp_buf->data, reply_size, 0);
@ -385,7 +386,7 @@ server_handshake(EV_P_ ev_io *w, buffer_t *buf)
if (acl || verbose) {
uint16_t p = ntohs(*(uint16_t *)(buf->data + request_len + in_addr_len));
if (!inet_ntop(AF_INET, (const void *)(buf->data + request_len),
ip, INET_ADDRSTRLEN)) {
ip, INET_ADDRSTRLEN)) {
LOGI("inet_ntop(AF_INET): %s", strerror(errno));
ip[0] = '\0';
}
@ -418,7 +419,7 @@ server_handshake(EV_P_ ev_io *w, buffer_t *buf)
if (acl || verbose) {
uint16_t p = ntohs(*(uint16_t *)(buf->data + request_len + in6_addr_len));
if (!inet_ntop(AF_INET6, (const void *)(buf->data + request_len),
ip, INET6_ADDRSTRLEN)) {
ip, INET6_ADDRSTRLEN)) {
LOGI("inet_ntop(AF_INET6): %s", strerror(errno));
ip[0] = '\0';
}
@ -449,7 +450,8 @@ server_handshake(EV_P_ ev_io *w, buffer_t *buf)
ret = tls_protocol->parse_packet(buf->data + 3 + abuf->len,
buf->len - 3 - abuf->len, &hostname);
if (ret == -1 && buf->len < BUF_SIZE && server->stage != STAGE_SNI) {
if (server_handshake_reply(EV_A_ w, 0, &response) < 0) return -1;
if (server_handshake_reply(EV_A_ w, 0, &response) < 0)
return -1;
server->stage = STAGE_SNI;
ev_timer_start(EV_A_ & server->delayed_connect_watcher);
return -1;
@ -463,7 +465,8 @@ server_handshake(EV_P_ ev_io *w, buffer_t *buf)
}
}
if (server_handshake_reply(EV_A_ w, 0, &response) < 0) return -1;
if (server_handshake_reply(EV_A_ w, 0, &response) < 0)
return -1;
server->stage = STAGE_STREAM;
buf->len -= (3 + abuf_len);
@ -504,20 +507,23 @@ server_handshake(EV_P_ ev_io *w, buffer_t *buf)
#ifdef __ANDROID__
&& !vpn
#endif
) { // resolve domain so we can bypass domain with geoip
if (get_sockaddr(host, port, &storage, 0, ipv6first)) goto not_bypass;
) { // resolve domain so we can bypass domain with geoip
if (get_sockaddr(host, port, &storage, 0, ipv6first))
goto not_bypass;
resolved = 1;
switch (((struct sockaddr *)&storage)->sa_family) {
case AF_INET:
{
struct sockaddr_in *addr_in = (struct sockaddr_in *)&storage;
if (!inet_ntop(AF_INET, &(addr_in->sin_addr), ip, INET_ADDRSTRLEN)) goto not_bypass;
if (!inet_ntop(AF_INET, &(addr_in->sin_addr), ip, INET_ADDRSTRLEN))
goto not_bypass;
break;
}
case AF_INET6:
{
struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&storage;
if (!inet_ntop(AF_INET6, &(addr_in6->sin6_addr), ip, INET6_ADDRSTRLEN)) goto not_bypass;
if (!inet_ntop(AF_INET6, &(addr_in6->sin6_addr), ip, INET6_ADDRSTRLEN))
goto not_bypass;
break;
}
default:
@ -549,7 +555,9 @@ server_handshake(EV_P_ ev_io *w, buffer_t *buf)
}
if (atyp == SOCKS5_ATYP_DOMAIN && !resolved)
#ifdef __ANDROID__
if (vpn) goto not_bypass; else
if (vpn)
goto not_bypass;
else
#endif
err = get_sockaddr(host, port, &storage, 0, ipv6first);
else
@ -562,7 +570,7 @@ server_handshake(EV_P_ ev_io *w, buffer_t *buf)
}
}
not_bypass:
not_bypass:
// Not bypass
if (remote == NULL) {
remote = create_remote(server->listener, NULL);
@ -1387,7 +1395,6 @@ signal_cb(EV_P_ ev_signal *w, int revents)
ev_io_stop(EV_DEFAULT, &plugin_watcher.io);
#endif
#endif
keep_resolving = 0;
ev_unloop(EV_A_ EVUNLOOP_ALL);
}
}
@ -1409,7 +1416,6 @@ plugin_watcher_cb(EV_P_ ev_io *w, int revents)
ev_signal_stop(EV_DEFAULT, &sigint_watcher);
ev_signal_stop(EV_DEFAULT, &sigterm_watcher);
ev_io_stop(EV_DEFAULT, &plugin_watcher.io);
keep_resolving = 0;
ev_unloop(EV_A_ EVUNLOOP_ALL);
}

27
src/netutils.c

@ -53,10 +53,6 @@ extern int verbose;
static const char valid_label_bytes[] =
"-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
#if defined(MODULE_LOCAL)
extern int keep_resolving;
#endif
int
set_reuseport(int socket)
{
@ -81,7 +77,7 @@ setinterface(int socket_fd, const char *interface_name)
{
struct ifreq interface;
memset(&interface, 0, sizeof(struct ifreq));
strncpy(interface.ifr_name, interface_name, IFNAMSIZ-1);
strncpy(interface.ifr_name, interface_name, IFNAMSIZ - 1);
int res = setsockopt(socket_fd, SOL_SOCKET, SO_BINDTODEVICE, &interface,
sizeof(struct ifreq));
return res;
@ -92,11 +88,10 @@ setinterface(int socket_fd, const char *interface_name)
int
bind_to_address(int socket_fd, const char *host)
{
static struct sockaddr_storage storage = {0};
static struct sockaddr_storage storage = { 0 };
if (storage.ss_family == AF_INET) {
return bind(socket_fd, (struct sockaddr *)&storage, sizeof(struct sockaddr_in));
}
else if (storage.ss_family == AF_INET6) {
} else if (storage.ss_family == AF_INET6) {
return bind(socket_fd, (struct sockaddr *)&storage, sizeof(struct sockaddr_in6));
} else if (host != NULL) {
struct cork_ip ip;
@ -152,21 +147,7 @@ get_sockaddr(char *host, char *port,
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
int err, i;
for (i = 1; i < 8; i++) {
err = getaddrinfo(host, port, &hints, &result);
#if defined(MODULE_LOCAL)
if (!keep_resolving)
break;
#endif
if ((!block || !err)) {
break;
} else {
sleep(pow(2, i));
LOGE("failed to resolve server name, wait %.0f seconds", pow(2, i));
}
}
int err = getaddrinfo(host, port, &hints, &result);
if (err != 0) {
LOGE("getaddrinfo: %s", gai_strerror(err));

8
src/plugin.c

@ -24,7 +24,6 @@
#include "config.h"
#endif
#include <string.h>
#ifndef __MINGW32__
#include <unistd.h>
@ -53,7 +52,7 @@ static struct cork_env *env = NULL;
static struct cork_exec *exec = NULL;
static struct cork_subprocess *sub = NULL;
#ifdef __MINGW32__
static uint16_t sub_control_port = 0;
static uint16_t sub_control_port = 0;
void cork_subprocess_set_control(struct cork_subprocess *self, uint16_t port);
#endif
@ -122,7 +121,7 @@ start_ss_plugin(const char *plugin,
#ifdef __MINGW32__
cork_subprocess_set_control(sub, sub_control_port);
#endif
return cork_subprocess_start(sub);
}
@ -164,7 +163,7 @@ start_obfsproxy(const char *plugin,
{
char *pch;
char *opts_dump = NULL;
char *buf = NULL;
char *buf = NULL;
int ret, buf_size = 0;
if (plugin_opts != NULL) {
@ -342,4 +341,3 @@ is_plugin_running()
}
return 0;
}

2
src/protocol.h

@ -28,7 +28,7 @@
typedef struct protocol {
const int default_port;
int(*const parse_packet)(const char *, size_t, char **);
int(*const parse_packet) (const char *, size_t, char **);
} protocol_t;
#endif

16
src/redir.c

@ -83,9 +83,8 @@ static void close_and_free_remote(EV_P_ remote_t *remote);
static void free_server(server_t *server);
static void close_and_free_server(EV_P_ server_t *server);
int verbose = 0;
int reuse_port = 0;
int keep_resolving = 1;
int verbose = 0;
int reuse_port = 0;
static crypto_t *crypto;
@ -521,8 +520,8 @@ remote_send_cb(EV_P_ ev_io *w, int revents)
if (remote->addr != NULL) {
#if defined(TCP_FASTOPEN_CONNECT)
int optval = 1;
if(setsockopt(remote->fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT,
(void *)&optval, sizeof(optval)) < 0)
if (setsockopt(remote->fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT,
(void *)&optval, sizeof(optval)) < 0)
FATAL("failed to set TCP_FASTOPEN_CONNECT");
s = connect(remote->fd, remote->addr, get_sockaddr_len(remote->addr));
if (s == 0)
@ -543,7 +542,7 @@ remote_send_cb(EV_P_ ev_io *w, int revents)
ev_timer_start(EV_A_ & remote_send_ctx->watcher);
} else {
if (errno == EOPNOTSUPP || errno == EPROTONOSUPPORT ||
errno == ENOPROTOOPT) {
errno == ENOPROTOOPT) {
fast_open = 0;
LOGE("fast open is not supported on this platform");
} else {
@ -823,8 +822,7 @@ signal_cb(EV_P_ ev_signal *w, int revents)
if (!is_plugin_running()) {
LOGE("plugin service exit unexpectedly");
ret_val = -1;
}
else
} else
return;
case SIGINT:
case SIGTERM:
@ -832,7 +830,6 @@ signal_cb(EV_P_ ev_signal *w, int revents)
ev_signal_stop(EV_DEFAULT, &sigterm_watcher);
ev_signal_stop(EV_DEFAULT, &sigchld_watcher);
keep_resolving = 0;
ev_unloop(EV_A_ EVUNLOOP_ALL);
}
}
@ -1193,7 +1190,6 @@ main(int argc, char **argv)
listen_ctx_t *listen_ctx_current = &listen_ctx;
do {
if (listen_ctx_current->tos) {
LOGI("listening at %s:%s (TOS 0x%x)", local_addr, local_port, listen_ctx_current->tos);
} else {

13
src/resolv.c

@ -65,7 +65,6 @@
* Implement DNS resolution interface using libc-ares
*/
#define SS_NUM_IOS 6
#define SS_INVALID_FD -1
#define SS_TIMER_AFTER 1.0
@ -73,7 +72,7 @@
struct resolv_ctx {
struct ev_io ios[SS_NUM_IOS];
struct ev_timer timer;
ev_tstamp last_tick;
ev_tstamp last_tick;
ares_channel channel;
struct ares_options options;
@ -183,9 +182,8 @@ resolv_init(struct ev_loop *loop, char *nameservers, int ipv6first)
FATAL("failed to set nameservers");
}
for (int i = 0; i < SS_NUM_IOS; i++) {
for (int i = 0; i < SS_NUM_IOS; i++)
ev_io_init(&default_ctx.ios[i], resolv_sock_cb, SS_INVALID_FD, 0);
}
default_ctx.last_tick = ev_now(default_loop);
ev_init(&default_ctx.timer, resolv_timer_cb);
@ -198,9 +196,8 @@ void
resolv_shutdown(struct ev_loop *loop)
{
ev_timer_stop(default_loop, &default_ctx.timer);
for (int i = 0; i < SS_NUM_IOS; i++) {
for (int i = 0; i < SS_NUM_IOS; i++)
ev_io_stop(default_loop, &default_ctx.ios[i]);
}
ares_cancel(default_ctx.channel);
ares_destroy(default_ctx.channel);
@ -444,7 +441,7 @@ resolv_timer_cb(struct ev_loop *loop, struct ev_timer *w, int revents)
{
struct resolv_ctx *ctx = cork_container_of(w, struct resolv_ctx, timer);
ev_tstamp now = ev_now(default_loop);
ev_tstamp now = ev_now(default_loop);
ev_tstamp after = ctx->last_tick - now + SS_TIMER_AFTER;
if (after < 0.0) {
@ -466,7 +463,7 @@ static void
resolv_sock_state_cb(void *data, int s, int read, int write)
{
struct resolv_ctx *ctx = (struct resolv_ctx *)data;
int events = (read ? EV_READ : 0) | (write ? EV_WRITE : 0);
int events = (read ? EV_READ : 0) | (write ? EV_WRITE : 0);
int i = 0, ffi = -1;
for (; i < SS_NUM_IOS; i++) {

30
src/server.c

@ -232,6 +232,7 @@ stat_update_cb(EV_P_ ev_timer *watcher, int revents)
close(sfd);
}
#endif
static void
@ -345,6 +346,7 @@ setnonblocking(int fd)
}
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
#endif
int
@ -520,9 +522,9 @@ connect_to_remote(EV_P_ struct addrinfo *res,
#if defined(MSG_FASTOPEN) && !defined(TCP_FASTOPEN_CONNECT)
int s = -1;
s = sendto(sockfd, server->buf->data, server->buf->len,
MSG_FASTOPEN, res->ai_addr, res->ai_addrlen);
MSG_FASTOPEN, res->ai_addr, res->ai_addrlen);
#elif defined(TCP_FASTOPEN_WINSOCK)
DWORD s = -1;
DWORD s = -1;
DWORD err = 0;
do {
int optval = 1;
@ -552,14 +554,14 @@ connect_to_remote(EV_P_ struct addrinfo *res,
&s, &remote->olap)) {
remote->connect_ex_done = 1;
break;
};
}
// XXX: ConnectEx pending, check later in remote_send
if (WSAGetLastError() == ERROR_IO_PENDING) {
err = CONNECT_IN_PROGRESS;
break;
}
ERROR("ConnectEx");
} while(0);
} while (0);
// Set error number
if (err) {
SetLastError(err);
@ -568,8 +570,8 @@ connect_to_remote(EV_P_ struct addrinfo *res,
int s = -1;
#if defined(TCP_FASTOPEN_CONNECT)
int optval = 1;
if(setsockopt(sockfd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT,
(void *)&optval, sizeof(optval)) < 0)
if (setsockopt(sockfd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT,
(void *)&optval, sizeof(optval)) < 0)
FATAL("failed to set TCP_FASTOPEN_CONNECT");
s = connect(sockfd, res->ai_addr, res->ai_addrlen);
#elif defined(CONNECT_DATA_IDEMPOTENT)
@ -580,7 +582,7 @@ connect_to_remote(EV_P_ struct addrinfo *res,
endpoints.sae_dstaddrlen = res->ai_addrlen;
s = connectx(sockfd, &endpoints, SAE_ASSOCID_ANY, CONNECT_DATA_IDEMPOTENT,
NULL, 0, NULL, NULL);
NULL, 0, NULL, NULL);
#else
FATAL("fast open is not enabled in this build");
#endif
@ -1228,8 +1230,8 @@ remote_send_cb(EV_P_ ev_io *w, int revents)
// Non-blocking way to fetch ConnectEx result
if (WSAGetOverlappedResult(remote->fd, &remote->olap,
&numBytes, FALSE, &flags)) {
remote->buf->len -= numBytes;
remote->buf->idx = numBytes;
remote->buf->len -= numBytes;
remote->buf->idx = numBytes;
remote->connect_ex_done = 1;
} else if (WSAGetLastError() == WSA_IO_INCOMPLETE) {
// XXX: ConnectEx still not connected, wait for next time
@ -1240,7 +1242,7 @@ remote_send_cb(EV_P_ ev_io *w, int revents)
close_and_free_remote(EV_A_ remote);
close_and_free_server(EV_A_ server);
return;
};
}
}
// Make getpeername work
@ -1498,8 +1500,7 @@ signal_cb(EV_P_ ev_signal *w, int revents)
if (!is_plugin_running()) {
LOGE("plugin service exit unexpectedly");
ret_val = -1;
}
else
} else
return;
#endif
case SIGINT:
@ -1534,6 +1535,7 @@ plugin_watcher_cb(EV_P_ ev_io *w, int revents)
ev_io_stop(EV_DEFAULT, &plugin_watcher.io);
ev_unloop(EV_A_ EVUNLOOP_ALL);
}
#endif
static void
@ -1946,9 +1948,9 @@ main(int argc, char **argv)
do {
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
addr.sin_port = htons(plugin_watcher.port);
addr.sin_port = htons(plugin_watcher.port);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) {
LOGE("failed to bind plugin control port");
break;

2
src/shadowsocks.h

@ -64,7 +64,7 @@ typedef struct {
extern "C" {
#endif
typedef void (*ss_local_callback) (int socks_fd, int udp_fd, void *data);
typedef void (*ss_local_callback)(int socks_fd, int udp_fd, void *data);
/*
* Create and start a shadowsocks local server.

49
src/tunnel.c

@ -84,9 +84,8 @@ static void close_and_free_server(EV_P_ server_t *server);
int vpn = 0;
#endif
int verbose = 0;
int reuse_port = 0;
int keep_resolving = 1;
int verbose = 0;
int reuse_port = 0;
static crypto_t *crypto;
@ -95,9 +94,9 @@ static int mode = TCP_ONLY;
#ifdef HAVE_SETRLIMIT
static int nofile = 0;
#endif
static int no_delay = 0;
static int no_delay = 0;
static int fast_open = 0;
static int ret_val = 0;
static int ret_val = 0;
static struct ev_signal sigint_watcher;
static struct ev_signal sigterm_watcher;
@ -122,6 +121,7 @@ setnonblocking(int fd)
}
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
#endif
int
@ -391,9 +391,8 @@ remote_send_cb(EV_P_ ev_io *w, int revents)
ev_timer_stop(EV_A_ & remote_send_ctx->watcher);
if (!remote_send_ctx->connected) {
int r = 0;
if (remote->addr == NULL) {
struct sockaddr_storage addr;
socklen_t len = sizeof(struct sockaddr_storage);
@ -460,7 +459,6 @@ remote_send_cb(EV_P_ ev_io *w, int revents)
}
ev_io_start(EV_A_ & remote->recv_ctx->io);
} else {
ERROR("getpeername");
// not connected
@ -529,13 +527,13 @@ remote_send_cb(EV_P_ ev_io *w, int revents)
memset((char *)&endpoints, 0, sizeof(endpoints));
endpoints.sae_dstaddr = (struct sockaddr *)&(remote->addr);
endpoints.sae_dstaddrlen = get_sockaddr_len(remote->addr);
s = connectx(remote->fd, &endpoints, SAE_ASSOCID_ANY,
CONNECT_RESUME_ON_READ_WRITE | CONNECT_DATA_IDEMPOTENT,
NULL, 0, NULL, NULL);
s = connectx(remote->fd, &endpoints, SAE_ASSOCID_ANY,
CONNECT_RESUME_ON_READ_WRITE | CONNECT_DATA_IDEMPOTENT,
NULL, 0, NULL, NULL);
#elif defined(TCP_FASTOPEN_CONNECT)
int optval = 1;
if(setsockopt(remote->fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT,
(void *)&optval, sizeof(optval)) < 0)
if (setsockopt(remote->fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT,
(void *)&optval, sizeof(optval)) < 0)
FATAL("failed to set TCP_FASTOPEN_CONNECT");
s = connect(remote->fd, remote->addr, get_sockaddr_len(remote->addr));
if (s == 0)
@ -547,9 +545,9 @@ remote_send_cb(EV_P_ ev_io *w, int revents)
#else
FATAL("tcp fast open is not supported on this platform");
#endif
remote->addr = NULL;
if (s == -1) {
if (errno == CONNECT_IN_PROGRESS) {
ev_io_start(EV_A_ & remote_send_ctx->io);
@ -557,7 +555,7 @@ remote_send_cb(EV_P_ ev_io *w, int revents)
} else {
fast_open = 0;
if (errno == EOPNOTSUPP || errno == EPROTONOSUPPORT ||
errno == ENOPROTOOPT) {
errno == ENOPROTOOPT) {
LOGE("fast open is not supported on this platform");
} else {
ERROR("fast_open_connect");
@ -569,7 +567,7 @@ remote_send_cb(EV_P_ ev_io *w, int revents)
}
} else {
s = send(remote->fd, remote->buf->data + remote->buf->idx,
remote->buf->len, 0);
remote->buf->len, 0);
}
if (s == -1) {
@ -811,7 +809,6 @@ accept_cb(EV_P_ ev_io *w, int revents)
// listen to remote connected event
ev_io_start(EV_A_ & remote->send_ctx->io);
ev_timer_start(EV_A_ & remote->send_ctx->watcher);
}
static void
@ -824,8 +821,7 @@ signal_cb(EV_P_ ev_signal *w, int revents)
if (!is_plugin_running()) {
LOGE("plugin service exit unexpectedly");
ret_val = -1;
}
else
} else
return;
#endif
case SIGINT:
@ -837,7 +833,6 @@ signal_cb(EV_P_ ev_signal *w, int revents)
#else
ev_io_stop(EV_DEFAULT, &plugin_watcher.io);
#endif
keep_resolving = 0;
ev_unloop(EV_A_ EVUNLOOP_ALL);
}
}
@ -859,9 +854,9 @@ plugin_watcher_cb(EV_P_ ev_io *w, int revents)
ev_signal_stop(EV_DEFAULT, &sigint_watcher);
ev_signal_stop(EV_DEFAULT, &sigterm_watcher);
ev_io_stop(EV_DEFAULT, &plugin_watcher.io);
keep_resolving = 0;
ev_unloop(EV_A_ EVUNLOOP_ALL);
}
#endif
int
@ -898,7 +893,7 @@ main(int argc, char **argv)
char *tunnel_addr_str = NULL;
static struct option long_options[] = {
{ "fast-open", no_argument, NULL, GETOPT_VAL_FAST_OPEN },
{ "fast-open", no_argument, NULL, GETOPT_VAL_FAST_OPEN },
{ "mtu", required_argument, NULL, GETOPT_VAL_MTU },
{ "no-delay", no_argument, NULL, GETOPT_VAL_NODELAY },
{ "mptcp", no_argument, NULL, GETOPT_VAL_MPTCP },
@ -1158,7 +1153,7 @@ main(int argc, char **argv)
if (local_addr == NULL) {
local_addr = "127.0.0.1";
}
if (fast_open == 1) {
#ifdef TCP_FASTOPEN
LOGI("using tcp fast open");
@ -1167,7 +1162,7 @@ main(int argc, char **argv)
fast_open = 0;
#endif
}
USE_SYSLOG(argv[0], pid_flags);
if (pid_flags) {
daemonize(pid_path);
@ -1194,9 +1189,9 @@ main(int argc, char **argv)
do {
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
addr.sin_port = htons(plugin_watcher.port);
addr.sin_port = htons(plugin_watcher.port);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) {
LOGE("failed to bind plugin control port");
break;

33
src/udprelay.c

@ -116,6 +116,7 @@ setnonblocking(int fd)
}
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
#endif
#if defined(MODULE_REMOTE) && defined(SO_BROADCAST)
@ -371,12 +372,12 @@ create_remote_socket(int ipv6)
}
} else {
#endif
if (bind(remote_sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
FATAL("[udp] cannot bind remote");
return -1;
}
#ifdef MODULE_REMOTE
if (bind(remote_sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
FATAL("[udp] cannot bind remote");
return -1;
}
#ifdef MODULE_REMOTE
}
#endif
} else {
// Or else bind to IPv4
@ -399,12 +400,12 @@ create_remote_socket(int ipv6)
}
} else {
#endif
if (bind(remote_sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
FATAL("[udp] cannot bind remote");
return -1;
}
#ifdef MODULE_REMOTE
if (bind(remote_sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
FATAL("[udp] cannot bind remote");
return -1;
}
#ifdef MODULE_REMOTE
}
#endif
}
return remote_sock;
@ -905,7 +906,8 @@ server_recv_cb(EV_P_ ev_io *w, int revents)
goto CLEAN_UP;
} else if (buf->len > packet_size) {
if (verbose) {
LOGI("[udp] UDP server_recv_recvmsg fragmentation, MTU at least be: " SSIZE_FMT, buf->len + PACKET_HEADER_SIZE);
LOGI("[udp] UDP server_recv_recvmsg fragmentation, MTU at least be: " SSIZE_FMT,
buf->len + PACKET_HEADER_SIZE);
}
}
@ -1182,9 +1184,9 @@ server_recv_cb(EV_P_ ev_io *w, int revents)
#endif
// Init remote_ctx
remote_ctx = new_remote(remotefd, server_ctx);
remote_ctx->src_addr = src_addr;
remote_ctx->af = remote_addr->sa_family;
remote_ctx = new_remote(remotefd, server_ctx);
remote_ctx->src_addr = src_addr;
remote_ctx->af = remote_addr->sa_family;
// Add to conn cache
cache_insert(conn_cache, key, HASH_KEY_LEN, (void *)remote_ctx);
@ -1228,7 +1230,8 @@ server_recv_cb(EV_P_ ev_io *w, int revents)
if (buf->len - addr_header_len > packet_size) {
if (verbose) {
LOGI("[udp] server_recv_sendto fragmentation, MTU at least be: " SSIZE_FMT, buf->len - addr_header_len + PACKET_HEADER_SIZE);
LOGI("[udp] server_recv_sendto fragmentation, MTU at least be: " SSIZE_FMT,
buf->len - addr_header_len + PACKET_HEADER_SIZE);
}
}

8
src/utils.c

@ -64,6 +64,7 @@ ERROR(const char *s)
char *msg = strerror(errno);
LOGE("%s: %s", s, msg);
}
#endif
int use_tty = 1;
@ -274,7 +275,6 @@ ss_is_ipv6addr(const char *addr)
return strcmp(addr, ":") > 0;
}
void
usage()
{
@ -500,7 +500,7 @@ get_default_conf(void)
#ifndef __MINGW32__
static char sysconf[] = "/etc/shadowsocks-libev/config.json";
static char *userconf = NULL;
static int buf_size = 0;
static int buf_size = 0;
char *conf_home;
conf_home = getenv("XDG_CONFIG_HOME");
@ -513,14 +513,14 @@ get_default_conf(void)
userconf = malloc(buf_size);
}
snprintf(userconf, buf_size, "%s%s", getenv("HOME"),
"/.config/shadowsocks-libev/config.json");
"/.config/shadowsocks-libev/config.json");
} else {
if (buf_size == 0) {
buf_size = 50 + strlen(conf_home);
userconf = malloc(buf_size);
}
snprintf(userconf, buf_size, "%s%s", conf_home,
"/shadowsocks-libev/config.json");
"/shadowsocks-libev/config.json");
}
// Check if the user-specific config exists.

1
src/utils.h

@ -119,7 +119,6 @@ extern FILE *logfile;
} \
while (0)
#else // not __MINGW32__
#include <syslog.h>

13
src/winsock.c

@ -44,7 +44,7 @@
static void
disable_quick_edit(void)
{
DWORD mode = 0;
DWORD mode = 0;
HANDLE console = GetStdHandle(STD_INPUT_HANDLE);
// Get current console mode
@ -118,7 +118,7 @@ ss_gai_strerror(int ecode)
FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_MAX_WIDTH_MASK,
NULL, ecode,
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)buff, GAI_STRERROR_BUFFER_SIZE, NULL);
return (char *)buff;
}
@ -126,13 +126,13 @@ ss_gai_strerror(int ecode)
static BOOL
get_conattr(HANDLE console, WORD *out_attr)
{
static BOOL done = FALSE;
static BOOL done = FALSE;
static WORD saved_attr = 0;
if (!done) {
CONSOLE_SCREEN_BUFFER_INFO info;
if (GetConsoleScreenBufferInfo(console, &info)) {
saved_attr = info.wAttributes;
done = TRUE;
done = TRUE;
}
}
if (out_attr != NULL) {
@ -225,12 +225,12 @@ winsock_dummybind(SOCKET fd, struct sockaddr *sa)
memset(&ss, 0, sizeof(ss));
if (sa->sa_family == AF_INET) {
struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
sin->sin_family = AF_INET;
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = INADDR_ANY;
} else if (sa->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
sin6->sin6_family = AF_INET6;
sin6->sin6_addr = in6addr_any;
sin6->sin6_addr = in6addr_any;
} else {
return -1;
}
@ -240,6 +240,7 @@ winsock_dummybind(SOCKET fd, struct sockaddr *sa)
}
return 0;
}
#endif
#endif // __MINGW32__

8
src/winsock.h

@ -89,8 +89,8 @@
#define close(fd) closesocket(fd)
// Override MinGW functions
#define setsockopt(a,b,c,d,e) setsockopt(a,b,c,(const char *)(d),e)
#define inet_ntop(a,b,c,d) inet_ntop(a,(void *)(b),c,d)
#define setsockopt(a, b, c, d, e) setsockopt(a, b, c, (const char *)(d), e)
#define inet_ntop(a, b, c, d) inet_ntop(a, (void *)(b), c, d)
// Override Windows built-in functions
#ifdef ERROR
@ -106,8 +106,8 @@ char *ss_gai_strerror(int ecode);
// Missing Unix functions
#define sleep(x) Sleep((x) * 1000)
#define bzero(s,n) memset(s,0,n)
#define strndup(s,n) ss_strndup(s,n)
#define bzero(s, n) memset(s, 0, n)
#define strndup(s, n) ss_strndup(s, n)
// Winsock compatibility functions
int setnonblocking(SOCKET socket);

Loading…
Cancel
Save