Set TTL for both IP versions

#146 #17
This commit is contained in:
ruti 2024-10-15 21:52:18 +03:00
parent 8116b0de61
commit 93587ce8c1
3 changed files with 30 additions and 54 deletions

View File

@ -32,35 +32,15 @@
#include "error.h" #include "error.h"
int get_family(struct sockaddr *dst) int setttl(int fd, int ttl)
{ {
#ifndef __NetBSD__ int ret6 = setsockopt(fd, IPPROTO_IPV6,
if (dst->sa_family == AF_INET6) { IPV6_UNICAST_HOPS, (char *)&ttl, sizeof(ttl));
struct sockaddr_in6 *d6 = (struct sockaddr_in6 *)dst; int ret4 = setsockopt(fd, IPPROTO_IP,
static char *pat = "\0\0\0\0\0\0\0\0\0\0\xff\xff"; IP_TTL, (char *)&ttl, sizeof(ttl));
if (!memcmp(&d6->sin6_addr, pat, 12)) { if (ret4 && ret6) {
return AF_INET; uniperror("setttl");
}
}
#endif
return dst->sa_family;
}
int setttl(int fd, int ttl, int family) {
int _ttl = 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");
return -1; return -1;
} }
return 0; return 0;
@ -143,7 +123,7 @@ void wait_send(int sfd)
#ifdef __linux__ #ifdef __linux__
ssize_t send_fake(int sfd, char *buffer, 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 = {}; struct sockaddr_in6 addr = {};
socklen_t addr_size = sizeof(addr); socklen_t addr_size = sizeof(addr);
@ -185,7 +165,7 @@ ssize_t send_fake(int sfd, char *buffer,
} }
memcpy(p, pkt.data, pkt.size < pos ? pkt.size : pos); 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; break;
} }
if (opt->md5sig) { if (opt->md5sig) {
@ -200,11 +180,11 @@ ssize_t send_fake(int sfd, char *buffer,
break; break;
} }
} }
if (opt->ip_options && fa == AF_INET if (opt->ip_options
&& setsockopt(sfd, IPPROTO_IP, IP_OPTIONS, && setsockopt(sfd, IPPROTO_IP, IP_OPTIONS,
opt->ip_options, opt->ip_options_len) < 0) { opt->ip_options, opt->ip_options_len) < 0) {
uniperror("setsockopt IP_OPTIONS"); uniperror("setsockopt IP_OPTIONS");
break; //break;
} }
struct iovec vec = { .iov_base = p, .iov_len = pos }; struct iovec vec = { .iov_base = p, .iov_len = pos };
@ -221,14 +201,14 @@ ssize_t send_fake(int sfd, char *buffer,
wait_send(sfd); wait_send(sfd);
memcpy(p, buffer, pos); memcpy(p, buffer, pos);
if (setttl(sfd, params.def_ttl, fa) < 0) { if (setttl(sfd, params.def_ttl) < 0) {
break; break;
} }
if (opt->ip_options && fa == AF_INET if (opt->ip_options
&& setsockopt(sfd, IPPROTO_IP, && setsockopt(sfd, IPPROTO_IP,
IP_OPTIONS, opt->ip_options, 0) < 0) { IP_OPTIONS, opt->ip_options, 0) < 0) {
uniperror("setsockopt IP_OPTIONS"); uniperror("setsockopt IP_OPTIONS");
break; //break;
} }
if (opt->md5sig) { if (opt->md5sig) {
struct tcp_md5sig md5 = { struct tcp_md5sig md5 = {
@ -255,7 +235,7 @@ ssize_t send_fake(int sfd, char *buffer,
OVERLAPPED ov = {}; OVERLAPPED ov = {};
ssize_t send_fake(int sfd, char *buffer, 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; struct packet pkt;
if (opt->fake_data.data) { if (opt->fake_data.data) {
@ -318,7 +298,7 @@ ssize_t send_fake(int sfd, char *buffer,
uniperror("SetFilePointer"); uniperror("SetFilePointer");
break; break;
} }
if (setttl(sfd, opt->ttl ? opt->ttl : 8, fa) < 0) { if (setttl(sfd, opt->ttl ? opt->ttl : 8) < 0) {
break; break;
} }
if (!TransmitFile(sfd, hfile, pos, pos, &ov, if (!TransmitFile(sfd, hfile, pos, pos, &ov,
@ -339,7 +319,7 @@ ssize_t send_fake(int sfd, char *buffer,
uniperror("WriteFile"); uniperror("WriteFile");
break; break;
} }
if (setttl(sfd, params.def_ttl, fa) < 0) { if (setttl(sfd, params.def_ttl) < 0) {
break; break;
} }
len = pos; len = pos;
@ -378,11 +358,11 @@ ssize_t send_oob(int sfd, char *buffer,
ssize_t send_disorder(int sfd, ssize_t send_disorder(int sfd,
char *buffer, long pos, int fa) char *buffer, long pos)
{ {
int bttl = 1; int bttl = 1;
if (setttl(sfd, bttl, fa) < 0) { if (setttl(sfd, bttl) < 0) {
return -1; return -1;
} }
ssize_t len = send(sfd, buffer, pos, 0); ssize_t len = send(sfd, buffer, pos, 0);
@ -391,7 +371,7 @@ ssize_t send_disorder(int sfd,
} }
else wait_send_if_support(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 -1;
} }
return len; return len;
@ -399,18 +379,18 @@ ssize_t send_disorder(int sfd,
ssize_t send_late_oob(int sfd, char *buffer, 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; int bttl = 1;
if (setttl(sfd, bttl, fa) < 0) { if (setttl(sfd, bttl) < 0) {
return -1; return -1;
} }
ssize_t len = send_oob(sfd, buffer, n, pos, c); ssize_t len = send_oob(sfd, buffer, n, pos, c);
if (len < 0) { if (len < 0) {
uniperror("send"); uniperror("send");
} }
if (setttl(sfd, params.def_ttl, fa) < 0) { if (setttl(sfd, params.def_ttl) < 0) {
return -1; return -1;
} }
return len; return len;
@ -453,7 +433,6 @@ ssize_t desync(int sfd, char *buffer, size_t bfsize,
char *host = 0; char *host = 0;
int len = 0, type = 0, host_pos = 0; int len = 0, type = 0, host_pos = 0;
int fa = get_family(dst);
// parse packet // parse packet
if ((len = parse_tls(buffer, n, &host))) { if ((len = parse_tls(buffer, n, &host))) {
@ -530,12 +509,12 @@ ssize_t desync(int sfd, char *buffer, size_t bfsize,
#ifdef FAKE_SUPPORT #ifdef FAKE_SUPPORT
case DESYNC_FAKE: case DESYNC_FAKE:
if (pos != lp) s = send_fake(sfd, if (pos != lp) s = send_fake(sfd,
buffer + lp, type, pos - lp, fa, &dp); buffer + lp, type, pos - lp, &dp);
break; break;
#endif #endif
case DESYNC_DISORDER: case DESYNC_DISORDER:
s = send_disorder(sfd, s = send_disorder(sfd,
buffer + lp, pos - lp, fa); buffer + lp, pos - lp);
break; break;
case DESYNC_OOB: case DESYNC_OOB:
@ -545,7 +524,7 @@ ssize_t desync(int sfd, char *buffer, size_t bfsize,
case DESYNC_DISOOB: case DESYNC_DISOOB:
s = send_late_oob(sfd, 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; break;
case DESYNC_SPLIT: case DESYNC_SPLIT:
@ -607,7 +586,6 @@ ssize_t desync_udp(int sfd, char *buffer, size_t bfsize,
ssize_t n, struct sockaddr *dst, int dp_c) ssize_t n, struct sockaddr *dst, int dp_c)
{ {
struct desync_params *dp = &params.dp[dp_c]; struct desync_params *dp = &params.dp[dp_c];
int fa = get_family(dst);
if (dp->udp_fake_count != 0) { if (dp->udp_fake_count != 0) {
struct packet pkt; struct packet pkt;
@ -625,7 +603,7 @@ ssize_t desync_udp(int sfd, char *buffer, size_t bfsize,
else pkt.size = 0; else pkt.size = 0;
} }
int bttl = dp->ttl ? dp->ttl : 8; int bttl = dp->ttl ? dp->ttl : 8;
if (setttl(sfd, bttl, fa) < 0) { if (setttl(sfd, bttl) < 0) {
return -1; return -1;
} }
for (int i = 0; i < dp->udp_fake_count; i++) { for (int i = 0; i < dp->udp_fake_count; i++) {
@ -636,7 +614,7 @@ ssize_t desync_udp(int sfd, char *buffer, size_t bfsize,
return -1; return -1;
} }
} }
if (setttl(sfd, params.def_ttl, fa) < 0) { if (setttl(sfd, params.def_ttl) < 0) {
return -1; return -1;
} }
} }

View File

@ -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); 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 setttl(int fd, int ttl, int family);
int post_desync(int sfd, int dp_c); int post_desync(int sfd, int dp_c);

View File

@ -140,7 +140,7 @@ int connect_hook(struct poolhd *pool, struct eval *val,
int socket_mod(int fd, struct sockaddr *dst) int socket_mod(int fd, struct sockaddr *dst)
{ {
if (params.custom_ttl) { if (params.custom_ttl) {
if (setttl(fd, params.def_ttl, get_family(dst)) < 0) { if (setttl(fd, params.def_ttl) < 0) {
return -1; return -1;
} }
} }