mirror of
https://github.com/shadowsocks/shadowsocks-libev.git
synced 2026-10-28 10:44:43 +08:00
Clean up
This commit is contained in:
23 files changed
+603
-366
No files matched your search
+1
-1
@@ -896,7 +896,7 @@ nl_class_brace = ignore # ignore/add/remove/force
|
||||
nl_class_init_args = ignore # ignore/add/remove/force
|
||||
|
||||
# Add or remove newline between return type and function name in a function definition
|
||||
nl_func_type_name = remove # ignore/add/remove/force
|
||||
nl_func_type_name = force # ignore/add/remove/force
|
||||
|
||||
# Add or remove newline between return type and function name inside a class {}
|
||||
# Uses nl_func_type_name or nl_func_proto_type_name if set to ignore.
|
||||
|
||||
@@ -38,7 +38,8 @@ static struct cork_dllist white_list_rules;
|
||||
|
||||
static int acl_mode = BLACK_LIST;
|
||||
|
||||
static void parse_addr_cidr(const char *str, char *host, int *cidr)
|
||||
static void
|
||||
parse_addr_cidr(const char *str, char *host, int *cidr)
|
||||
{
|
||||
int ret = -1, n = 0;
|
||||
char *pch;
|
||||
@@ -59,27 +60,31 @@ static void parse_addr_cidr(const char *str, char *host, int *cidr)
|
||||
}
|
||||
}
|
||||
|
||||
char *trimwhitespace(char *str)
|
||||
char *
|
||||
trimwhitespace(char *str)
|
||||
{
|
||||
char *end;
|
||||
char *end;
|
||||
|
||||
// Trim leading space
|
||||
while(isspace(*str)) str++;
|
||||
// Trim leading space
|
||||
while (isspace(*str))
|
||||
str++;
|
||||
|
||||
if(*str == 0) // All spaces?
|
||||
return str;
|
||||
if (*str == 0) // All spaces?
|
||||
return str;
|
||||
|
||||
// Trim trailing space
|
||||
end = str + strlen(str) - 1;
|
||||
while(end > str && isspace(*end)) end--;
|
||||
// Trim trailing space
|
||||
end = str + strlen(str) - 1;
|
||||
while (end > str && isspace(*end))
|
||||
end--;
|
||||
|
||||
// Write new null terminator
|
||||
*(end+1) = 0;
|
||||
// Write new null terminator
|
||||
*(end + 1) = 0;
|
||||
|
||||
return str;
|
||||
return str;
|
||||
}
|
||||
|
||||
int init_acl(const char *path)
|
||||
int
|
||||
init_acl(const char *path)
|
||||
{
|
||||
// initialize ipset
|
||||
ipset_init_library();
|
||||
@@ -123,23 +128,23 @@ int init_acl(const char *path)
|
||||
}
|
||||
|
||||
if (strcmp(line, "[black_list]") == 0
|
||||
|| strcmp(line, "[bypass_list]") == 0) {
|
||||
|| strcmp(line, "[bypass_list]") == 0) {
|
||||
list_ipv4 = &black_list_ipv4;
|
||||
list_ipv6 = &black_list_ipv6;
|
||||
rules = &black_list_rules;
|
||||
continue;
|
||||
} else if (strcmp(line, "[white_list]") == 0
|
||||
|| strcmp(line, "[proxy_list]") == 0) {
|
||||
|| strcmp(line, "[proxy_list]") == 0) {
|
||||
list_ipv4 = &white_list_ipv4;
|
||||
list_ipv6 = &white_list_ipv6;
|
||||
rules = &white_list_rules;
|
||||
continue;
|
||||
} else if (strcmp(line, "[reject_all]") == 0
|
||||
|| strcmp(line, "[bypass_all]") == 0) {
|
||||
|| strcmp(line, "[bypass_all]") == 0) {
|
||||
acl_mode = WHITE_LIST;
|
||||
continue;
|
||||
} else if (strcmp(line, "[accept_all]") == 0
|
||||
|| strcmp(line, "[proxy_all]") == 0) {
|
||||
|| strcmp(line, "[proxy_all]") == 0) {
|
||||
acl_mode = BLACK_LIST;
|
||||
continue;
|
||||
}
|
||||
@@ -177,7 +182,8 @@ int init_acl(const char *path)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void free_rules(struct cork_dllist *rules)
|
||||
void
|
||||
free_rules(struct cork_dllist *rules)
|
||||
{
|
||||
struct cork_dllist_item *iter;
|
||||
while ((iter = cork_dllist_head(rules)) != NULL) {
|
||||
@@ -186,7 +192,8 @@ void free_rules(struct cork_dllist *rules)
|
||||
}
|
||||
}
|
||||
|
||||
void free_acl(void)
|
||||
void
|
||||
free_acl(void)
|
||||
{
|
||||
ipset_done(&black_list_ipv4);
|
||||
ipset_done(&black_list_ipv6);
|
||||
@@ -197,7 +204,8 @@ void free_acl(void)
|
||||
free_rules(&white_list_rules);
|
||||
}
|
||||
|
||||
int get_acl_mode(void)
|
||||
int
|
||||
get_acl_mode(void)
|
||||
{
|
||||
return acl_mode;
|
||||
}
|
||||
@@ -207,7 +215,8 @@ int get_acl_mode(void)
|
||||
* Return 1, if match black list.
|
||||
* Return -1, if match white list.
|
||||
*/
|
||||
int acl_match_host(const char *host)
|
||||
int
|
||||
acl_match_host(const char *host)
|
||||
{
|
||||
struct cork_ip addr;
|
||||
int ret = 0;
|
||||
@@ -237,7 +246,8 @@ int acl_match_host(const char *host)
|
||||
return ret;
|
||||
}
|
||||
|
||||
int acl_add_ip(const char *ip)
|
||||
int
|
||||
acl_add_ip(const char *ip)
|
||||
{
|
||||
struct cork_ip addr;
|
||||
int err = cork_ip_init(&addr, ip);
|
||||
@@ -254,7 +264,8 @@ int acl_add_ip(const char *ip)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int acl_remove_ip(const char *ip)
|
||||
int
|
||||
acl_remove_ip(const char *ip)
|
||||
{
|
||||
struct cork_ip addr;
|
||||
int err = cork_ip_init(&addr, ip);
|
||||
|
||||
+4
-2
@@ -47,7 +47,8 @@
|
||||
|
||||
extern char *prefix;
|
||||
|
||||
int protect_socket(int fd)
|
||||
int
|
||||
protect_socket(int fd)
|
||||
{
|
||||
int sock;
|
||||
struct sockaddr_un addr;
|
||||
@@ -96,7 +97,8 @@ int protect_socket(int fd)
|
||||
return ret;
|
||||
}
|
||||
|
||||
int send_traffic_stat(uint64_t tx, uint64_t rx)
|
||||
int
|
||||
send_traffic_stat(uint64_t tx, uint64_t rx)
|
||||
{
|
||||
int sock;
|
||||
struct sockaddr_un addr;
|
||||
|
||||
+13
-7
@@ -45,8 +45,9 @@
|
||||
*
|
||||
* @return EINVAL if dst is NULL, ENOMEM if malloc fails, 0 otherwise
|
||||
*/
|
||||
int cache_create(struct cache **dst, const size_t capacity,
|
||||
void (*free_cb)(void *element))
|
||||
int
|
||||
cache_create(struct cache **dst, const size_t capacity,
|
||||
void (*free_cb)(void *element))
|
||||
{
|
||||
struct cache *new = NULL;
|
||||
|
||||
@@ -75,7 +76,8 @@ int cache_create(struct cache **dst, const size_t capacity,
|
||||
*
|
||||
* @return EINVAL if cache is NULL, 0 otherwise
|
||||
*/
|
||||
int cache_delete(struct cache *cache, int keep_data)
|
||||
int
|
||||
cache_delete(struct cache *cache, int keep_data)
|
||||
{
|
||||
struct cache_entry *entry, *tmp;
|
||||
|
||||
@@ -115,7 +117,8 @@ int cache_delete(struct cache *cache, int keep_data)
|
||||
*
|
||||
* @return EINVAL if cache is NULL, 0 otherwise
|
||||
*/
|
||||
int cache_remove(struct cache *cache, char *key, size_t key_len)
|
||||
int
|
||||
cache_remove(struct cache *cache, char *key, size_t key_len)
|
||||
{
|
||||
struct cache_entry *tmp;
|
||||
|
||||
@@ -161,7 +164,8 @@ int cache_remove(struct cache *cache, char *key, size_t key_len)
|
||||
*
|
||||
* @return EINVAL if cache is NULL, 0 otherwise
|
||||
*/
|
||||
int cache_lookup(struct cache *cache, char *key, size_t key_len, void *result)
|
||||
int
|
||||
cache_lookup(struct cache *cache, char *key, size_t key_len, void *result)
|
||||
{
|
||||
struct cache_entry *tmp = NULL;
|
||||
char **dirty_hack = result;
|
||||
@@ -182,7 +186,8 @@ int cache_lookup(struct cache *cache, char *key, size_t key_len, void *result)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cache_key_exist(struct cache *cache, char *key, size_t key_len)
|
||||
int
|
||||
cache_key_exist(struct cache *cache, char *key, size_t key_len)
|
||||
{
|
||||
struct cache_entry *tmp = NULL;
|
||||
|
||||
@@ -218,7 +223,8 @@ int cache_key_exist(struct cache *cache, char *key, size_t key_len)
|
||||
*
|
||||
* @return EINVAL if cache is NULL, ENOMEM if malloc fails, 0 otherwise
|
||||
*/
|
||||
int cache_insert(struct cache *cache, char *key, size_t key_len, void *data)
|
||||
int
|
||||
cache_insert(struct cache *cache, char *key, size_t key_len, void *data)
|
||||
{
|
||||
struct cache_entry *entry = NULL;
|
||||
struct cache_entry *tmp_entry = NULL;
|
||||
|
||||
+75
-42
@@ -89,7 +89,8 @@ static int enc_method;
|
||||
static struct cache *iv_cache;
|
||||
|
||||
#ifdef DEBUG
|
||||
static void dump(char *tag, char *text, int len)
|
||||
static void
|
||||
dump(char *tag, char *text, int len)
|
||||
{
|
||||
int i;
|
||||
printf("%s: ", tag);
|
||||
@@ -234,7 +235,8 @@ static const int supported_ciphers_key_size[CIPHER_NUM] = {
|
||||
0, 16, 16, 16, 24, 32, 16, 24, 32, 16, 16, 24, 32, 16, 8, 16, 16, 16, 32, 32, 32
|
||||
};
|
||||
|
||||
static int safe_memcmp(const void *s1, const void *s2, size_t n)
|
||||
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;
|
||||
@@ -245,7 +247,8 @@ static int safe_memcmp(const void *s1, const void *s2, size_t n)
|
||||
return !!ret;
|
||||
}
|
||||
|
||||
int balloc(buffer_t *ptr, size_t capacity)
|
||||
int
|
||||
balloc(buffer_t *ptr, size_t capacity)
|
||||
{
|
||||
sodium_memzero(ptr, sizeof(buffer_t));
|
||||
ptr->array = ss_malloc(capacity);
|
||||
@@ -253,7 +256,8 @@ int balloc(buffer_t *ptr, size_t capacity)
|
||||
return capacity;
|
||||
}
|
||||
|
||||
int brealloc(buffer_t *ptr, size_t len, size_t capacity)
|
||||
int
|
||||
brealloc(buffer_t *ptr, size_t len, size_t capacity)
|
||||
{
|
||||
if (ptr == NULL)
|
||||
return -1;
|
||||
@@ -265,7 +269,8 @@ int brealloc(buffer_t *ptr, size_t len, size_t capacity)
|
||||
return real_capacity;
|
||||
}
|
||||
|
||||
void bfree(buffer_t *ptr)
|
||||
void
|
||||
bfree(buffer_t *ptr)
|
||||
{
|
||||
if (ptr == NULL)
|
||||
return;
|
||||
@@ -277,9 +282,10 @@ void bfree(buffer_t *ptr)
|
||||
}
|
||||
}
|
||||
|
||||
static int crypto_stream_xor_ic(uint8_t *c, const uint8_t *m, uint64_t mlen,
|
||||
const uint8_t *n, uint64_t ic, const uint8_t *k,
|
||||
int method)
|
||||
static int
|
||||
crypto_stream_xor_ic(uint8_t *c, const uint8_t *m, uint64_t mlen,
|
||||
const uint8_t *n, uint64_t ic, const uint8_t *k,
|
||||
int method)
|
||||
{
|
||||
switch (method) {
|
||||
case SALSA20:
|
||||
@@ -293,16 +299,18 @@ static int crypto_stream_xor_ic(uint8_t *c, const uint8_t *m, uint64_t mlen,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int random_compare(const void *_x, const void *_y, uint32_t i,
|
||||
uint64_t a)
|
||||
static int
|
||||
random_compare(const void *_x, const void *_y, uint32_t i,
|
||||
uint64_t a)
|
||||
{
|
||||
uint8_t x = *((uint8_t *)_x);
|
||||
uint8_t y = *((uint8_t *)_y);
|
||||
return a % (x + i) - a % (y + i);
|
||||
}
|
||||
|
||||
static void merge(uint8_t *left, int llength, uint8_t *right,
|
||||
int rlength, uint32_t salt, uint64_t key)
|
||||
static void
|
||||
merge(uint8_t *left, int llength, uint8_t *right,
|
||||
int rlength, uint32_t salt, uint64_t key)
|
||||
{
|
||||
uint8_t *ltmp = (uint8_t *)malloc(llength * sizeof(uint8_t));
|
||||
uint8_t *rtmp = (uint8_t *)malloc(rlength * sizeof(uint8_t));
|
||||
@@ -348,8 +356,9 @@ static void merge(uint8_t *left, int llength, uint8_t *right,
|
||||
ss_free(rtmp);
|
||||
}
|
||||
|
||||
static void merge_sort(uint8_t array[], int length,
|
||||
uint32_t salt, uint64_t key)
|
||||
static void
|
||||
merge_sort(uint8_t array[], int length,
|
||||
uint32_t salt, uint64_t key)
|
||||
{
|
||||
uint8_t middle;
|
||||
uint8_t *left, *right;
|
||||
@@ -371,12 +380,14 @@ static void merge_sort(uint8_t array[], int length,
|
||||
merge(left, llength, right, middle, salt, key);
|
||||
}
|
||||
|
||||
int enc_get_iv_len()
|
||||
int
|
||||
enc_get_iv_len()
|
||||
{
|
||||
return enc_iv_len;
|
||||
}
|
||||
|
||||
unsigned char *enc_md5(const unsigned char *d, size_t n, unsigned char *md)
|
||||
unsigned char *
|
||||
enc_md5(const unsigned char *d, size_t n, unsigned char *md)
|
||||
{
|
||||
#if defined(USE_CRYPTO_OPENSSL)
|
||||
return MD5(d, n, md);
|
||||
@@ -397,7 +408,8 @@ unsigned char *enc_md5(const unsigned char *d, size_t n, unsigned char *md)
|
||||
#endif
|
||||
}
|
||||
|
||||
void enc_table_init(const char *pass)
|
||||
void
|
||||
enc_table_init(const char *pass)
|
||||
{
|
||||
uint32_t i;
|
||||
uint64_t key = 0;
|
||||
@@ -420,7 +432,8 @@ void enc_table_init(const char *pass)
|
||||
dec_table[enc_table[i]] = i;
|
||||
}
|
||||
|
||||
int cipher_iv_size(const cipher_kt_t *cipher)
|
||||
int
|
||||
cipher_iv_size(const cipher_kt_t *cipher)
|
||||
{
|
||||
#if defined(USE_CRYPTO_OPENSSL)
|
||||
return EVP_CIPHER_iv_length(cipher);
|
||||
@@ -432,7 +445,8 @@ int cipher_iv_size(const cipher_kt_t *cipher)
|
||||
#endif
|
||||
}
|
||||
|
||||
int cipher_key_size(const cipher_kt_t *cipher)
|
||||
int
|
||||
cipher_key_size(const cipher_kt_t *cipher)
|
||||
{
|
||||
#if defined(USE_CRYPTO_OPENSSL)
|
||||
return EVP_CIPHER_key_length(cipher);
|
||||
@@ -463,8 +477,9 @@ int cipher_key_size(const cipher_kt_t *cipher)
|
||||
#endif
|
||||
}
|
||||
|
||||
int bytes_to_key(const cipher_kt_t *cipher, const digest_type_t *md,
|
||||
const uint8_t *pass, uint8_t *key, uint8_t *iv)
|
||||
int
|
||||
bytes_to_key(const cipher_kt_t *cipher, const digest_type_t *md,
|
||||
const uint8_t *pass, uint8_t *key, uint8_t *iv)
|
||||
{
|
||||
size_t datal;
|
||||
datal = strlen((const char *)pass);
|
||||
@@ -679,7 +694,8 @@ int bytes_to_key(const cipher_kt_t *cipher, const digest_type_t *md,
|
||||
#endif
|
||||
}
|
||||
|
||||
int rand_bytes(uint8_t *output, int len)
|
||||
int
|
||||
rand_bytes(uint8_t *output, int len)
|
||||
{
|
||||
#if defined(USE_CRYPTO_OPENSSL)
|
||||
return RAND_bytes(output, len);
|
||||
@@ -805,7 +821,8 @@ int rand_bytes(uint8_t *output, int len)
|
||||
#endif
|
||||
}
|
||||
|
||||
const cipher_kt_t *get_cipher_type(int method)
|
||||
const cipher_kt_t *
|
||||
get_cipher_type(int method)
|
||||
{
|
||||
if (method <= TABLE || method >= CIPHER_NUM) {
|
||||
LOGE("get_cipher_type(): Illegal method");
|
||||
@@ -842,7 +859,8 @@ const cipher_kt_t *get_cipher_type(int method)
|
||||
#endif
|
||||
}
|
||||
|
||||
const digest_type_t *get_digest_type(const char *digest)
|
||||
const digest_type_t *
|
||||
get_digest_type(const char *digest)
|
||||
{
|
||||
if (digest == NULL) {
|
||||
LOGE("get_digest_type(): Digest name is null");
|
||||
@@ -858,7 +876,8 @@ const digest_type_t *get_digest_type(const char *digest)
|
||||
#endif
|
||||
}
|
||||
|
||||
void cipher_context_init(cipher_ctx_t *ctx, int method, int enc)
|
||||
void
|
||||
cipher_context_init(cipher_ctx_t *ctx, int method, int enc)
|
||||
{
|
||||
if (method <= TABLE || method >= CIPHER_NUM) {
|
||||
LOGE("cipher_context_init(): Illegal method");
|
||||
@@ -883,7 +902,7 @@ void cipher_context_init(cipher_ctx_t *ctx, int method, int enc)
|
||||
cc->mode = supported_modes_applecc[method];
|
||||
cc->padding = ccNoPadding;
|
||||
} else {
|
||||
cc->mode = supported_modes_applecc[method];
|
||||
cc->mode = supported_modes_applecc[method];
|
||||
if (cc->mode == kCCModeCTR) {
|
||||
cc->padding = ccNoPadding;
|
||||
} else {
|
||||
@@ -938,8 +957,9 @@ void cipher_context_init(cipher_ctx_t *ctx, int method, int enc)
|
||||
#endif
|
||||
}
|
||||
|
||||
void cipher_context_set_iv(cipher_ctx_t *ctx, uint8_t *iv, size_t iv_len,
|
||||
int enc)
|
||||
void
|
||||
cipher_context_set_iv(cipher_ctx_t *ctx, uint8_t *iv, size_t iv_len,
|
||||
int enc)
|
||||
{
|
||||
const unsigned char *true_key;
|
||||
|
||||
@@ -1051,7 +1071,8 @@ void cipher_context_set_iv(cipher_ctx_t *ctx, uint8_t *iv, size_t iv_len,
|
||||
#endif
|
||||
}
|
||||
|
||||
void cipher_context_release(cipher_ctx_t *ctx)
|
||||
void
|
||||
cipher_context_release(cipher_ctx_t *ctx)
|
||||
{
|
||||
if (enc_method >= SALSA20) {
|
||||
return;
|
||||
@@ -1080,8 +1101,9 @@ void cipher_context_release(cipher_ctx_t *ctx)
|
||||
#endif
|
||||
}
|
||||
|
||||
static int cipher_context_update(cipher_ctx_t *ctx, uint8_t *output, size_t *olen,
|
||||
const uint8_t *input, size_t ilen)
|
||||
static int
|
||||
cipher_context_update(cipher_ctx_t *ctx, uint8_t *output, size_t *olen,
|
||||
const uint8_t *input, size_t ilen)
|
||||
{
|
||||
#ifdef USE_CRYPTO_APPLECC
|
||||
cipher_cc_t *cc = &ctx->cc;
|
||||
@@ -1108,7 +1130,8 @@ static int cipher_context_update(cipher_ctx_t *ctx, uint8_t *output, size_t *ole
|
||||
#endif
|
||||
}
|
||||
|
||||
int ss_onetimeauth(buffer_t *buf, uint8_t *iv, size_t capacity)
|
||||
int
|
||||
ss_onetimeauth(buffer_t *buf, uint8_t *iv, size_t capacity)
|
||||
{
|
||||
uint8_t hash[ONETIMEAUTH_BYTES * 2];
|
||||
uint8_t auth_key[MAX_IV_LENGTH + MAX_KEY_LENGTH];
|
||||
@@ -1133,7 +1156,8 @@ int ss_onetimeauth(buffer_t *buf, uint8_t *iv, size_t capacity)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ss_onetimeauth_verify(buffer_t *buf, uint8_t *iv)
|
||||
int
|
||||
ss_onetimeauth_verify(buffer_t *buf, uint8_t *iv)
|
||||
{
|
||||
uint8_t hash[ONETIMEAUTH_BYTES * 2];
|
||||
uint8_t auth_key[MAX_IV_LENGTH + MAX_KEY_LENGTH];
|
||||
@@ -1153,7 +1177,8 @@ int ss_onetimeauth_verify(buffer_t *buf, uint8_t *iv)
|
||||
return safe_memcmp(buf->array + len, hash, ONETIMEAUTH_BYTES);
|
||||
}
|
||||
|
||||
int ss_encrypt_all(buffer_t *plain, int method, int auth, size_t capacity)
|
||||
int
|
||||
ss_encrypt_all(buffer_t *plain, int method, int auth, size_t capacity)
|
||||
{
|
||||
if (method > TABLE) {
|
||||
cipher_ctx_t evp;
|
||||
@@ -1218,7 +1243,8 @@ int ss_encrypt_all(buffer_t *plain, int method, int auth, size_t capacity)
|
||||
}
|
||||
}
|
||||
|
||||
int ss_encrypt(buffer_t *plain, enc_ctx_t *ctx, size_t capacity)
|
||||
int
|
||||
ss_encrypt(buffer_t *plain, enc_ctx_t *ctx, size_t capacity)
|
||||
{
|
||||
if (ctx != NULL) {
|
||||
static buffer_t tmp = { 0, 0, 0, NULL };
|
||||
@@ -1291,7 +1317,8 @@ int ss_encrypt(buffer_t *plain, enc_ctx_t *ctx, size_t capacity)
|
||||
}
|
||||
}
|
||||
|
||||
int ss_decrypt_all(buffer_t *cipher, int method, int auth, size_t capacity)
|
||||
int
|
||||
ss_decrypt_all(buffer_t *cipher, int method, int auth, size_t capacity)
|
||||
{
|
||||
if (method > TABLE) {
|
||||
size_t iv_len = enc_iv_len;
|
||||
@@ -1364,7 +1391,8 @@ int ss_decrypt_all(buffer_t *cipher, int method, int auth, size_t capacity)
|
||||
}
|
||||
}
|
||||
|
||||
int ss_decrypt(buffer_t *cipher, enc_ctx_t *ctx, size_t capacity)
|
||||
int
|
||||
ss_decrypt(buffer_t *cipher, enc_ctx_t *ctx, size_t capacity)
|
||||
{
|
||||
if (ctx != NULL) {
|
||||
static buffer_t tmp = { 0, 0, 0, NULL };
|
||||
@@ -1448,7 +1476,8 @@ int ss_decrypt(buffer_t *cipher, enc_ctx_t *ctx, size_t capacity)
|
||||
}
|
||||
}
|
||||
|
||||
void enc_ctx_init(int method, enc_ctx_t *ctx, int enc)
|
||||
void
|
||||
enc_ctx_init(int method, enc_ctx_t *ctx, int enc)
|
||||
{
|
||||
sodium_memzero(ctx, sizeof(enc_ctx_t));
|
||||
cipher_context_init(&ctx->evp, method, enc);
|
||||
@@ -1458,7 +1487,8 @@ void enc_ctx_init(int method, enc_ctx_t *ctx, int enc)
|
||||
}
|
||||
}
|
||||
|
||||
void enc_key_init(int method, const char *pass)
|
||||
void
|
||||
enc_key_init(int method, const char *pass)
|
||||
{
|
||||
if (method <= TABLE || method >= CIPHER_NUM) {
|
||||
LOGE("enc_key_init(): Illegal method");
|
||||
@@ -1546,7 +1576,8 @@ void enc_key_init(int method, const char *pass)
|
||||
enc_method = method;
|
||||
}
|
||||
|
||||
int enc_init(const char *pass, const char *method)
|
||||
int
|
||||
enc_init(const char *pass, const char *method)
|
||||
{
|
||||
int m = TABLE;
|
||||
if (method != NULL) {
|
||||
@@ -1567,7 +1598,8 @@ int enc_init(const char *pass, const char *method)
|
||||
return m;
|
||||
}
|
||||
|
||||
int ss_check_hash(buffer_t *buf, chunk_t *chunk, enc_ctx_t *ctx, size_t capacity)
|
||||
int
|
||||
ss_check_hash(buffer_t *buf, chunk_t *chunk, enc_ctx_t *ctx, size_t capacity)
|
||||
{
|
||||
int i, j, k;
|
||||
ssize_t blen = buf->len;
|
||||
@@ -1625,7 +1657,8 @@ int ss_check_hash(buffer_t *buf, chunk_t *chunk, enc_ctx_t *ctx, size_t capacity
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ss_gen_hash(buffer_t *buf, uint32_t *counter, enc_ctx_t *ctx, size_t capacity)
|
||||
int
|
||||
ss_gen_hash(buffer_t *buf, uint32_t *counter, enc_ctx_t *ctx, size_t capacity)
|
||||
{
|
||||
ssize_t blen = buf->len;
|
||||
uint16_t chunk_len = htons((uint16_t)blen);
|
||||
|
||||
+11
-8
@@ -44,7 +44,7 @@ static int get_header(const char *, const char *, int, char **);
|
||||
static int next_header(const char **, int *);
|
||||
|
||||
static const protocol_t http_protocol_st = {
|
||||
.default_port = 80,
|
||||
.default_port = 80,
|
||||
.parse_packet = &parse_http_header,
|
||||
};
|
||||
const protocol_t *const http_protocol = &http_protocol_st;
|
||||
@@ -63,7 +63,8 @@ const protocol_t *const http_protocol = &http_protocol_st;
|
||||
*
|
||||
*/
|
||||
static int
|
||||
parse_http_header(const char* data, size_t data_len, char **hostname) {
|
||||
parse_http_header(const char *data, size_t data_len, char **hostname)
|
||||
{
|
||||
int result, i;
|
||||
|
||||
if (hostname == NULL)
|
||||
@@ -84,7 +85,7 @@ parse_http_header(const char* data, size_t data_len, char **hostname) {
|
||||
for (i = result - 1; i >= 0; i--)
|
||||
if ((*hostname)[i] == ':') {
|
||||
(*hostname)[i] = '\0';
|
||||
result = i;
|
||||
result = i;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -92,7 +93,8 @@ parse_http_header(const char* data, size_t data_len, char **hostname) {
|
||||
}
|
||||
|
||||
static int
|
||||
get_header(const char *header, const char *data, int data_len, char **value) {
|
||||
get_header(const char *header, const char *data, int data_len, char **value)
|
||||
{
|
||||
int len, header_len;
|
||||
|
||||
header_len = strlen(header);
|
||||
@@ -123,7 +125,8 @@ get_header(const char *header, const char *data, int data_len, char **value) {
|
||||
}
|
||||
|
||||
static int
|
||||
next_header(const char **data, int *len) {
|
||||
next_header(const char **data, int *len)
|
||||
{
|
||||
int header_len;
|
||||
|
||||
/* perhaps we can optimize this to reuse the value of header_len, rather
|
||||
@@ -136,13 +139,13 @@ next_header(const char **data, int *len) {
|
||||
|
||||
/* advanced past the <CR><LF> pair */
|
||||
*data += 2;
|
||||
*len -= 2;
|
||||
*len -= 2;
|
||||
|
||||
/* Find the length of the next header */
|
||||
header_len = 0;
|
||||
while (*len > header_len + 1
|
||||
&& (*data)[header_len] != '\r'
|
||||
&& (*data)[header_len + 1] != '\n')
|
||||
&& (*data)[header_len] != '\r'
|
||||
&& (*data)[header_len + 1] != '\n')
|
||||
header_len++;
|
||||
|
||||
return header_len;
|
||||
|
||||
+8
-4
@@ -32,7 +32,8 @@
|
||||
|
||||
#include <libcork/core.h>
|
||||
|
||||
static char *to_string(const json_value *value)
|
||||
static char *
|
||||
to_string(const json_value *value)
|
||||
{
|
||||
if (value->type == json_string) {
|
||||
return ss_strndup(value->u.string.ptr, value->u.string.length);
|
||||
@@ -47,13 +48,15 @@ static char *to_string(const json_value *value)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void free_addr(ss_addr_t *addr)
|
||||
void
|
||||
free_addr(ss_addr_t *addr)
|
||||
{
|
||||
ss_free(addr->host);
|
||||
ss_free(addr->port);
|
||||
}
|
||||
|
||||
void parse_addr(const char *str, ss_addr_t *addr)
|
||||
void
|
||||
parse_addr(const char *str, ss_addr_t *addr)
|
||||
{
|
||||
int ipv6 = 0, ret = -1, n = 0;
|
||||
char *pch;
|
||||
@@ -95,7 +98,8 @@ void parse_addr(const char *str, ss_addr_t *addr)
|
||||
}
|
||||
}
|
||||
|
||||
jconf_t *read_jconf(const char *file)
|
||||
jconf_t *
|
||||
read_jconf(const char *file)
|
||||
{
|
||||
static jconf_t conf;
|
||||
|
||||
|
||||
+24
-15
@@ -39,7 +39,7 @@
|
||||
#ifdef __cplusplus
|
||||
const struct _json_value json_value_none; /* zero-d by ctor */
|
||||
#else
|
||||
const struct _json_value json_value_none = { NULL, 0, {0}, {NULL} };
|
||||
const struct _json_value json_value_none = { NULL, 0, { 0 }, { NULL } };
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
@@ -49,7 +49,8 @@ const struct _json_value json_value_none = { NULL, 0, {0}, {NULL} };
|
||||
|
||||
typedef unsigned short json_uchar;
|
||||
|
||||
static unsigned char hex_value(json_char c)
|
||||
static unsigned char
|
||||
hex_value(json_char c)
|
||||
{
|
||||
if (isdigit((uint8_t)c)) {
|
||||
return c - '0';
|
||||
@@ -89,17 +90,20 @@ typedef struct {
|
||||
int first_pass;
|
||||
} json_state;
|
||||
|
||||
static void *default_alloc(size_t size, int zero, void *user_data)
|
||||
static void *
|
||||
default_alloc(size_t size, int zero, void *user_data)
|
||||
{
|
||||
return zero ? calloc(1, size) : ss_malloc(size);
|
||||
}
|
||||
|
||||
static void default_free(void *ptr, void *user_data)
|
||||
static void
|
||||
default_free(void *ptr, void *user_data)
|
||||
{
|
||||
ss_free(ptr);
|
||||
}
|
||||
|
||||
static void *json_alloc(json_state *state, unsigned long size, int zero)
|
||||
static void *
|
||||
json_alloc(json_state *state, unsigned long size, int zero)
|
||||
{
|
||||
if ((state->ulong_max - state->used_memory) < size) {
|
||||
return 0;
|
||||
@@ -113,8 +117,9 @@ static void *json_alloc(json_state *state, unsigned long size, int zero)
|
||||
return state->settings.mem_alloc(size, zero, state->settings.user_data);
|
||||
}
|
||||
|
||||
static int new_value(json_state *state, json_value **top, json_value **root,
|
||||
json_value **alloc, json_type type)
|
||||
static int
|
||||
new_value(json_state *state, json_value **top, json_value **root,
|
||||
json_value **alloc, json_type type)
|
||||
{
|
||||
json_value *value;
|
||||
int values_size;
|
||||
@@ -231,16 +236,17 @@ static const long
|
||||
flag_line_comment = 1 << 13,
|
||||
flag_block_comment = 1 << 14;
|
||||
|
||||
json_value *json_parse_ex(json_settings *settings,
|
||||
const json_char *json,
|
||||
size_t length,
|
||||
char *error_buf)
|
||||
json_value *
|
||||
json_parse_ex(json_settings *settings,
|
||||
const json_char *json,
|
||||
size_t length,
|
||||
char *error_buf)
|
||||
{
|
||||
json_char error[json_error_max];
|
||||
unsigned int cur_line;
|
||||
const json_char *cur_line_begin, *i, *end;
|
||||
json_value *top, *root, *alloc = 0;
|
||||
json_state state = { 0UL, 0U, 0UL, {0UL, 0, NULL, NULL, NULL}, 0 };
|
||||
json_state state = { 0UL, 0U, 0UL, { 0UL, 0, NULL, NULL, NULL }, 0 };
|
||||
long flags;
|
||||
long num_digits = 0, num_e = 0;
|
||||
json_int_t num_fraction = 0;
|
||||
@@ -932,13 +938,15 @@ e_failed:
|
||||
return 0;
|
||||
}
|
||||
|
||||
json_value *json_parse(const json_char *json, size_t length)
|
||||
json_value *
|
||||
json_parse(const json_char *json, size_t length)
|
||||
{
|
||||
json_settings settings = { 0UL, 0, NULL, NULL, NULL };
|
||||
return json_parse_ex(&settings, json, length, 0);
|
||||
}
|
||||
|
||||
void json_value_free_ex(json_settings *settings, json_value *value)
|
||||
void
|
||||
json_value_free_ex(json_settings *settings, json_value *value)
|
||||
{
|
||||
json_value *cur_value;
|
||||
|
||||
@@ -985,7 +993,8 @@ void json_value_free_ex(json_settings *settings, json_value *value)
|
||||
}
|
||||
}
|
||||
|
||||
void json_value_free(json_value *value)
|
||||
void
|
||||
json_value_free(json_value *value)
|
||||
{
|
||||
json_settings settings = { 0UL, 0, NULL, NULL, NULL };
|
||||
settings.mem_free = default_free;
|
||||
|
||||
+4
-2
@@ -183,7 +183,8 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
inline operator json_int_t() const
|
||||
inline operator
|
||||
json_int_t() const
|
||||
{
|
||||
switch (type) {
|
||||
case json_integer:
|
||||
@@ -197,7 +198,8 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
inline operator bool() const
|
||||
inline operator
|
||||
bool() const
|
||||
{
|
||||
if (type != json_boolean) {
|
||||
return false;
|
||||
|
||||
+65
-41
@@ -123,7 +123,8 @@ static server_t *new_server(int fd, int method);
|
||||
static struct cork_dllist connections;
|
||||
|
||||
#ifndef __MINGW32__
|
||||
int setnonblocking(int fd)
|
||||
int
|
||||
setnonblocking(int fd)
|
||||
{
|
||||
int flags;
|
||||
if (-1 == (flags = fcntl(fd, F_GETFL, 0))) {
|
||||
@@ -134,7 +135,8 @@ int setnonblocking(int fd)
|
||||
|
||||
#endif
|
||||
|
||||
int create_and_bind(const char *addr, const char *port)
|
||||
int
|
||||
create_and_bind(const char *addr, const char *port)
|
||||
{
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *result, *rp;
|
||||
@@ -187,7 +189,8 @@ int create_and_bind(const char *addr, const char *port)
|
||||
return listen_sock;
|
||||
}
|
||||
|
||||
static void free_connections(struct ev_loop *loop)
|
||||
static void
|
||||
free_connections(struct ev_loop *loop)
|
||||
{
|
||||
struct cork_dllist_item *curr, *next;
|
||||
cork_dllist_foreach_void(&connections, curr, next) {
|
||||
@@ -198,7 +201,8 @@ static void free_connections(struct ev_loop *loop)
|
||||
}
|
||||
}
|
||||
|
||||
static void server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
server_ctx_t *server_recv_ctx = (server_ctx_t *)w;
|
||||
server_t *server = server_recv_ctx->server;
|
||||
@@ -208,7 +212,6 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
|
||||
if (remote == NULL) {
|
||||
buf = server->buf;
|
||||
|
||||
} else {
|
||||
buf = remote->buf;
|
||||
}
|
||||
@@ -362,7 +365,6 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
exit(1);
|
||||
#endif
|
||||
}
|
||||
|
||||
} else {
|
||||
int s = send(remote->fd, remote->buf->array, remote->buf->len, 0);
|
||||
if (s == -1) {
|
||||
@@ -508,13 +510,13 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
|
||||
memcpy(resp_buf->array, &response, sizeof(struct socks5_response));
|
||||
memcpy(resp_buf->array + sizeof(struct socks5_response),
|
||||
&sock_addr.sin_addr, sizeof(sock_addr.sin_addr));
|
||||
&sock_addr.sin_addr, sizeof(sock_addr.sin_addr));
|
||||
memcpy(resp_buf->array + sizeof(struct socks5_response) +
|
||||
sizeof(sock_addr.sin_addr),
|
||||
&sock_addr.sin_port, sizeof(sock_addr.sin_port));
|
||||
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->array, reply_size, 0);
|
||||
|
||||
@@ -535,19 +537,19 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
}
|
||||
}
|
||||
|
||||
size_t abuf_len = abuf->len;
|
||||
size_t abuf_len = abuf->len;
|
||||
int sni_detected = 0;
|
||||
|
||||
if (atyp == 1 || atyp == 4) {
|
||||
char *hostname;
|
||||
uint16_t p = ntohs(*(uint16_t *)(abuf->array + abuf->len - 2));
|
||||
int ret = 0;
|
||||
int ret = 0;
|
||||
if (p == http_protocol->default_port)
|
||||
ret = http_protocol->parse_packet(buf->array + 3 + abuf->len,
|
||||
buf->len - 3 - abuf->len, &hostname);
|
||||
buf->len - 3 - abuf->len, &hostname);
|
||||
else if (p == tls_protocol->default_port)
|
||||
ret = tls_protocol->parse_packet(buf->array + 3 + abuf->len,
|
||||
buf->len - 3 - abuf->len, &hostname);
|
||||
buf->len - 3 - abuf->len, &hostname);
|
||||
if (ret == -1) {
|
||||
server->stage = 2;
|
||||
bfree(abuf);
|
||||
@@ -556,12 +558,12 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
sni_detected = 1;
|
||||
|
||||
// Reconstruct address buffer
|
||||
abuf->len = 0;
|
||||
abuf->len = 0;
|
||||
abuf->array[abuf->len++] = 3;
|
||||
abuf->array[abuf->len++] = ret;
|
||||
memcpy(abuf->array + abuf->len, hostname, ret);
|
||||
abuf->len += ret;
|
||||
p = htons(p);
|
||||
p = htons(p);
|
||||
memcpy(abuf->array + abuf->len, &p, 2);
|
||||
abuf->len += 2;
|
||||
|
||||
@@ -597,16 +599,21 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
int bypass = get_acl_mode() == WHITE_LIST;
|
||||
|
||||
if (get_acl_mode() == BLACK_LIST) {
|
||||
if (ip_match > 0) bypass = 1; // bypass IPs in black list
|
||||
|
||||
if (host_match > 0) bypass = 1; // bypass hostnames in black list
|
||||
else if (host_match < 0) bypass = 0; // proxy hostnames in white list
|
||||
if (ip_match > 0)
|
||||
bypass = 1; // bypass IPs in black list
|
||||
|
||||
if (host_match > 0)
|
||||
bypass = 1; // bypass hostnames in black list
|
||||
else if (host_match < 0)
|
||||
bypass = 0; // proxy hostnames in white list
|
||||
} else if (get_acl_mode() == WHITE_LIST) {
|
||||
if (ip_match < 0) bypass = 0; // proxy IPs in white list
|
||||
if (ip_match < 0)
|
||||
bypass = 0; // proxy IPs in white list
|
||||
|
||||
if (host_match < 0) bypass = 0; // proxy hostnames in white list
|
||||
else if (host_match > 0) bypass = 1; // bypass hostnames in black list
|
||||
if (host_match < 0)
|
||||
bypass = 0; // proxy hostnames in white list
|
||||
else if (host_match > 0)
|
||||
bypass = 1; // bypass hostnames in black list
|
||||
}
|
||||
|
||||
if (bypass) {
|
||||
@@ -677,7 +684,8 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
}
|
||||
}
|
||||
|
||||
static void server_send_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
server_send_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
server_ctx_t *server_send_ctx = (server_ctx_t *)w;
|
||||
server_t *server = server_send_ctx->server;
|
||||
@@ -715,7 +723,8 @@ static void server_send_cb(EV_P_ ev_io *w, int revents)
|
||||
}
|
||||
|
||||
#ifdef ANDROID
|
||||
static void stat_update_cb(struct ev_loop *loop)
|
||||
static void
|
||||
stat_update_cb(struct ev_loop *loop)
|
||||
{
|
||||
ev_tstamp now = ev_now(loop);
|
||||
if (now - last > 1.0) {
|
||||
@@ -726,7 +735,8 @@ static void stat_update_cb(struct ev_loop *loop)
|
||||
|
||||
#endif
|
||||
|
||||
static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents)
|
||||
static void
|
||||
remote_timeout_cb(EV_P_ ev_timer *watcher, int revents)
|
||||
{
|
||||
remote_ctx_t *remote_ctx = (remote_ctx_t *)(((void *)watcher)
|
||||
- sizeof(ev_io));
|
||||
@@ -741,7 +751,8 @@ static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents)
|
||||
close_and_free_server(EV_A_ server);
|
||||
}
|
||||
|
||||
static void remote_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
remote_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
remote_ctx_t *remote_recv_ctx = (remote_ctx_t *)w;
|
||||
remote_t *remote = remote_recv_ctx->remote;
|
||||
@@ -814,7 +825,8 @@ static void remote_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
setsockopt(remote->fd, SOL_TCP, TCP_NODELAY, &opt, sizeof(opt));
|
||||
}
|
||||
|
||||
static void remote_send_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
remote_send_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
remote_ctx_t *remote_send_ctx = (remote_ctx_t *)w;
|
||||
remote_t *remote = remote_send_ctx->remote;
|
||||
@@ -877,7 +889,8 @@ static void remote_send_cb(EV_P_ ev_io *w, int revents)
|
||||
}
|
||||
}
|
||||
|
||||
static remote_t *new_remote(int fd, int timeout)
|
||||
static remote_t *
|
||||
new_remote(int fd, int timeout)
|
||||
{
|
||||
remote_t *remote;
|
||||
remote = ss_malloc(sizeof(remote_t));
|
||||
@@ -905,7 +918,8 @@ static remote_t *new_remote(int fd, int timeout)
|
||||
return remote;
|
||||
}
|
||||
|
||||
static void free_remote(remote_t *remote)
|
||||
static void
|
||||
free_remote(remote_t *remote)
|
||||
{
|
||||
if (remote->server != NULL) {
|
||||
remote->server->remote = NULL;
|
||||
@@ -919,7 +933,8 @@ static void free_remote(remote_t *remote)
|
||||
ss_free(remote);
|
||||
}
|
||||
|
||||
static void close_and_free_remote(EV_P_ remote_t *remote)
|
||||
static void
|
||||
close_and_free_remote(EV_P_ remote_t *remote)
|
||||
{
|
||||
if (remote != NULL) {
|
||||
ev_timer_stop(EV_A_ & remote->send_ctx->watcher);
|
||||
@@ -931,7 +946,8 @@ static void close_and_free_remote(EV_P_ remote_t *remote)
|
||||
}
|
||||
}
|
||||
|
||||
static server_t *new_server(int fd, int method)
|
||||
static server_t *
|
||||
new_server(int fd, int method)
|
||||
{
|
||||
server_t *server;
|
||||
server = ss_malloc(sizeof(server_t));
|
||||
@@ -967,7 +983,8 @@ static server_t *new_server(int fd, int method)
|
||||
return server;
|
||||
}
|
||||
|
||||
static void free_server(server_t *server)
|
||||
static void
|
||||
free_server(server_t *server)
|
||||
{
|
||||
cork_dllist_remove(&server->entries);
|
||||
|
||||
@@ -991,7 +1008,8 @@ static void free_server(server_t *server)
|
||||
ss_free(server);
|
||||
}
|
||||
|
||||
static void close_and_free_server(EV_P_ server_t *server)
|
||||
static void
|
||||
close_and_free_server(EV_P_ server_t *server)
|
||||
{
|
||||
if (server != NULL) {
|
||||
ev_io_stop(EV_A_ & server->send_ctx->io);
|
||||
@@ -1001,8 +1019,9 @@ static void close_and_free_server(EV_P_ server_t *server)
|
||||
}
|
||||
}
|
||||
|
||||
static remote_t *create_remote(listen_ctx_t *listener,
|
||||
struct sockaddr *addr)
|
||||
static remote_t *
|
||||
create_remote(listen_ctx_t *listener,
|
||||
struct sockaddr *addr)
|
||||
{
|
||||
struct sockaddr *remote_addr;
|
||||
|
||||
@@ -1049,7 +1068,8 @@ static remote_t *create_remote(listen_ctx_t *listener,
|
||||
return remote;
|
||||
}
|
||||
|
||||
static void signal_cb(EV_P_ ev_signal *w, int revents)
|
||||
static void
|
||||
signal_cb(EV_P_ ev_signal *w, int revents)
|
||||
{
|
||||
if (revents & EV_SIGNAL) {
|
||||
switch (w->signum) {
|
||||
@@ -1063,7 +1083,8 @@ static void signal_cb(EV_P_ ev_signal *w, int revents)
|
||||
}
|
||||
}
|
||||
|
||||
void accept_cb(EV_P_ ev_io *w, int revents)
|
||||
void
|
||||
accept_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
listen_ctx_t *listener = (listen_ctx_t *)w;
|
||||
int serverfd = accept(listener->fd, NULL, NULL);
|
||||
@@ -1084,13 +1105,15 @@ void accept_cb(EV_P_ ev_io *w, int revents)
|
||||
ev_io_start(EV_A_ & server->recv_ctx->io);
|
||||
}
|
||||
|
||||
void resolve_int_cb(int dummy)
|
||||
void
|
||||
resolve_int_cb(int dummy)
|
||||
{
|
||||
keep_resolving = 0;
|
||||
}
|
||||
|
||||
#ifndef LIB_ONLY
|
||||
int main(int argc, char **argv)
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
int i, c;
|
||||
int pid_flags = 0;
|
||||
@@ -1438,7 +1461,8 @@ int main(int argc, char **argv)
|
||||
|
||||
#else
|
||||
|
||||
int start_ss_local_server(profile_t profile)
|
||||
int
|
||||
start_ss_local_server(profile_t profile)
|
||||
{
|
||||
srand(time(NULL));
|
||||
|
||||
|
||||
+46
-29
@@ -72,15 +72,16 @@
|
||||
#define BUF_SIZE 65535
|
||||
#endif
|
||||
|
||||
int verbose = 0;
|
||||
char *executable = "ss-server";
|
||||
char *working_dir = NULL;
|
||||
int verbose = 0;
|
||||
char *executable = "ss-server";
|
||||
char *working_dir = NULL;
|
||||
int working_dir_size = 0;
|
||||
|
||||
static struct cork_hash_table *server_table;
|
||||
|
||||
#ifndef __MINGW32__
|
||||
static int setnonblocking(int fd)
|
||||
static int
|
||||
setnonblocking(int fd)
|
||||
{
|
||||
int flags;
|
||||
if (-1 == (flags = fcntl(fd, F_GETFL, 0))) {
|
||||
@@ -91,9 +92,10 @@ static int setnonblocking(int fd)
|
||||
|
||||
#endif
|
||||
|
||||
static void build_config(char *prefix, struct server *server)
|
||||
static void
|
||||
build_config(char *prefix, struct server *server)
|
||||
{
|
||||
char *path = NULL;
|
||||
char *path = NULL;
|
||||
int path_size = strlen(prefix) + strlen(server->port) + 20;
|
||||
|
||||
path = malloc(path_size);
|
||||
@@ -114,7 +116,8 @@ static void build_config(char *prefix, struct server *server)
|
||||
ss_free(path);
|
||||
}
|
||||
|
||||
static char *construct_command_line(struct manager_ctx *manager, struct server *server)
|
||||
static char *
|
||||
construct_command_line(struct manager_ctx *manager, struct server *server)
|
||||
{
|
||||
static char cmd[BUF_SIZE];
|
||||
int i;
|
||||
@@ -179,7 +182,8 @@ static char *construct_command_line(struct manager_ctx *manager, struct server *
|
||||
return cmd;
|
||||
}
|
||||
|
||||
static char *get_data(char *buf, int len)
|
||||
static char *
|
||||
get_data(char *buf, int len)
|
||||
{
|
||||
char *data;
|
||||
int pos = 0;
|
||||
@@ -194,7 +198,8 @@ static char *get_data(char *buf, int len)
|
||||
return data;
|
||||
}
|
||||
|
||||
static char *get_action(char *buf, int len)
|
||||
static char *
|
||||
get_action(char *buf, int len)
|
||||
{
|
||||
char *action;
|
||||
int pos = 0;
|
||||
@@ -213,7 +218,8 @@ static char *get_action(char *buf, int len)
|
||||
return action;
|
||||
}
|
||||
|
||||
static struct server *get_server(char *buf, int len)
|
||||
static struct server *
|
||||
get_server(char *buf, int len)
|
||||
{
|
||||
char *data = get_data(buf, len);
|
||||
char error_buf[512];
|
||||
@@ -259,7 +265,8 @@ static struct server *get_server(char *buf, int len)
|
||||
return server;
|
||||
}
|
||||
|
||||
static int parse_traffic(char *buf, int len, char *port, uint64_t *traffic)
|
||||
static int
|
||||
parse_traffic(char *buf, int len, char *port, uint64_t *traffic)
|
||||
{
|
||||
char *data = get_data(buf, len);
|
||||
char error_buf[512];
|
||||
@@ -292,7 +299,8 @@ static int parse_traffic(char *buf, int len, char *port, uint64_t *traffic)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void add_server(struct manager_ctx *manager, struct server *server)
|
||||
static void
|
||||
add_server(struct manager_ctx *manager, struct server *server)
|
||||
{
|
||||
bool new = false;
|
||||
cork_hash_table_put(server_table, (void *)server->port, (void *)server, &new, NULL, NULL);
|
||||
@@ -303,7 +311,8 @@ static void add_server(struct manager_ctx *manager, struct server *server)
|
||||
}
|
||||
}
|
||||
|
||||
static void kill_server(char *prefix, char *pid_file)
|
||||
static void
|
||||
kill_server(char *prefix, char *pid_file)
|
||||
{
|
||||
char *path = NULL;
|
||||
int pid, path_size = strlen(prefix) + strlen(pid_file) + 2;
|
||||
@@ -325,7 +334,8 @@ static void kill_server(char *prefix, char *pid_file)
|
||||
ss_free(path);
|
||||
}
|
||||
|
||||
static void stop_server(char *prefix, char *port)
|
||||
static void
|
||||
stop_server(char *prefix, char *port)
|
||||
{
|
||||
char *path = NULL;
|
||||
int pid, path_size = strlen(prefix) + strlen(port) + 20;
|
||||
@@ -346,7 +356,8 @@ static void stop_server(char *prefix, char *port)
|
||||
ss_free(path);
|
||||
}
|
||||
|
||||
static void remove_server(char *prefix, char *port)
|
||||
static void
|
||||
remove_server(char *prefix, char *port)
|
||||
{
|
||||
char *old_port = NULL;
|
||||
struct server *old_server = NULL;
|
||||
@@ -360,7 +371,8 @@ static void remove_server(char *prefix, char *port)
|
||||
stop_server(prefix, port);
|
||||
}
|
||||
|
||||
static void update_stat(char *port, uint64_t traffic)
|
||||
static void
|
||||
update_stat(char *port, uint64_t traffic)
|
||||
{
|
||||
void *ret = cork_hash_table_get(server_table, (void *)port);
|
||||
if (ret != NULL) {
|
||||
@@ -369,7 +381,8 @@ static void update_stat(char *port, uint64_t traffic)
|
||||
}
|
||||
}
|
||||
|
||||
static void manager_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
manager_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
struct manager_ctx *manager = (struct manager_ctx *)w;
|
||||
socklen_t len;
|
||||
@@ -491,7 +504,8 @@ ERROR_MSG:
|
||||
}
|
||||
}
|
||||
|
||||
static void signal_cb(EV_P_ ev_signal *w, int revents)
|
||||
static void
|
||||
signal_cb(EV_P_ ev_signal *w, int revents)
|
||||
{
|
||||
if (revents & EV_SIGNAL) {
|
||||
switch (w->signum) {
|
||||
@@ -502,7 +516,8 @@ static void signal_cb(EV_P_ ev_signal *w, int revents)
|
||||
}
|
||||
}
|
||||
|
||||
int create_server_socket(const char *host, const char *port)
|
||||
int
|
||||
create_server_socket(const char *host, const char *port)
|
||||
{
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *result, *rp, *ipv4v6bindall;
|
||||
@@ -576,7 +591,8 @@ int create_server_socket(const char *host, const char *port)
|
||||
return server_sock;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
int i, c;
|
||||
int pid_flags = 0;
|
||||
@@ -605,12 +621,12 @@ int main(int argc, char **argv)
|
||||
|
||||
int option_index = 0;
|
||||
static struct option long_options[] = {
|
||||
{ "fast-open" , no_argument , 0, 0 },
|
||||
{ "acl" , required_argument, 0, 0 },
|
||||
{ "fast-open", no_argument, 0, 0 },
|
||||
{ "acl", required_argument, 0, 0 },
|
||||
{ "manager-address", required_argument, 0, 0 },
|
||||
{ "executable" , required_argument, 0, 0 },
|
||||
{ "mtu" , required_argument, 0, 0 },
|
||||
{ "help" , no_argument , 0, 0 },
|
||||
{ "executable", required_argument, 0, 0 },
|
||||
{ "mtu", required_argument, 0, 0 },
|
||||
{ "help", no_argument, 0, 0 },
|
||||
{ 0, 0, 0, 0 }
|
||||
};
|
||||
|
||||
@@ -812,8 +828,8 @@ int main(int argc, char **argv)
|
||||
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
const char *homedir = pw->pw_dir;
|
||||
working_dir_size = strlen(homedir)+15;
|
||||
working_dir = malloc(working_dir_size);
|
||||
working_dir_size = strlen(homedir) + 15;
|
||||
working_dir = malloc(working_dir_size);
|
||||
snprintf(working_dir, working_dir_size, "%s/.shadowsocks", homedir);
|
||||
|
||||
int err = mkdir(working_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
||||
@@ -832,10 +848,11 @@ int main(int argc, char **argv)
|
||||
size_t len = strlen(ep->d_name);
|
||||
if (strcmp(ep->d_name + len - 3, "pid") == 0) {
|
||||
kill_server(working_dir, ep->d_name);
|
||||
if (verbose) LOGI("kill %s", ep->d_name);
|
||||
if (verbose)
|
||||
LOGI("kill %s", ep->d_name);
|
||||
}
|
||||
}
|
||||
closedir (dp);
|
||||
closedir(dp);
|
||||
} else {
|
||||
ss_free(working_dir);
|
||||
FATAL("Couldn't open the directory");
|
||||
|
||||
+21
-12
@@ -55,19 +55,21 @@
|
||||
extern int verbose;
|
||||
|
||||
static const char valid_label_bytes[] =
|
||||
"-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
|
||||
"-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
#if defined(MODULE_LOCAL)
|
||||
extern int keep_resolving;
|
||||
#endif
|
||||
|
||||
int set_reuseport(int socket)
|
||||
int
|
||||
set_reuseport(int socket)
|
||||
{
|
||||
int opt = 1;
|
||||
return setsockopt(socket, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
|
||||
}
|
||||
|
||||
size_t get_sockaddr_len(struct sockaddr *addr)
|
||||
size_t
|
||||
get_sockaddr_len(struct sockaddr *addr)
|
||||
{
|
||||
if (addr->sa_family == AF_INET) {
|
||||
return sizeof(struct sockaddr_in);
|
||||
@@ -78,7 +80,8 @@ size_t get_sockaddr_len(struct sockaddr *addr)
|
||||
}
|
||||
|
||||
#ifdef SET_INTERFACE
|
||||
int setinterface(int socket_fd, const char *interface_name)
|
||||
int
|
||||
setinterface(int socket_fd, const char *interface_name)
|
||||
{
|
||||
struct ifreq interface;
|
||||
memset(&interface, 0, sizeof(interface));
|
||||
@@ -87,9 +90,11 @@ int setinterface(int socket_fd, const char *interface_name)
|
||||
sizeof(struct ifreq));
|
||||
return res;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
int bind_to_address(int socket_fd, const char *host)
|
||||
int
|
||||
bind_to_address(int socket_fd, const char *host)
|
||||
{
|
||||
if (host != NULL) {
|
||||
struct cork_ip ip;
|
||||
@@ -112,7 +117,8 @@ int bind_to_address(int socket_fd, const char *host)
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t get_sockaddr(char *host, char *port, struct sockaddr_storage *storage, int block)
|
||||
ssize_t
|
||||
get_sockaddr(char *host, char *port, struct sockaddr_storage *storage, int block)
|
||||
{
|
||||
struct cork_ip ip;
|
||||
if (cork_ip_init(&ip, host) != -1) {
|
||||
@@ -187,8 +193,9 @@ ssize_t get_sockaddr(char *host, char *port, struct sockaddr_storage *storage, i
|
||||
return -1;
|
||||
}
|
||||
|
||||
int sockaddr_cmp(struct sockaddr_storage *addr1,
|
||||
struct sockaddr_storage *addr2, socklen_t len)
|
||||
int
|
||||
sockaddr_cmp(struct sockaddr_storage *addr1,
|
||||
struct sockaddr_storage *addr2, socklen_t len)
|
||||
{
|
||||
struct sockaddr_in *p1_in = (struct sockaddr_in *)addr1;
|
||||
struct sockaddr_in *p2_in = (struct sockaddr_in *)addr2;
|
||||
@@ -229,8 +236,9 @@ int sockaddr_cmp(struct sockaddr_storage *addr1,
|
||||
}
|
||||
}
|
||||
|
||||
int sockaddr_cmp_addr(struct sockaddr_storage *addr1,
|
||||
struct sockaddr_storage *addr2, socklen_t len)
|
||||
int
|
||||
sockaddr_cmp_addr(struct sockaddr_storage *addr1,
|
||||
struct sockaddr_storage *addr2, socklen_t len)
|
||||
{
|
||||
struct sockaddr_in *p1_in = (struct sockaddr_in *)addr1;
|
||||
struct sockaddr_in *p2_in = (struct sockaddr_in *)addr2;
|
||||
@@ -255,7 +263,8 @@ int sockaddr_cmp_addr(struct sockaddr_storage *addr1,
|
||||
}
|
||||
}
|
||||
|
||||
int validate_hostname(const char *hostname, const int hostname_len)
|
||||
int
|
||||
validate_hostname(const char *hostname, const int hostname_len)
|
||||
{
|
||||
if (hostname == NULL)
|
||||
return 0;
|
||||
@@ -269,7 +278,7 @@ int validate_hostname(const char *hostname, const int hostname_len)
|
||||
const char *label = hostname;
|
||||
while (label < hostname + hostname_len) {
|
||||
size_t label_len = hostname_len - (label - hostname);
|
||||
char *next_dot = strchr(label, '.');
|
||||
char *next_dot = strchr(label, '.');
|
||||
if (next_dot != NULL)
|
||||
label_len = next_dot - label;
|
||||
|
||||
|
||||
+1
-1
@@ -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
|
||||
+40
-24
@@ -92,7 +92,8 @@ static int auth = 0;
|
||||
static int nofile = 0;
|
||||
#endif
|
||||
|
||||
int getdestaddr(int fd, struct sockaddr_storage *destaddr)
|
||||
int
|
||||
getdestaddr(int fd, struct sockaddr_storage *destaddr)
|
||||
{
|
||||
socklen_t socklen = sizeof(*destaddr);
|
||||
int error = 0;
|
||||
@@ -107,7 +108,8 @@ int getdestaddr(int fd, struct sockaddr_storage *destaddr)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int setnonblocking(int fd)
|
||||
int
|
||||
setnonblocking(int fd)
|
||||
{
|
||||
int flags;
|
||||
if (-1 == (flags = fcntl(fd, F_GETFL, 0))) {
|
||||
@@ -116,7 +118,8 @@ int setnonblocking(int fd)
|
||||
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
||||
}
|
||||
|
||||
int create_and_bind(const char *addr, const char *port)
|
||||
int
|
||||
create_and_bind(const char *addr, const char *port)
|
||||
{
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *result, *rp;
|
||||
@@ -169,14 +172,15 @@ int create_and_bind(const char *addr, const char *port)
|
||||
return listen_sock;
|
||||
}
|
||||
|
||||
static void server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
server_ctx_t *server_recv_ctx = (server_ctx_t *)w;
|
||||
server_t *server = server_recv_ctx->server;
|
||||
remote_t *remote = server->remote;
|
||||
|
||||
ssize_t r = recv(server->fd, remote->buf->array + remote->buf->len,
|
||||
BUF_SIZE - remote->buf->len, 0);
|
||||
BUF_SIZE - remote->buf->len, 0);
|
||||
|
||||
if (r == 0) {
|
||||
// connection closed
|
||||
@@ -224,7 +228,7 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
|
||||
if (!remote->send_ctx->connected) {
|
||||
// SNI
|
||||
int ret = 0;
|
||||
int ret = 0;
|
||||
uint16_t port = 0;
|
||||
|
||||
if (AF_INET6 == server->destaddr.ss_family) { // IPv6
|
||||
@@ -234,10 +238,10 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
}
|
||||
if (port == http_protocol->default_port)
|
||||
ret = http_protocol->parse_packet(remote->buf->array,
|
||||
remote->buf->len, &server->hostname);
|
||||
remote->buf->len, &server->hostname);
|
||||
else if (port == tls_protocol->default_port)
|
||||
ret = tls_protocol->parse_packet(remote->buf->array,
|
||||
remote->buf->len, &server->hostname);
|
||||
remote->buf->len, &server->hostname);
|
||||
if (ret > 0) {
|
||||
server->hostname_len = ret;
|
||||
}
|
||||
@@ -283,7 +287,8 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
}
|
||||
}
|
||||
|
||||
static void server_send_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
server_send_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
server_ctx_t *server_send_ctx = (server_ctx_t *)w;
|
||||
server_t *server = server_send_ctx->server;
|
||||
@@ -319,7 +324,8 @@ static void server_send_cb(EV_P_ ev_io *w, int revents)
|
||||
}
|
||||
}
|
||||
|
||||
static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents)
|
||||
static void
|
||||
remote_timeout_cb(EV_P_ ev_timer *watcher, int revents)
|
||||
{
|
||||
remote_ctx_t *remote_ctx = (remote_ctx_t *)(((void *)watcher)
|
||||
- sizeof(ev_io));
|
||||
@@ -332,7 +338,8 @@ static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents)
|
||||
close_and_free_server(EV_A_ server);
|
||||
}
|
||||
|
||||
static void remote_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
remote_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
remote_ctx_t *remote_recv_ctx = (remote_ctx_t *)w;
|
||||
remote_t *remote = remote_recv_ctx->remote;
|
||||
@@ -393,7 +400,8 @@ static void remote_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
setsockopt(remote->fd, SOL_TCP, TCP_NODELAY, &opt, sizeof(opt));
|
||||
}
|
||||
|
||||
static void remote_send_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
remote_send_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
remote_ctx_t *remote_send_ctx = (remote_ctx_t *)w;
|
||||
remote_t *remote = remote_send_ctx->remote;
|
||||
@@ -429,7 +437,6 @@ static void remote_send_cb(EV_P_ ev_io *w, int revents)
|
||||
memcpy(abuf->array + abuf->len, &port, 2);
|
||||
|
||||
LOGI("Dest: %s:%d", server->hostname, ntohs(port));
|
||||
|
||||
} else if (AF_INET6 == server->destaddr.ss_family) { // IPv6
|
||||
abuf->array[abuf->len++] = 4; // Type 4 is IPv6 address
|
||||
|
||||
@@ -515,7 +522,8 @@ static void remote_send_cb(EV_P_ ev_io *w, int revents)
|
||||
}
|
||||
}
|
||||
|
||||
static remote_t *new_remote(int fd, int timeout)
|
||||
static remote_t *
|
||||
new_remote(int fd, int timeout)
|
||||
{
|
||||
remote_t *remote;
|
||||
remote = ss_malloc(sizeof(remote_t));
|
||||
@@ -541,7 +549,8 @@ static remote_t *new_remote(int fd, int timeout)
|
||||
return remote;
|
||||
}
|
||||
|
||||
static void free_remote(remote_t *remote)
|
||||
static void
|
||||
free_remote(remote_t *remote)
|
||||
{
|
||||
if (remote != NULL) {
|
||||
if (remote->server != NULL) {
|
||||
@@ -557,7 +566,8 @@ static void free_remote(remote_t *remote)
|
||||
}
|
||||
}
|
||||
|
||||
static void close_and_free_remote(EV_P_ remote_t *remote)
|
||||
static void
|
||||
close_and_free_remote(EV_P_ remote_t *remote)
|
||||
{
|
||||
if (remote != NULL) {
|
||||
ev_timer_stop(EV_A_ & remote->send_ctx->watcher);
|
||||
@@ -568,7 +578,8 @@ static void close_and_free_remote(EV_P_ remote_t *remote)
|
||||
}
|
||||
}
|
||||
|
||||
static server_t *new_server(int fd, int method)
|
||||
static server_t *
|
||||
new_server(int fd, int method)
|
||||
{
|
||||
server_t *server;
|
||||
server = ss_malloc(sizeof(server_t));
|
||||
@@ -582,8 +593,8 @@ static server_t *new_server(int fd, int method)
|
||||
server->send_ctx->server = server;
|
||||
server->send_ctx->connected = 0;
|
||||
|
||||
server->hostname = NULL;
|
||||
server->hostname_len = 0;
|
||||
server->hostname = NULL;
|
||||
server->hostname_len = 0;
|
||||
|
||||
if (method) {
|
||||
server->e_ctx = ss_malloc(sizeof(enc_ctx_t));
|
||||
@@ -603,7 +614,8 @@ static server_t *new_server(int fd, int method)
|
||||
return server;
|
||||
}
|
||||
|
||||
static void free_server(server_t *server)
|
||||
static void
|
||||
free_server(server_t *server)
|
||||
{
|
||||
if (server != NULL) {
|
||||
if (server->hostname != NULL) {
|
||||
@@ -630,7 +642,8 @@ static void free_server(server_t *server)
|
||||
}
|
||||
}
|
||||
|
||||
static void close_and_free_server(EV_P_ server_t *server)
|
||||
static void
|
||||
close_and_free_server(EV_P_ server_t *server)
|
||||
{
|
||||
if (server != NULL) {
|
||||
ev_io_stop(EV_A_ & server->send_ctx->io);
|
||||
@@ -640,7 +653,8 @@ static void close_and_free_server(EV_P_ server_t *server)
|
||||
}
|
||||
}
|
||||
|
||||
static void accept_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
accept_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
listen_ctx_t *listener = (listen_ctx_t *)w;
|
||||
struct sockaddr_storage destaddr;
|
||||
@@ -729,13 +743,15 @@ static void accept_cb(EV_P_ ev_io *w, int revents)
|
||||
ev_io_start(EV_A_ & server->recv_ctx->io);
|
||||
}
|
||||
|
||||
void signal_cb(int dummy)
|
||||
void
|
||||
signal_cb(int dummy)
|
||||
{
|
||||
keep_resolving = 0;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
srand(time(NULL));
|
||||
|
||||
|
||||
+31
-17
@@ -83,7 +83,8 @@ static struct sockaddr *choose_ipv4_first(struct ResolvQuery *);
|
||||
static struct sockaddr *choose_ipv6_first(struct ResolvQuery *);
|
||||
static struct sockaddr *choose_any(struct ResolvQuery *);
|
||||
|
||||
int resolv_init(struct ev_loop *loop, char **nameservers, int nameserver_num, int ipv6first)
|
||||
int
|
||||
resolv_init(struct ev_loop *loop, char **nameservers, int nameserver_num, int ipv6first)
|
||||
{
|
||||
if (ipv6first)
|
||||
resolv_mode = MODE_IPV6_FIRST;
|
||||
@@ -110,7 +111,7 @@ int resolv_init(struct ev_loop *loop, char **nameservers, int nameserver_num, in
|
||||
|
||||
if (nameserver_num == 1 && nameservers != NULL) {
|
||||
if (strncmp("127.0.0.1", nameservers[0], 9) == 0
|
||||
|| strncmp("::1", nameservers[0], 3) == 0) {
|
||||
|| strncmp("::1", nameservers[0], 3) == 0) {
|
||||
if (verbose) {
|
||||
LOGI("bind UDP resolver to %s", nameservers[0]);
|
||||
}
|
||||
@@ -139,7 +140,8 @@ int resolv_init(struct ev_loop *loop, char **nameservers, int nameserver_num, in
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
void resolv_shutdown(struct ev_loop *loop)
|
||||
void
|
||||
resolv_shutdown(struct ev_loop *loop)
|
||||
{
|
||||
struct dns_ctx *ctx = (struct dns_ctx *)resolv_io_watcher.data;
|
||||
|
||||
@@ -152,9 +154,10 @@ void resolv_shutdown(struct ev_loop *loop)
|
||||
dns_close(ctx);
|
||||
}
|
||||
|
||||
struct ResolvQuery *resolv_query(const char *hostname, void (*client_cb)(struct sockaddr *, void *),
|
||||
void (*client_free_cb)(void *), void *client_cb_data,
|
||||
uint16_t port)
|
||||
struct ResolvQuery *
|
||||
resolv_query(const char *hostname, void (*client_cb)(struct sockaddr *, void *),
|
||||
void (*client_free_cb)(void *), void *client_cb_data,
|
||||
uint16_t port)
|
||||
{
|
||||
struct dns_ctx *ctx = (struct dns_ctx *)resolv_io_watcher.data;
|
||||
|
||||
@@ -205,7 +208,8 @@ struct ResolvQuery *resolv_query(const char *hostname, void (*client_cb)(struct
|
||||
return cb_data;
|
||||
}
|
||||
|
||||
void resolv_cancel(struct ResolvQuery *query_handle)
|
||||
void
|
||||
resolv_cancel(struct ResolvQuery *query_handle)
|
||||
{
|
||||
struct ResolvQuery *cb_data = (struct ResolvQuery *)query_handle;
|
||||
struct dns_ctx *ctx = (struct dns_ctx *)resolv_io_watcher.data;
|
||||
@@ -227,7 +231,8 @@ void resolv_cancel(struct ResolvQuery *query_handle)
|
||||
/*
|
||||
* DNS UDP socket activity callback
|
||||
*/
|
||||
static void resolv_sock_cb(struct ev_loop *loop, struct ev_io *w, int revents)
|
||||
static void
|
||||
resolv_sock_cb(struct ev_loop *loop, struct ev_io *w, int revents)
|
||||
{
|
||||
struct dns_ctx *ctx = (struct dns_ctx *)w->data;
|
||||
|
||||
@@ -239,7 +244,8 @@ static void resolv_sock_cb(struct ev_loop *loop, struct ev_io *w, int revents)
|
||||
/*
|
||||
* Wrapper for client callback we provide to udns
|
||||
*/
|
||||
static void dns_query_v4_cb(struct dns_ctx *ctx, struct dns_rr_a4 *result, void *data)
|
||||
static void
|
||||
dns_query_v4_cb(struct dns_ctx *ctx, struct dns_rr_a4 *result, void *data)
|
||||
{
|
||||
struct ResolvQuery *cb_data = (struct ResolvQuery *)data;
|
||||
|
||||
@@ -285,7 +291,8 @@ static void dns_query_v4_cb(struct dns_ctx *ctx, struct dns_rr_a4 *result, void
|
||||
}
|
||||
}
|
||||
|
||||
static void dns_query_v6_cb(struct dns_ctx *ctx, struct dns_rr_a6 *result, void *data)
|
||||
static void
|
||||
dns_query_v6_cb(struct dns_ctx *ctx, struct dns_rr_a6 *result, void *data)
|
||||
{
|
||||
struct ResolvQuery *cb_data = (struct ResolvQuery *)data;
|
||||
|
||||
@@ -334,7 +341,8 @@ static void dns_query_v6_cb(struct dns_ctx *ctx, struct dns_rr_a6 *result, void
|
||||
/*
|
||||
* Called once all queries have been completed
|
||||
*/
|
||||
static void process_client_callback(struct ResolvQuery *cb_data)
|
||||
static void
|
||||
process_client_callback(struct ResolvQuery *cb_data)
|
||||
{
|
||||
struct sockaddr *best_address = NULL;
|
||||
|
||||
@@ -358,7 +366,8 @@ static void process_client_callback(struct ResolvQuery *cb_data)
|
||||
ss_free(cb_data);
|
||||
}
|
||||
|
||||
static struct sockaddr *choose_ipv4_first(struct ResolvQuery *cb_data)
|
||||
static struct sockaddr *
|
||||
choose_ipv4_first(struct ResolvQuery *cb_data)
|
||||
{
|
||||
for (int i = 0; i < cb_data->response_count; i++)
|
||||
if (cb_data->responses[i]->sa_family == AF_INET) {
|
||||
@@ -368,7 +377,8 @@ static struct sockaddr *choose_ipv4_first(struct ResolvQuery *cb_data)
|
||||
return choose_any(cb_data);
|
||||
}
|
||||
|
||||
static struct sockaddr *choose_ipv6_first(struct ResolvQuery *cb_data)
|
||||
static struct sockaddr *
|
||||
choose_ipv6_first(struct ResolvQuery *cb_data)
|
||||
{
|
||||
for (int i = 0; i < cb_data->response_count; i++)
|
||||
if (cb_data->responses[i]->sa_family == AF_INET6) {
|
||||
@@ -378,7 +388,8 @@ static struct sockaddr *choose_ipv6_first(struct ResolvQuery *cb_data)
|
||||
return choose_any(cb_data);
|
||||
}
|
||||
|
||||
static struct sockaddr *choose_any(struct ResolvQuery *cb_data)
|
||||
static struct sockaddr *
|
||||
choose_any(struct ResolvQuery *cb_data)
|
||||
{
|
||||
if (cb_data->response_count >= 1) {
|
||||
return cb_data->responses[0];
|
||||
@@ -390,7 +401,8 @@ static struct sockaddr *choose_any(struct ResolvQuery *cb_data)
|
||||
/*
|
||||
* DNS timeout callback
|
||||
*/
|
||||
static void resolv_timeout_cb(struct ev_loop *loop, struct ev_timer *w, int revents)
|
||||
static void
|
||||
resolv_timeout_cb(struct ev_loop *loop, struct ev_timer *w, int revents)
|
||||
{
|
||||
struct dns_ctx *ctx = (struct dns_ctx *)w->data;
|
||||
|
||||
@@ -402,7 +414,8 @@ static void resolv_timeout_cb(struct ev_loop *loop, struct ev_timer *w, int reve
|
||||
/*
|
||||
* Callback to setup DNS timeout callback
|
||||
*/
|
||||
static void dns_timer_setup_cb(struct dns_ctx *ctx, int timeout, void *data)
|
||||
static void
|
||||
dns_timer_setup_cb(struct dns_ctx *ctx, int timeout, void *data)
|
||||
{
|
||||
struct ev_loop *loop = (struct ev_loop *)data;
|
||||
|
||||
@@ -416,7 +429,8 @@ static void dns_timer_setup_cb(struct dns_ctx *ctx, int timeout, void *data)
|
||||
}
|
||||
}
|
||||
|
||||
static inline int all_queries_are_null(struct ResolvQuery *cb_data)
|
||||
static inline int
|
||||
all_queries_are_null(struct ResolvQuery *cb_data)
|
||||
{
|
||||
int result = 1;
|
||||
|
||||
|
||||
+17
-10
@@ -42,7 +42,8 @@ extern void ss_error(const char *s);
|
||||
static void free_rule(rule_t *);
|
||||
|
||||
rule_t *
|
||||
new_rule() {
|
||||
new_rule()
|
||||
{
|
||||
rule_t *rule;
|
||||
|
||||
rule = calloc(1, sizeof(rule_t));
|
||||
@@ -55,7 +56,8 @@ new_rule() {
|
||||
}
|
||||
|
||||
int
|
||||
accept_rule_arg(rule_t *rule, const char *arg) {
|
||||
accept_rule_arg(rule_t *rule, const char *arg)
|
||||
{
|
||||
if (rule->pattern == NULL) {
|
||||
rule->pattern = strdup(arg);
|
||||
if (rule->pattern == NULL) {
|
||||
@@ -71,12 +73,14 @@ accept_rule_arg(rule_t *rule, const char *arg) {
|
||||
}
|
||||
|
||||
void
|
||||
add_rule(struct cork_dllist *rules, rule_t *rule) {
|
||||
add_rule(struct cork_dllist *rules, rule_t *rule)
|
||||
{
|
||||
cork_dllist_add(rules, &rule->entries);
|
||||
}
|
||||
|
||||
int
|
||||
init_rule(rule_t *rule) {
|
||||
init_rule(rule_t *rule)
|
||||
{
|
||||
if (rule->pattern_re == NULL) {
|
||||
const char *reerr;
|
||||
int reerroffset;
|
||||
@@ -85,7 +89,7 @@ init_rule(rule_t *rule) {
|
||||
pcre_compile(rule->pattern, 0, &reerr, &reerroffset, NULL);
|
||||
if (rule->pattern_re == NULL) {
|
||||
LOGE("Regex compilation of \"%s\" failed: %s, offset %d",
|
||||
rule->pattern, reerr, reerroffset);
|
||||
rule->pattern, reerr, reerroffset);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -94,18 +98,19 @@ init_rule(rule_t *rule) {
|
||||
}
|
||||
|
||||
rule_t *
|
||||
lookup_rule(const struct cork_dllist *rules, const char *name, size_t name_len) {
|
||||
lookup_rule(const struct cork_dllist *rules, const char *name, size_t name_len)
|
||||
{
|
||||
struct cork_dllist_item *curr, *next;
|
||||
|
||||
if (name == NULL) {
|
||||
name = "";
|
||||
name = "";
|
||||
name_len = 0;
|
||||
}
|
||||
|
||||
cork_dllist_foreach_void(rules, curr, next) {
|
||||
rule_t *rule = cork_container_of(curr, rule_t, entries);
|
||||
if (pcre_exec(rule->pattern_re, NULL,
|
||||
name, name_len, 0, 0, NULL, 0) >= 0)
|
||||
name, name_len, 0, 0, NULL, 0) >= 0)
|
||||
return rule;
|
||||
}
|
||||
|
||||
@@ -113,13 +118,15 @@ lookup_rule(const struct cork_dllist *rules, const char *name, size_t name_len)
|
||||
}
|
||||
|
||||
void
|
||||
remove_rule(rule_t *rule) {
|
||||
remove_rule(rule_t *rule)
|
||||
{
|
||||
cork_dllist_remove(&rule->entries);
|
||||
free_rule(rule);
|
||||
}
|
||||
|
||||
static void
|
||||
free_rule(rule_t *rule) {
|
||||
free_rule(rule_t *rule)
|
||||
{
|
||||
if (rule == NULL)
|
||||
return;
|
||||
|
||||
|
||||
+59
-34
@@ -110,10 +110,10 @@ static int is_header_complete(const buffer_t *buf);
|
||||
|
||||
int verbose = 0;
|
||||
|
||||
static int acl = 0;
|
||||
static int mode = TCP_ONLY;
|
||||
static int auth = 0;
|
||||
static int ipv6first = 0;
|
||||
static int acl = 0;
|
||||
static int mode = TCP_ONLY;
|
||||
static int auth = 0;
|
||||
static int ipv6first = 0;
|
||||
|
||||
static int fast_open = 0;
|
||||
#ifdef HAVE_SETRLIMIT
|
||||
@@ -131,7 +131,8 @@ ev_timer stat_update_watcher;
|
||||
|
||||
static struct cork_dllist connections;
|
||||
|
||||
static void stat_update_cb(EV_P_ ev_timer *watcher, int revents)
|
||||
static void
|
||||
stat_update_cb(EV_P_ ev_timer *watcher, int revents)
|
||||
{
|
||||
struct sockaddr_un svaddr, claddr;
|
||||
int sfd = -1;
|
||||
@@ -206,7 +207,8 @@ static void stat_update_cb(EV_P_ ev_timer *watcher, int revents)
|
||||
close(sfd);
|
||||
}
|
||||
|
||||
static void free_connections(struct ev_loop *loop)
|
||||
static void
|
||||
free_connections(struct ev_loop *loop)
|
||||
{
|
||||
struct cork_dllist_item *curr, *next;
|
||||
cork_dllist_foreach_void(&connections, curr, next) {
|
||||
@@ -217,7 +219,8 @@ static void free_connections(struct ev_loop *loop)
|
||||
}
|
||||
}
|
||||
|
||||
static size_t parse_header_len(const char atyp, const char *data, size_t offset)
|
||||
static size_t
|
||||
parse_header_len(const char atyp, const char *data, size_t offset)
|
||||
{
|
||||
size_t len = 0;
|
||||
if ((atyp & ADDRTYPE_MASK) == 1) {
|
||||
@@ -235,7 +238,8 @@ static size_t parse_header_len(const char atyp, const char *data, size_t offset)
|
||||
return len;
|
||||
}
|
||||
|
||||
static int is_header_complete(const buffer_t *buf)
|
||||
static int
|
||||
is_header_complete(const buffer_t *buf)
|
||||
{
|
||||
size_t header_len = 0;
|
||||
size_t buf_len = buf->len;
|
||||
@@ -271,7 +275,8 @@ static int is_header_complete(const buffer_t *buf)
|
||||
return buf_len >= header_len;
|
||||
}
|
||||
|
||||
static char *get_peer_name(int fd)
|
||||
static char *
|
||||
get_peer_name(int fd)
|
||||
{
|
||||
static char peer_name[INET6_ADDRSTRLEN] = { 0 };
|
||||
struct sockaddr_storage addr;
|
||||
@@ -293,7 +298,8 @@ static char *get_peer_name(int fd)
|
||||
return peer_name;
|
||||
}
|
||||
|
||||
static void report_addr(int fd)
|
||||
static void
|
||||
report_addr(int fd)
|
||||
{
|
||||
char *peer_name;
|
||||
peer_name = get_peer_name(fd);
|
||||
@@ -303,7 +309,8 @@ static void report_addr(int fd)
|
||||
shutdown(fd, SHUT_RDWR);
|
||||
}
|
||||
|
||||
int setfastopen(int fd)
|
||||
int
|
||||
setfastopen(int fd)
|
||||
{
|
||||
int s = 0;
|
||||
#ifdef TCP_FASTOPEN
|
||||
@@ -328,7 +335,8 @@ int setfastopen(int fd)
|
||||
}
|
||||
|
||||
#ifndef __MINGW32__
|
||||
int setnonblocking(int fd)
|
||||
int
|
||||
setnonblocking(int fd)
|
||||
{
|
||||
int flags;
|
||||
if (-1 == (flags = fcntl(fd, F_GETFL, 0))) {
|
||||
@@ -339,7 +347,8 @@ int setnonblocking(int fd)
|
||||
|
||||
#endif
|
||||
|
||||
int create_and_bind(const char *host, const char *port, int mptcp)
|
||||
int
|
||||
create_and_bind(const char *host, const char *port, int mptcp)
|
||||
{
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *result, *rp, *ipv4v6bindall;
|
||||
@@ -436,9 +445,10 @@ int create_and_bind(const char *host, const char *port, int mptcp)
|
||||
return listen_sock;
|
||||
}
|
||||
|
||||
static remote_t *connect_to_remote(struct addrinfo *res,
|
||||
server_t *server,
|
||||
int *connected)
|
||||
static remote_t *
|
||||
connect_to_remote(struct addrinfo *res,
|
||||
server_t *server,
|
||||
int *connected)
|
||||
{
|
||||
int sockfd;
|
||||
#ifdef SET_INTERFACE
|
||||
@@ -539,7 +549,8 @@ static remote_t *connect_to_remote(struct addrinfo *res,
|
||||
return remote;
|
||||
}
|
||||
|
||||
static void server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
server_ctx_t *server_recv_ctx = (server_ctx_t *)w;
|
||||
server_t *server = server_recv_ctx->server;
|
||||
@@ -921,7 +932,8 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
FATAL("server context error");
|
||||
}
|
||||
|
||||
static void server_send_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
server_send_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
server_ctx_t *server_send_ctx = (server_ctx_t *)w;
|
||||
server_t *server = server_send_ctx->server;
|
||||
@@ -975,7 +987,8 @@ static void server_send_cb(EV_P_ ev_io *w, int revents)
|
||||
}
|
||||
}
|
||||
|
||||
static void server_timeout_cb(EV_P_ ev_timer *watcher, int revents)
|
||||
static void
|
||||
server_timeout_cb(EV_P_ ev_timer *watcher, int revents)
|
||||
{
|
||||
server_ctx_t *server_ctx = (server_ctx_t *)(((void *)watcher)
|
||||
- sizeof(ev_io));
|
||||
@@ -990,14 +1003,16 @@ static void server_timeout_cb(EV_P_ ev_timer *watcher, int revents)
|
||||
close_and_free_server(EV_A_ server);
|
||||
}
|
||||
|
||||
static void query_free_cb(void *data)
|
||||
static void
|
||||
query_free_cb(void *data)
|
||||
{
|
||||
if (data != NULL) {
|
||||
ss_free(data);
|
||||
}
|
||||
}
|
||||
|
||||
static void server_resolve_cb(struct sockaddr *addr, void *data)
|
||||
static void
|
||||
server_resolve_cb(struct sockaddr *addr, void *data)
|
||||
{
|
||||
query_t *query = (query_t *)data;
|
||||
server_t *server = query->server;
|
||||
@@ -1057,7 +1072,8 @@ static void server_resolve_cb(struct sockaddr *addr, void *data)
|
||||
}
|
||||
}
|
||||
|
||||
static void remote_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
remote_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
remote_ctx_t *remote_recv_ctx = (remote_ctx_t *)w;
|
||||
remote_t *remote = remote_recv_ctx->remote;
|
||||
@@ -1132,7 +1148,8 @@ static void remote_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
setsockopt(remote->fd, SOL_TCP, TCP_NODELAY, &opt, sizeof(opt));
|
||||
}
|
||||
|
||||
static void remote_send_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
remote_send_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
remote_ctx_t *remote_send_ctx = (remote_ctx_t *)w;
|
||||
remote_t *remote = remote_send_ctx->remote;
|
||||
@@ -1217,7 +1234,8 @@ static void remote_send_cb(EV_P_ ev_io *w, int revents)
|
||||
}
|
||||
}
|
||||
|
||||
static remote_t *new_remote(int fd)
|
||||
static remote_t *
|
||||
new_remote(int fd)
|
||||
{
|
||||
if (verbose) {
|
||||
remote_conn++;
|
||||
@@ -1244,7 +1262,8 @@ static remote_t *new_remote(int fd)
|
||||
return remote;
|
||||
}
|
||||
|
||||
static void free_remote(remote_t *remote)
|
||||
static void
|
||||
free_remote(remote_t *remote)
|
||||
{
|
||||
if (remote->server != NULL) {
|
||||
remote->server->remote = NULL;
|
||||
@@ -1258,7 +1277,8 @@ static void free_remote(remote_t *remote)
|
||||
ss_free(remote);
|
||||
}
|
||||
|
||||
static void close_and_free_remote(EV_P_ remote_t *remote)
|
||||
static void
|
||||
close_and_free_remote(EV_P_ remote_t *remote)
|
||||
{
|
||||
if (remote != NULL) {
|
||||
ev_io_stop(EV_A_ & remote->send_ctx->io);
|
||||
@@ -1272,7 +1292,8 @@ static void close_and_free_remote(EV_P_ remote_t *remote)
|
||||
}
|
||||
}
|
||||
|
||||
static server_t *new_server(int fd, listen_ctx_t *listener)
|
||||
static server_t *
|
||||
new_server(int fd, listen_ctx_t *listener)
|
||||
{
|
||||
if (verbose) {
|
||||
server_conn++;
|
||||
@@ -1325,7 +1346,8 @@ static server_t *new_server(int fd, listen_ctx_t *listener)
|
||||
return server;
|
||||
}
|
||||
|
||||
static void free_server(server_t *server)
|
||||
static void
|
||||
free_server(server_t *server)
|
||||
{
|
||||
cork_dllist_remove(&server->entries);
|
||||
|
||||
@@ -1361,7 +1383,8 @@ static void free_server(server_t *server)
|
||||
ss_free(server);
|
||||
}
|
||||
|
||||
static void close_and_free_server(EV_P_ server_t *server)
|
||||
static void
|
||||
close_and_free_server(EV_P_ server_t *server)
|
||||
{
|
||||
if (server != NULL) {
|
||||
if (server->query != NULL) {
|
||||
@@ -1380,7 +1403,8 @@ static void close_and_free_server(EV_P_ server_t *server)
|
||||
}
|
||||
}
|
||||
|
||||
static void signal_cb(EV_P_ ev_signal *w, int revents)
|
||||
static void
|
||||
signal_cb(EV_P_ ev_signal *w, int revents)
|
||||
{
|
||||
if (revents & EV_SIGNAL) {
|
||||
switch (w->signum) {
|
||||
@@ -1391,7 +1415,8 @@ static void signal_cb(EV_P_ ev_signal *w, int revents)
|
||||
}
|
||||
}
|
||||
|
||||
static void accept_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
accept_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
listen_ctx_t *listener = (listen_ctx_t *)w;
|
||||
int serverfd = accept(listener->fd, NULL, NULL);
|
||||
@@ -1404,7 +1429,7 @@ static void accept_cb(EV_P_ ev_io *w, int revents)
|
||||
char *peer_name = get_peer_name(serverfd);
|
||||
if (peer_name != NULL) {
|
||||
if ((get_acl_mode() == BLACK_LIST && acl_match_host(peer_name) == 1)
|
||||
|| (get_acl_mode() == WHITE_LIST && acl_match_host(peer_name) >= 0)) {
|
||||
|| (get_acl_mode() == WHITE_LIST && acl_match_host(peer_name) >= 0)) {
|
||||
if (verbose)
|
||||
LOGI("Access denied from %s", peer_name);
|
||||
close(serverfd);
|
||||
@@ -1429,7 +1454,8 @@ static void accept_cb(EV_P_ ev_io *w, int revents)
|
||||
ev_timer_start(EV_A_ & server->recv_ctx->watcher);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
int i, c;
|
||||
int pid_flags = 0;
|
||||
@@ -1560,7 +1586,6 @@ int main(int argc, char **argv)
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
if (argc == 1) {
|
||||
if (conf_path == NULL) {
|
||||
conf_path = DEFAULT_CONF_PATH;
|
||||
|
||||
@@ -61,12 +61,11 @@ static int parse_extensions(const char *, size_t, char **);
|
||||
static int parse_server_name_extension(const char *, size_t, char **);
|
||||
|
||||
static const protocol_t tls_protocol_st = {
|
||||
.default_port = 443,
|
||||
.default_port = 443,
|
||||
.parse_packet = &parse_tls_header,
|
||||
};
|
||||
const protocol_t *const tls_protocol = &tls_protocol_st;
|
||||
|
||||
|
||||
/* Parse a TLS packet for the Server Name Indication extension in the client
|
||||
* hello handshake, returning the first servername found (pointer to static
|
||||
* array)
|
||||
@@ -81,7 +80,8 @@ const protocol_t *const tls_protocol = &tls_protocol_st;
|
||||
* < -4 - Invalid TLS client hello
|
||||
*/
|
||||
static int
|
||||
parse_tls_header(const char *data, size_t data_len, char **hostname) {
|
||||
parse_tls_header(const char *data, size_t data_len, char **hostname)
|
||||
{
|
||||
char tls_content_type;
|
||||
char tls_version_major;
|
||||
char tls_version_minor;
|
||||
@@ -116,14 +116,14 @@ parse_tls_header(const char *data, size_t data_len, char **hostname) {
|
||||
tls_version_minor = data[2];
|
||||
if (tls_version_major < 3) {
|
||||
LOGI("Received SSL %d.%d handshake which can not support SNI.",
|
||||
tls_version_major, tls_version_minor);
|
||||
tls_version_major, tls_version_minor);
|
||||
|
||||
return -2;
|
||||
}
|
||||
|
||||
/* TLS record length */
|
||||
len = ((unsigned char)data[3] << 8) +
|
||||
(unsigned char)data[4] + TLS_HEADER_LEN;
|
||||
(unsigned char)data[4] + TLS_HEADER_LEN;
|
||||
data_len = MIN(data_len, len);
|
||||
|
||||
/* Check we received entire TLS record length */
|
||||
@@ -143,30 +143,30 @@ parse_tls_header(const char *data, size_t data_len, char **hostname) {
|
||||
}
|
||||
|
||||
/* Skip past fixed length records:
|
||||
1 Handshake Type
|
||||
3 Length
|
||||
2 Version (again)
|
||||
32 Random
|
||||
to Session ID Length
|
||||
* 1 Handshake Type
|
||||
* 3 Length
|
||||
* 2 Version (again)
|
||||
* 32 Random
|
||||
* to Session ID Length
|
||||
*/
|
||||
pos += 38;
|
||||
|
||||
/* Session ID */
|
||||
if (pos + 1 > data_len)
|
||||
return -5;
|
||||
len = (unsigned char)data[pos];
|
||||
len = (unsigned char)data[pos];
|
||||
pos += 1 + len;
|
||||
|
||||
/* Cipher Suites */
|
||||
if (pos + 2 > data_len)
|
||||
return -5;
|
||||
len = ((unsigned char)data[pos] << 8) + (unsigned char)data[pos + 1];
|
||||
len = ((unsigned char)data[pos] << 8) + (unsigned char)data[pos + 1];
|
||||
pos += 2 + len;
|
||||
|
||||
/* Compression Methods */
|
||||
if (pos + 1 > data_len)
|
||||
return -5;
|
||||
len = (unsigned char)data[pos];
|
||||
len = (unsigned char)data[pos];
|
||||
pos += 1 + len;
|
||||
|
||||
if (pos == data_len && tls_version_major == 3 && tls_version_minor == 0) {
|
||||
@@ -177,7 +177,7 @@ parse_tls_header(const char *data, size_t data_len, char **hostname) {
|
||||
/* Extensions */
|
||||
if (pos + 2 > data_len)
|
||||
return -5;
|
||||
len = ((unsigned char)data[pos] << 8) + (unsigned char)data[pos + 1];
|
||||
len = ((unsigned char)data[pos] << 8) + (unsigned char)data[pos + 1];
|
||||
pos += 2;
|
||||
|
||||
if (pos + len > data_len)
|
||||
@@ -186,7 +186,8 @@ parse_tls_header(const char *data, size_t data_len, char **hostname) {
|
||||
}
|
||||
|
||||
static int
|
||||
parse_extensions(const char *data, size_t data_len, char **hostname) {
|
||||
parse_extensions(const char *data, size_t data_len, char **hostname)
|
||||
{
|
||||
size_t pos = 0;
|
||||
size_t len;
|
||||
|
||||
@@ -194,12 +195,12 @@ parse_extensions(const char *data, size_t data_len, char **hostname) {
|
||||
while (pos + 4 <= data_len) {
|
||||
/* Extension Length */
|
||||
len = ((unsigned char)data[pos + 2] << 8) +
|
||||
(unsigned char)data[pos + 3];
|
||||
(unsigned char)data[pos + 3];
|
||||
|
||||
/* Check if it's a server name extension */
|
||||
if (data[pos] == 0x00 && data[pos + 1] == 0x00) {
|
||||
/* There can be only one extension of each type, so we break
|
||||
our state and move p to beinnging of the extension here */
|
||||
* our state and move p to beinnging of the extension here */
|
||||
if (pos + 4 + len > data_len)
|
||||
return -5;
|
||||
return parse_server_name_extension(data + pos + 4, len, hostname);
|
||||
@@ -215,33 +216,34 @@ parse_extensions(const char *data, size_t data_len, char **hostname) {
|
||||
|
||||
static int
|
||||
parse_server_name_extension(const char *data, size_t data_len,
|
||||
char **hostname) {
|
||||
char **hostname)
|
||||
{
|
||||
size_t pos = 2; /* skip server name list length */
|
||||
size_t len;
|
||||
|
||||
while (pos + 3 < data_len) {
|
||||
len = ((unsigned char)data[pos + 1] << 8) +
|
||||
(unsigned char)data[pos + 2];
|
||||
(unsigned char)data[pos + 2];
|
||||
|
||||
if (pos + 3 + len > data_len)
|
||||
return -5;
|
||||
|
||||
switch (data[pos]) { /* name type */
|
||||
case 0x00: /* host_name */
|
||||
*hostname = malloc(len + 1);
|
||||
if (*hostname == NULL) {
|
||||
ERROR("malloc() failure");
|
||||
return -4;
|
||||
}
|
||||
case 0x00: /* host_name */
|
||||
*hostname = malloc(len + 1);
|
||||
if (*hostname == NULL) {
|
||||
ERROR("malloc() failure");
|
||||
return -4;
|
||||
}
|
||||
|
||||
strncpy(*hostname, data + pos + 3, len);
|
||||
strncpy(*hostname, data + pos + 3, len);
|
||||
|
||||
(*hostname)[len] = '\0';
|
||||
(*hostname)[len] = '\0';
|
||||
|
||||
return len;
|
||||
default:
|
||||
LOGI("Unknown server name extension name type: %d",
|
||||
data[pos]);
|
||||
return len;
|
||||
default:
|
||||
LOGI("Unknown server name extension name type: %d",
|
||||
data[pos]);
|
||||
}
|
||||
pos += 3 + len;
|
||||
}
|
||||
|
||||
+32
-16
@@ -100,7 +100,8 @@ static int nofile = 0;
|
||||
#endif
|
||||
|
||||
#ifndef __MINGW32__
|
||||
static int setnonblocking(int fd)
|
||||
static int
|
||||
setnonblocking(int fd)
|
||||
{
|
||||
int flags;
|
||||
if (-1 == (flags = fcntl(fd, F_GETFL, 0))) {
|
||||
@@ -111,7 +112,8 @@ static int setnonblocking(int fd)
|
||||
|
||||
#endif
|
||||
|
||||
int create_and_bind(const char *addr, const char *port)
|
||||
int
|
||||
create_and_bind(const char *addr, const char *port)
|
||||
{
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *result, *rp;
|
||||
@@ -164,7 +166,8 @@ int create_and_bind(const char *addr, const char *port)
|
||||
return listen_sock;
|
||||
}
|
||||
|
||||
static void server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
server_ctx_t *server_recv_ctx = (server_ctx_t *)w;
|
||||
server_t *server = server_recv_ctx->server;
|
||||
@@ -234,7 +237,8 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
}
|
||||
}
|
||||
|
||||
static void server_send_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
server_send_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
server_ctx_t *server_send_ctx = (server_ctx_t *)w;
|
||||
server_t *server = server_send_ctx->server;
|
||||
@@ -276,7 +280,8 @@ static void server_send_cb(EV_P_ ev_io *w, int revents)
|
||||
}
|
||||
}
|
||||
|
||||
static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents)
|
||||
static void
|
||||
remote_timeout_cb(EV_P_ ev_timer *watcher, int revents)
|
||||
{
|
||||
remote_ctx_t *remote_ctx = (remote_ctx_t *)(((void *)watcher)
|
||||
- sizeof(ev_io));
|
||||
@@ -293,7 +298,8 @@ static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents)
|
||||
close_and_free_server(EV_A_ server);
|
||||
}
|
||||
|
||||
static void remote_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
remote_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
remote_ctx_t *remote_recv_ctx = (remote_ctx_t *)w;
|
||||
remote_t *remote = remote_recv_ctx->remote;
|
||||
@@ -356,7 +362,8 @@ static void remote_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
setsockopt(remote->fd, SOL_TCP, TCP_NODELAY, &opt, sizeof(opt));
|
||||
}
|
||||
|
||||
static void remote_send_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
remote_send_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
remote_ctx_t *remote_send_ctx = (remote_ctx_t *)w;
|
||||
remote_t *remote = remote_send_ctx->remote;
|
||||
@@ -488,7 +495,8 @@ static void remote_send_cb(EV_P_ ev_io *w, int revents)
|
||||
}
|
||||
}
|
||||
|
||||
static remote_t *new_remote(int fd, int timeout)
|
||||
static remote_t *
|
||||
new_remote(int fd, int timeout)
|
||||
{
|
||||
remote_t *remote;
|
||||
remote = ss_malloc(sizeof(remote_t));
|
||||
@@ -514,7 +522,8 @@ static remote_t *new_remote(int fd, int timeout)
|
||||
return remote;
|
||||
}
|
||||
|
||||
static void free_remote(remote_t *remote)
|
||||
static void
|
||||
free_remote(remote_t *remote)
|
||||
{
|
||||
if (remote != NULL) {
|
||||
if (remote->server != NULL) {
|
||||
@@ -530,7 +539,8 @@ static void free_remote(remote_t *remote)
|
||||
}
|
||||
}
|
||||
|
||||
static void close_and_free_remote(EV_P_ remote_t *remote)
|
||||
static void
|
||||
close_and_free_remote(EV_P_ remote_t *remote)
|
||||
{
|
||||
if (remote != NULL) {
|
||||
ev_timer_stop(EV_A_ & remote->send_ctx->watcher);
|
||||
@@ -541,7 +551,8 @@ static void close_and_free_remote(EV_P_ remote_t *remote)
|
||||
}
|
||||
}
|
||||
|
||||
static server_t *new_server(int fd, int method)
|
||||
static server_t *
|
||||
new_server(int fd, int method)
|
||||
{
|
||||
server_t *server;
|
||||
|
||||
@@ -573,7 +584,8 @@ static server_t *new_server(int fd, int method)
|
||||
return server;
|
||||
}
|
||||
|
||||
static void free_server(server_t *server)
|
||||
static void
|
||||
free_server(server_t *server)
|
||||
{
|
||||
if (server != NULL) {
|
||||
if (server->remote != NULL) {
|
||||
@@ -597,7 +609,8 @@ static void free_server(server_t *server)
|
||||
}
|
||||
}
|
||||
|
||||
static void close_and_free_server(EV_P_ server_t *server)
|
||||
static void
|
||||
close_and_free_server(EV_P_ server_t *server)
|
||||
{
|
||||
if (server != NULL) {
|
||||
ev_io_stop(EV_A_ & server->send_ctx->io);
|
||||
@@ -607,7 +620,8 @@ static void close_and_free_server(EV_P_ server_t *server)
|
||||
}
|
||||
}
|
||||
|
||||
static void accept_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
accept_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
struct listen_ctx *listener = (struct listen_ctx *)w;
|
||||
int serverfd = accept(listener->fd, NULL, NULL);
|
||||
@@ -696,13 +710,15 @@ static void accept_cb(EV_P_ ev_io *w, int revents)
|
||||
}
|
||||
}
|
||||
|
||||
void signal_cb(int dummy)
|
||||
void
|
||||
signal_cb(int dummy)
|
||||
{
|
||||
keep_resolving = 0;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
srand(time(NULL));
|
||||
|
||||
|
||||
+50
-28
@@ -104,7 +104,8 @@ static int server_num = 0;
|
||||
static server_ctx_t *server_ctx_list[MAX_REMOTE_NUM] = { NULL };
|
||||
|
||||
#ifndef __MINGW32__
|
||||
static int setnonblocking(int fd)
|
||||
static int
|
||||
setnonblocking(int fd)
|
||||
{
|
||||
int flags;
|
||||
if (-1 == (flags = fcntl(fd, F_GETFL, 0))) {
|
||||
@@ -116,7 +117,8 @@ static int setnonblocking(int fd)
|
||||
#endif
|
||||
|
||||
#if defined(MODULE_REMOTE) && defined(SO_BROADCAST)
|
||||
static int set_broadcast(int socket_fd)
|
||||
static int
|
||||
set_broadcast(int socket_fd)
|
||||
{
|
||||
int opt = 1;
|
||||
return setsockopt(socket_fd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(opt));
|
||||
@@ -125,7 +127,8 @@ static int set_broadcast(int socket_fd)
|
||||
#endif
|
||||
|
||||
#ifdef SO_NOSIGPIPE
|
||||
static int set_nosigpipe(int socket_fd)
|
||||
static int
|
||||
set_nosigpipe(int socket_fd)
|
||||
{
|
||||
int opt = 1;
|
||||
return setsockopt(socket_fd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
|
||||
@@ -143,7 +146,8 @@ static int set_nosigpipe(int socket_fd)
|
||||
#define IP_RECVORIGDSTADDR 20
|
||||
#endif
|
||||
|
||||
static int get_dstaddr(struct msghdr *msg, struct sockaddr_storage *dstaddr)
|
||||
static int
|
||||
get_dstaddr(struct msghdr *msg, struct sockaddr_storage *dstaddr)
|
||||
{
|
||||
struct cmsghdr *cmsg;
|
||||
|
||||
@@ -165,7 +169,8 @@ static int get_dstaddr(struct msghdr *msg, struct sockaddr_storage *dstaddr)
|
||||
#endif
|
||||
|
||||
#define HASH_KEY_LEN sizeof(struct sockaddr_storage) + sizeof(int)
|
||||
static char *hash_key(const int af, const struct sockaddr_storage *addr)
|
||||
static char *
|
||||
hash_key(const int af, const struct sockaddr_storage *addr)
|
||||
{
|
||||
size_t addr_len = sizeof(struct sockaddr_storage);
|
||||
static char key[HASH_KEY_LEN];
|
||||
@@ -178,8 +183,9 @@ static char *hash_key(const int af, const struct sockaddr_storage *addr)
|
||||
}
|
||||
|
||||
#if defined(MODULE_REDIR) || defined(MODULE_REMOTE)
|
||||
static int construct_udprealy_header(const struct sockaddr_storage *in_addr,
|
||||
char *addr_header)
|
||||
static int
|
||||
construct_udprealy_header(const struct sockaddr_storage *in_addr,
|
||||
char *addr_header)
|
||||
{
|
||||
int addr_header_len = 0;
|
||||
if (in_addr->ss_family == AF_INET) {
|
||||
@@ -206,9 +212,10 @@ static int construct_udprealy_header(const struct sockaddr_storage *in_addr,
|
||||
|
||||
#endif
|
||||
|
||||
static int parse_udprealy_header(const char *buf, const size_t buf_len,
|
||||
int *auth, char *host, char *port,
|
||||
struct sockaddr_storage *storage)
|
||||
static int
|
||||
parse_udprealy_header(const char *buf, const size_t buf_len,
|
||||
int *auth, char *host, char *port,
|
||||
struct sockaddr_storage *storage)
|
||||
{
|
||||
const uint8_t atyp = *(uint8_t *)buf;
|
||||
int offset = 1;
|
||||
@@ -292,7 +299,8 @@ static int parse_udprealy_header(const char *buf, const size_t buf_len,
|
||||
return offset;
|
||||
}
|
||||
|
||||
static char *get_addr_str(const struct sockaddr *sa)
|
||||
static char *
|
||||
get_addr_str(const struct sockaddr *sa)
|
||||
{
|
||||
static char s[SS_ADDRSTRLEN];
|
||||
memset(s, 0, SS_ADDRSTRLEN);
|
||||
@@ -328,7 +336,8 @@ static char *get_addr_str(const struct sockaddr *sa)
|
||||
return s;
|
||||
}
|
||||
|
||||
int create_remote_socket(int ipv6)
|
||||
int
|
||||
create_remote_socket(int ipv6)
|
||||
{
|
||||
int remote_sock;
|
||||
|
||||
@@ -369,7 +378,8 @@ int create_remote_socket(int ipv6)
|
||||
return remote_sock;
|
||||
}
|
||||
|
||||
int create_server_socket(const char *host, const char *port)
|
||||
int
|
||||
create_server_socket(const char *host, const char *port)
|
||||
{
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *result, *rp, *ipv4v6bindall;
|
||||
@@ -465,7 +475,8 @@ int create_server_socket(const char *host, const char *port)
|
||||
return server_sock;
|
||||
}
|
||||
|
||||
remote_ctx_t *new_remote(int fd, server_ctx_t *server_ctx)
|
||||
remote_ctx_t *
|
||||
new_remote(int fd, server_ctx_t *server_ctx)
|
||||
{
|
||||
remote_ctx_t *ctx = ss_malloc(sizeof(remote_ctx_t));
|
||||
memset(ctx, 0, sizeof(remote_ctx_t));
|
||||
@@ -480,7 +491,8 @@ remote_ctx_t *new_remote(int fd, server_ctx_t *server_ctx)
|
||||
return ctx;
|
||||
}
|
||||
|
||||
server_ctx_t *new_server_ctx(int fd)
|
||||
server_ctx_t *
|
||||
new_server_ctx(int fd)
|
||||
{
|
||||
server_ctx_t *ctx = ss_malloc(sizeof(server_ctx_t));
|
||||
memset(ctx, 0, sizeof(server_ctx_t));
|
||||
@@ -493,7 +505,8 @@ server_ctx_t *new_server_ctx(int fd)
|
||||
}
|
||||
|
||||
#ifdef MODULE_REMOTE
|
||||
struct query_ctx *new_query_ctx(char *buf, size_t len)
|
||||
struct query_ctx *
|
||||
new_query_ctx(char *buf, size_t len)
|
||||
{
|
||||
struct query_ctx *ctx = ss_malloc(sizeof(struct query_ctx));
|
||||
memset(ctx, 0, sizeof(struct query_ctx));
|
||||
@@ -504,7 +517,8 @@ struct query_ctx *new_query_ctx(char *buf, size_t len)
|
||||
return ctx;
|
||||
}
|
||||
|
||||
void close_and_free_query(EV_P_ struct query_ctx *ctx)
|
||||
void
|
||||
close_and_free_query(EV_P_ struct query_ctx *ctx)
|
||||
{
|
||||
if (ctx != NULL) {
|
||||
if (ctx->query != NULL) {
|
||||
@@ -521,7 +535,8 @@ void close_and_free_query(EV_P_ struct query_ctx *ctx)
|
||||
|
||||
#endif
|
||||
|
||||
void close_and_free_remote(EV_P_ remote_ctx_t *ctx)
|
||||
void
|
||||
close_and_free_remote(EV_P_ remote_ctx_t *ctx)
|
||||
{
|
||||
if (ctx != NULL) {
|
||||
ev_timer_stop(EV_A_ & ctx->watcher);
|
||||
@@ -531,7 +546,8 @@ void close_and_free_remote(EV_P_ remote_ctx_t *ctx)
|
||||
}
|
||||
}
|
||||
|
||||
static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents)
|
||||
static void
|
||||
remote_timeout_cb(EV_P_ ev_timer *watcher, int revents)
|
||||
{
|
||||
remote_ctx_t *remote_ctx = (remote_ctx_t *)(((void *)watcher)
|
||||
- sizeof(ev_io));
|
||||
@@ -545,7 +561,8 @@ static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents)
|
||||
}
|
||||
|
||||
#ifdef MODULE_REMOTE
|
||||
static void query_resolve_cb(struct sockaddr *addr, void *data)
|
||||
static void
|
||||
query_resolve_cb(struct sockaddr *addr, void *data)
|
||||
{
|
||||
struct query_ctx *query_ctx = (struct query_ctx *)data;
|
||||
struct ev_loop *loop = query_ctx->server_ctx->loop;
|
||||
@@ -632,7 +649,8 @@ static void query_resolve_cb(struct sockaddr *addr, void *data)
|
||||
|
||||
#endif
|
||||
|
||||
static void remote_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
remote_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
ssize_t r;
|
||||
remote_ctx_t *remote_ctx = (remote_ctx_t *)w;
|
||||
@@ -804,7 +822,8 @@ CLEAN_UP:
|
||||
ss_free(buf);
|
||||
}
|
||||
|
||||
static void server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
static void
|
||||
server_recv_cb(EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
server_ctx_t *server_ctx = (server_ctx_t *)w;
|
||||
struct sockaddr_storage src_addr;
|
||||
@@ -1269,7 +1288,8 @@ CLEAN_UP:
|
||||
ss_free(buf);
|
||||
}
|
||||
|
||||
void free_cb(void *element)
|
||||
void
|
||||
free_cb(void *element)
|
||||
{
|
||||
remote_ctx_t *remote_ctx = (remote_ctx_t *)element;
|
||||
|
||||
@@ -1280,14 +1300,15 @@ void free_cb(void *element)
|
||||
close_and_free_remote(EV_DEFAULT, remote_ctx);
|
||||
}
|
||||
|
||||
int init_udprelay(const char *server_host, const char *server_port,
|
||||
int
|
||||
init_udprelay(const char *server_host, const char *server_port,
|
||||
#ifdef MODULE_LOCAL
|
||||
const struct sockaddr *remote_addr, const int remote_addr_len,
|
||||
const struct sockaddr *remote_addr, const int remote_addr_len,
|
||||
#ifdef MODULE_TUNNEL
|
||||
const ss_addr_t tunnel_addr,
|
||||
const ss_addr_t tunnel_addr,
|
||||
#endif
|
||||
#endif
|
||||
int mtu, int method, int auth, int timeout, const char *iface)
|
||||
int mtu, int method, int auth, int timeout, const char *iface)
|
||||
{
|
||||
// Initialize ev loop
|
||||
struct ev_loop *loop = EV_DEFAULT;
|
||||
@@ -1336,7 +1357,8 @@ int init_udprelay(const char *server_host, const char *server_port,
|
||||
return 0;
|
||||
}
|
||||
|
||||
void free_udprelay()
|
||||
void
|
||||
free_udprelay()
|
||||
{
|
||||
struct ev_loop *loop = EV_DEFAULT;
|
||||
while (server_num-- > 0) {
|
||||
|
||||
+20
-11
@@ -53,7 +53,8 @@ int use_syslog = 0;
|
||||
#endif
|
||||
|
||||
#ifndef __MINGW32__
|
||||
void ERROR(const char *s)
|
||||
void
|
||||
ERROR(const char *s)
|
||||
{
|
||||
char *msg = strerror(errno);
|
||||
LOGE("%s: %s", s, msg);
|
||||
@@ -63,7 +64,8 @@ void ERROR(const char *s)
|
||||
|
||||
int use_tty = 1;
|
||||
|
||||
char *ss_itoa(int i)
|
||||
char *
|
||||
ss_itoa(int i)
|
||||
{
|
||||
/* Room for INT_DIGITS digits, - and '\0' */
|
||||
static char buf[INT_DIGITS + 2];
|
||||
@@ -87,7 +89,8 @@ char *ss_itoa(int i)
|
||||
/*
|
||||
* setuid() and setgid() for a specified user.
|
||||
*/
|
||||
int run_as(const char *user)
|
||||
int
|
||||
run_as(const char *user)
|
||||
{
|
||||
#ifndef __MINGW32__
|
||||
if (user[0]) {
|
||||
@@ -164,7 +167,8 @@ int run_as(const char *user)
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *ss_strndup(const char *s, size_t n)
|
||||
char *
|
||||
ss_strndup(const char *s, size_t n)
|
||||
{
|
||||
size_t len = strlen(s);
|
||||
char *ret;
|
||||
@@ -179,13 +183,15 @@ char *ss_strndup(const char *s, size_t n)
|
||||
return ret;
|
||||
}
|
||||
|
||||
void FATAL(const char *msg)
|
||||
void
|
||||
FATAL(const char *msg)
|
||||
{
|
||||
LOGE("%s", msg);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
void *ss_malloc(size_t size)
|
||||
void *
|
||||
ss_malloc(size_t size)
|
||||
{
|
||||
void *tmp = malloc(size);
|
||||
if (tmp == NULL)
|
||||
@@ -193,7 +199,8 @@ void *ss_malloc(size_t size)
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void *ss_realloc(void *ptr, size_t new_size)
|
||||
void *
|
||||
ss_realloc(void *ptr, size_t new_size)
|
||||
{
|
||||
void *new = realloc(ptr, new_size);
|
||||
if (new == NULL) {
|
||||
@@ -204,8 +211,8 @@ void *ss_realloc(void *ptr, size_t new_size)
|
||||
return new;
|
||||
}
|
||||
|
||||
|
||||
void usage()
|
||||
void
|
||||
usage()
|
||||
{
|
||||
printf("\n");
|
||||
printf("shadowsocks-libev %s\n\n", VERSION);
|
||||
@@ -319,7 +326,8 @@ void usage()
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void daemonize(const char *path)
|
||||
void
|
||||
daemonize(const char *path)
|
||||
{
|
||||
#ifndef __MINGW32__
|
||||
/* Our process ID and Session ID */
|
||||
@@ -370,7 +378,8 @@ void daemonize(const char *path)
|
||||
}
|
||||
|
||||
#ifdef HAVE_SETRLIMIT
|
||||
int set_nofile(int nofile)
|
||||
int
|
||||
set_nofile(int nofile)
|
||||
{
|
||||
struct rlimit limit = { nofile, nofile }; /* set both soft and hard limit */
|
||||
|
||||
|
||||
+1
-1
@@ -210,6 +210,6 @@ void *ss_realloc(void *ptr, size_t new_size);
|
||||
do { \
|
||||
free(ptr); \
|
||||
ptr = NULL; \
|
||||
} while(0)
|
||||
} while (0)
|
||||
|
||||
#endif // _UTILS_H
|
||||
+12
-6
@@ -27,7 +27,8 @@
|
||||
#undef setsockopt
|
||||
#endif
|
||||
|
||||
void winsock_init(void)
|
||||
void
|
||||
winsock_init(void)
|
||||
{
|
||||
WORD wVersionRequested;
|
||||
WSADATA wsaData;
|
||||
@@ -43,12 +44,14 @@ void winsock_init(void)
|
||||
}
|
||||
}
|
||||
|
||||
void winsock_cleanup(void)
|
||||
void
|
||||
winsock_cleanup(void)
|
||||
{
|
||||
WSACleanup();
|
||||
}
|
||||
|
||||
void ss_error(const char *s)
|
||||
void
|
||||
ss_error(const char *s)
|
||||
{
|
||||
LPVOID *msg = NULL;
|
||||
FormatMessage(
|
||||
@@ -62,7 +65,8 @@ void ss_error(const char *s)
|
||||
}
|
||||
}
|
||||
|
||||
int setnonblocking(int fd)
|
||||
int
|
||||
setnonblocking(int fd)
|
||||
{
|
||||
u_long iMode = 1;
|
||||
long int iResult;
|
||||
@@ -73,13 +77,15 @@ int setnonblocking(int fd)
|
||||
return iResult;
|
||||
}
|
||||
|
||||
size_t strnlen(const char *s, size_t maxlen)
|
||||
size_t
|
||||
strnlen(const char *s, size_t maxlen)
|
||||
{
|
||||
const char *end = memchr(s, 0, maxlen);
|
||||
return end ? (size_t)(end - s) : maxlen;
|
||||
}
|
||||
|
||||
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size)
|
||||
const char *
|
||||
inet_ntop(int af, const void *src, char *dst, socklen_t size)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
unsigned long s = size;
|
||||
|
||||
Reference in new issue
Block a user