Skip to content

Commit

Permalink
Set TTL for both IP versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ruti committed Oct 15, 2024
1 parent 8116b0d commit 93587ce
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 54 deletions.
78 changes: 28 additions & 50 deletions desync.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,15 @@
#include "error.h"


int get_family(struct sockaddr *dst)
int setttl(int fd, int ttl)
{
#ifndef __NetBSD__
if (dst->sa_family == AF_INET6) {
struct sockaddr_in6 *d6 = (struct sockaddr_in6 *)dst;
static char *pat = "\0\0\0\0\0\0\0\0\0\0\xff\xff";

if (!memcmp(&d6->sin6_addr, pat, 12)) {
return AF_INET;
}
}
#endif
return dst->sa_family;
}


int setttl(int fd, int ttl, int family) {
int _ttl = ttl;
int ret6 = setsockopt(fd, IPPROTO_IPV6,
IPV6_UNICAST_HOPS, (char *)&ttl, sizeof(ttl));
int ret4 = setsockopt(fd, IPPROTO_IP,
IP_TTL, (char *)&ttl, sizeof(ttl));

if (family == AF_INET) {
if (setsockopt(fd, IPPROTO_IP,
IP_TTL, (char *)&_ttl, sizeof(_ttl)) < 0) {
uniperror("setsockopt IP_TTL");
return -1;
}
}
else if (setsockopt(fd, IPPROTO_IPV6,
IPV6_UNICAST_HOPS, (char *)&_ttl, sizeof(_ttl)) < 0) {
uniperror("setsockopt IPV6_UNICAST_HOPS");
if (ret4 && ret6) {
uniperror("setttl");
return -1;
}
return 0;
Expand Down Expand Up @@ -143,7 +123,7 @@ void wait_send(int sfd)

#ifdef __linux__
ssize_t send_fake(int sfd, char *buffer,
int cnt, long pos, int fa, struct desync_params *opt)
int cnt, long pos, struct desync_params *opt)
{
struct sockaddr_in6 addr = {};
socklen_t addr_size = sizeof(addr);
Expand Down Expand Up @@ -185,7 +165,7 @@ ssize_t send_fake(int sfd, char *buffer,
}
memcpy(p, pkt.data, pkt.size < pos ? pkt.size : pos);

if (setttl(sfd, opt->ttl ? opt->ttl : 8, fa) < 0) {
if (setttl(sfd, opt->ttl ? opt->ttl : 8) < 0) {
break;
}
if (opt->md5sig) {
Expand All @@ -200,11 +180,11 @@ ssize_t send_fake(int sfd, char *buffer,
break;
}
}
if (opt->ip_options && fa == AF_INET
if (opt->ip_options
&& setsockopt(sfd, IPPROTO_IP, IP_OPTIONS,
opt->ip_options, opt->ip_options_len) < 0) {
uniperror("setsockopt IP_OPTIONS");
break;
//break;
}
struct iovec vec = { .iov_base = p, .iov_len = pos };

Expand All @@ -221,14 +201,14 @@ ssize_t send_fake(int sfd, char *buffer,
wait_send(sfd);
memcpy(p, buffer, pos);

if (setttl(sfd, params.def_ttl, fa) < 0) {
if (setttl(sfd, params.def_ttl) < 0) {
break;
}
if (opt->ip_options && fa == AF_INET
if (opt->ip_options
&& setsockopt(sfd, IPPROTO_IP,
IP_OPTIONS, opt->ip_options, 0) < 0) {
uniperror("setsockopt IP_OPTIONS");
break;
//break;
}
if (opt->md5sig) {
struct tcp_md5sig md5 = {
Expand All @@ -255,7 +235,7 @@ ssize_t send_fake(int sfd, char *buffer,
OVERLAPPED ov = {};

ssize_t send_fake(int sfd, char *buffer,
int cnt, long pos, int fa, struct desync_params *opt)
int cnt, long pos, struct desync_params *opt)
{
struct packet pkt;
if (opt->fake_data.data) {
Expand Down Expand Up @@ -318,7 +298,7 @@ ssize_t send_fake(int sfd, char *buffer,
uniperror("SetFilePointer");
break;
}
if (setttl(sfd, opt->ttl ? opt->ttl : 8, fa) < 0) {
if (setttl(sfd, opt->ttl ? opt->ttl : 8) < 0) {
break;
}
if (!TransmitFile(sfd, hfile, pos, pos, &ov,
Expand All @@ -339,7 +319,7 @@ ssize_t send_fake(int sfd, char *buffer,
uniperror("WriteFile");
break;
}
if (setttl(sfd, params.def_ttl, fa) < 0) {
if (setttl(sfd, params.def_ttl) < 0) {
break;
}
len = pos;
Expand Down Expand Up @@ -378,11 +358,11 @@ ssize_t send_oob(int sfd, char *buffer,


ssize_t send_disorder(int sfd,
char *buffer, long pos, int fa)
char *buffer, long pos)
{
int bttl = 1;

if (setttl(sfd, bttl, fa) < 0) {
if (setttl(sfd, bttl) < 0) {
return -1;
}
ssize_t len = send(sfd, buffer, pos, 0);
Expand All @@ -391,26 +371,26 @@ ssize_t send_disorder(int sfd,
}
else wait_send_if_support(sfd);

if (setttl(sfd, params.def_ttl, fa) < 0) {
if (setttl(sfd, params.def_ttl) < 0) {
return -1;
}
return len;
}


ssize_t send_late_oob(int sfd, char *buffer,
ssize_t n, long pos, int fa, char *c)
ssize_t n, long pos, char *c)
{
int bttl = 1;

if (setttl(sfd, bttl, fa) < 0) {
if (setttl(sfd, bttl) < 0) {
return -1;
}
ssize_t len = send_oob(sfd, buffer, n, pos, c);
if (len < 0) {
uniperror("send");
}
if (setttl(sfd, params.def_ttl, fa) < 0) {
if (setttl(sfd, params.def_ttl) < 0) {
return -1;
}
return len;
Expand Down Expand Up @@ -453,7 +433,6 @@ ssize_t desync(int sfd, char *buffer, size_t bfsize,

char *host = 0;
int len = 0, type = 0, host_pos = 0;
int fa = get_family(dst);

// parse packet
if ((len = parse_tls(buffer, n, &host))) {
Expand Down Expand Up @@ -530,12 +509,12 @@ ssize_t desync(int sfd, char *buffer, size_t bfsize,
#ifdef FAKE_SUPPORT
case DESYNC_FAKE:
if (pos != lp) s = send_fake(sfd,
buffer + lp, type, pos - lp, fa, &dp);
buffer + lp, type, pos - lp, &dp);
break;
#endif
case DESYNC_DISORDER:
s = send_disorder(sfd,
buffer + lp, pos - lp, fa);
buffer + lp, pos - lp);
break;

case DESYNC_OOB:
Expand All @@ -545,7 +524,7 @@ ssize_t desync(int sfd, char *buffer, size_t bfsize,

case DESYNC_DISOOB:
s = send_late_oob(sfd,
buffer + lp, n - lp, pos - lp, fa, dp.oob_char);
buffer + lp, n - lp, pos - lp, dp.oob_char);
break;

case DESYNC_SPLIT:
Expand Down Expand Up @@ -607,7 +586,6 @@ ssize_t desync_udp(int sfd, char *buffer, size_t bfsize,
ssize_t n, struct sockaddr *dst, int dp_c)
{
struct desync_params *dp = &params.dp[dp_c];
int fa = get_family(dst);

if (dp->udp_fake_count != 0) {
struct packet pkt;
Expand All @@ -625,7 +603,7 @@ ssize_t desync_udp(int sfd, char *buffer, size_t bfsize,
else pkt.size = 0;
}
int bttl = dp->ttl ? dp->ttl : 8;
if (setttl(sfd, bttl, fa) < 0) {
if (setttl(sfd, bttl) < 0) {
return -1;
}
for (int i = 0; i < dp->udp_fake_count; i++) {
Expand All @@ -636,7 +614,7 @@ ssize_t desync_udp(int sfd, char *buffer, size_t bfsize,
return -1;
}
}
if (setttl(sfd, params.def_ttl, fa) < 0) {
if (setttl(sfd, params.def_ttl) < 0) {
return -1;
}
}
Expand Down
4 changes: 1 addition & 3 deletions desync.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ ssize_t desync(int sfd, char *buffer, size_t bfsize, ssize_t n, ssize_t offset,

ssize_t desync_udp(int sfd, char *buffer, size_t bfsize, ssize_t n, struct sockaddr *dst, int dp_c);

int get_family(struct sockaddr *dst);

int setttl(int fd, int ttl, int family);
int setttl(int fd, int ttl);

int post_desync(int sfd, int dp_c);

Expand Down
2 changes: 1 addition & 1 deletion extend.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ int connect_hook(struct poolhd *pool, struct eval *val,
int socket_mod(int fd, struct sockaddr *dst)
{
if (params.custom_ttl) {
if (setttl(fd, params.def_ttl, get_family(dst)) < 0) {
if (setttl(fd, params.def_ttl) < 0) {
return -1;
}
}
Expand Down

0 comments on commit 93587ce

Please sign in to comment.