-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate freebind and support fwmark option on linux
- Loading branch information
Showing
5 changed files
with
98 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
add_library(TcpConnector STATIC | ||
tcp_connector.c | ||
freebind.c | ||
|
||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Remember to define _CRT_RAND_S before you include | ||
// stdlib.h. | ||
#define _CRT_RAND_S | ||
#include "freebind.h" | ||
#include "frand.h" | ||
#include "loggers/network_logger.h" | ||
#include "tunnel.h" | ||
|
||
bool applyFreeBindRandomDestIp(tunnel_t* self,socket_context_t *dest_ctx) | ||
{ | ||
|
||
tcp_connector_state_t* state = TSTATE(self); | ||
|
||
unsigned int seed = fastRand(); | ||
|
||
switch (dest_ctx->address.sa.sa_family) | ||
{ | ||
case AF_INET: | ||
// no probelm if overflows | ||
{ | ||
#ifdef OS_UNIX | ||
const uint32_t large_random = (((uint32_t) rand_r(&seed)) % state->outbound_ip_range); | ||
#else | ||
const uint32_t large_random = (((uint32_t) rand_s(&seed)) % state->outbound_ip_range); | ||
#endif | ||
uint32_t calc = ntohl((uint32_t) dest_ctx->address.sin.sin_addr.s_addr); | ||
calc = calc & ~(state->outbound_ip_range - 1ULL); | ||
calc = htonl(calc + large_random); | ||
|
||
memcpy(&(dest_ctx->address.sin.sin_addr), &calc, sizeof(struct in_addr)); | ||
} | ||
break; | ||
case AF_INET6: | ||
// no probelm if overflows | ||
{ | ||
#ifdef OS_UNIX | ||
const uint64_t large_random = (((uint64_t) rand_r(&seed)) % state->outbound_ip_range); | ||
#else | ||
const uint64_t large_random = (((uint64_t) rand_s(&seed)) % state->outbound_ip_range); | ||
#endif | ||
uint64_t *addr_ptr = (uint64_t *) &dest_ctx->address.sin6.sin6_addr; | ||
addr_ptr += 1; | ||
|
||
uint64_t calc = ntohll(*addr_ptr); | ||
calc = calc & ~(state->outbound_ip_range - 1ULL); | ||
calc = htonll(calc + large_random); | ||
|
||
memcpy(8 + ((char *) &(dest_ctx->address.sin6.sin6_addr)), &calc, sizeof(calc)); | ||
} | ||
break; | ||
|
||
default: | ||
LOGE("TcpConnector: invalid destination address family"); | ||
return false; | ||
} | ||
return true; | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#pragma once | ||
#include "api.h" | ||
#include "types.h" | ||
|
||
|
||
bool applyFreeBindRandomDestIp(tunnel_t* self,socket_context_t *dest_ctx); | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters