tpws: save some memory by using sockaddr_in64

This commit is contained in:
bol-van 2024-10-14 22:32:50 +03:00
parent 01b3f4d3cf
commit ea97e9ade2
6 changed files with 31 additions and 10 deletions

View File

@ -183,6 +183,10 @@ void sacopy(struct sockaddr_storage *sa_dest, const struct sockaddr *sa)
sa_dest->ss_family = 0;
}
}
void sa46copy(sockaddr_in46 *sa_dest, const struct sockaddr *sa)
{
sacopy((struct sockaddr_storage*)sa_dest, sa);
}
bool is_localnet(const struct sockaddr *a)
{

View File

@ -8,6 +8,14 @@
#include <stdio.h>
#include <time.h>
// this saves memory. sockaddr_storage is larger than required. it can be 128 bytes. sockaddr_in6 is 28 bytes.
typedef union
{
struct sockaddr_in sa4; // size 16
struct sockaddr_in6 sa6; // size 28
char _align[32]; // force 16-byte alignment for ip6_and int128 ops
} sockaddr_in46;
void rtrim(char *s);
char *strncasestr(const char *s,const char *find, size_t slen);
@ -27,6 +35,7 @@ uint16_t saport(const struct sockaddr *sa);
bool saconvmapped(struct sockaddr_storage *a);
void sacopy(struct sockaddr_storage *sa_dest, const struct sockaddr *sa);
void sa46copy(sockaddr_in46 *sa_dest, const struct sockaddr *sa);
bool is_localnet(const struct sockaddr *a);
bool is_linklocal(const struct sockaddr_in6* a);

View File

@ -103,6 +103,12 @@ static void *resolver_thread(void *arg)
ri->ga_res = getaddrinfo(ri->dom,sport,&hints,&ai);
if (!ri->ga_res)
{
if (ai->ai_addrlen>sizeof(ri->ss))
{
DLOG_ERR("getaddrinfo returned too large address\n");
ri->ga_res = EAI_FAIL;
}
else
memcpy(&ri->ss, ai->ai_addr, ai->ai_addrlen);
freeaddrinfo(ai);
}

View File

@ -6,10 +6,12 @@
#include <sys/socket.h>
#include <netdb.h>
#include "helpers.h"
struct resolve_item
{
char dom[256]; // request dom
struct sockaddr_storage ss; // resolve result
sockaddr_in46 ss; // resolve result
int ga_res; // getaddrinfo result code
uint16_t port; // request port
void *ptr;

View File

@ -619,7 +619,7 @@ static tproxy_conn_t* add_tcp_connection(int efd, struct tailhead *conn_list,int
if (proxy_type==CONN_TYPE_TRANSPARENT)
{
sacopy(&conn->dest, (struct sockaddr *)&orig_dst);
sa46copy(&conn->dest, (struct sockaddr *)&orig_dst);
if(!(conn->partner = new_conn(remote_fd, true)))
{
@ -693,7 +693,7 @@ static bool check_connection_attempt(tproxy_conn_t *conn, int efd)
{
if (params.debug>=1)
{
struct sockaddr_storage sa;
sockaddr_in46 sa;
socklen_t salen=sizeof(sa);
char ip_port[48];
@ -920,7 +920,7 @@ static bool handle_proxy_mode(tproxy_conn_t *conn, struct tailhead *conn_list)
socks4_send_rep(conn->fd, S4_REP_FAILED);
return false;
}
conn->dest.ss_family = AF_INET;
((struct sockaddr_in*)&conn->dest)->sin_family = AF_INET;
((struct sockaddr_in*)&conn->dest)->sin_port = m->port;
((struct sockaddr_in*)&conn->dest)->sin_addr.s_addr = m->ip;
return proxy_mode_connect_remote(conn, conn_list);
@ -952,12 +952,12 @@ static bool handle_proxy_mode(tproxy_conn_t *conn, struct tailhead *conn_list)
switch(m->atyp)
{
case S5_ATYP_IP4:
conn->dest.ss_family = AF_INET;
((struct sockaddr_in*)&conn->dest)->sin_family = AF_INET;
((struct sockaddr_in*)&conn->dest)->sin_port = m->d4.port;
((struct sockaddr_in*)&conn->dest)->sin_addr = m->d4.addr;
break;
case S5_ATYP_IP6:
conn->dest.ss_family = AF_INET6;
((struct sockaddr_in6*)&conn->dest)->sin6_family = AF_INET6;
((struct sockaddr_in6*)&conn->dest)->sin6_port = m->d6.port;
((struct sockaddr_in6*)&conn->dest)->sin6_addr = m->d6.addr;
((struct sockaddr_in6*)&conn->dest)->sin6_flowinfo = 0;
@ -1037,7 +1037,7 @@ static bool resolve_complete(struct resolve_item *ri, struct tailhead *conn_list
DBGPRINT("resolve_complete put hostname : %s\n", ri->dom);
conn->track.hostname = strdup(ri->dom);
}
sacopy(&conn->dest, (struct sockaddr *)&ri->ss);
sa46copy(&conn->dest, (struct sockaddr *)&ri->ss);
return proxy_mode_connect_remote(conn,conn_list);
}
}

View File

@ -54,7 +54,7 @@ struct tproxy_conn
int splice_pipe[2];
conn_state_t state;
conn_type_t conn_type;
struct sockaddr_storage dest;
sockaddr_in46 src, dest;
struct tproxy_conn *partner; // other leg
time_t orphan_since;