Skip to content

Commit

Permalink
pass link_socket object to i/o functions
Browse files Browse the repository at this point in the history
In order to prepare the code to work with distinct sockets,
it is essential that i/o functions do not operate on any
hard-coded socket object (i.e. c->c2.link_socket).

This patch changes all the low-level i/o functionis to work
with a socket specified as argument rather than a fixed one.

Change-Id: I8eae2d3356bbcc5d632eeb4fbe80de8009d9b40d
Signed-off-by: Antonio Quartulli <[email protected]>
Signed-off-by: Gianmarco De Gregori <[email protected]>
Acked-by: Frank Lichtenheld <[email protected]>
Message-Id: <[email protected]>
URL: https://www.mail-archive.com/[email protected]/msg29603.html
Signed-off-by: Gert Doering <[email protected]>
  • Loading branch information
ordex authored and cron2 committed Oct 23, 2024
1 parent 3636da3 commit 490d132
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 68 deletions.
1 change: 1 addition & 0 deletions src/openvpn/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ struct event_arg
event_arg_t type;
union {
struct multi_instance *mi; /* if type = EVENT_ARG_MULTI_INSTANCE */
struct link_socket *sock; /* if type = EVENT_ARG_LINK_SOCKET */
} u;
};

Expand Down
51 changes: 27 additions & 24 deletions src/openvpn/forward.c
Original file line number Diff line number Diff line change
Expand Up @@ -884,23 +884,24 @@ check_timeout_random_component(struct context *c)
*/

static inline void
socks_postprocess_incoming_link(struct context *c)
socks_postprocess_incoming_link(struct context *c, struct link_socket *sock)
{
if (c->c2.link_socket->socks_proxy && c->c2.link_socket->info.proto == PROTO_UDP)
if (sock->socks_proxy && sock->info.proto == PROTO_UDP)
{
socks_process_incoming_udp(&c->c2.buf, &c->c2.from);
}
}

static inline void
socks_preprocess_outgoing_link(struct context *c,
struct link_socket *sock,
struct link_socket_actual **to_addr,
int *size_delta)
{
if (c->c2.link_socket->socks_proxy && c->c2.link_socket->info.proto == PROTO_UDP)
if (sock->socks_proxy && sock->info.proto == PROTO_UDP)
{
*size_delta += socks_process_outgoing_udp(&c->c2.to_link, c->c2.to_link_addr);
*to_addr = &c->c2.link_socket->socks_relay;
*to_addr = &sock->socks_relay;
}
}

Expand All @@ -925,7 +926,7 @@ link_socket_write_post_size_adjust(int *size,
*/

void
read_incoming_link(struct context *c)
read_incoming_link(struct context *c, struct link_socket *sock)
{
/*
* Set up for recvfrom call to read datagram
Expand All @@ -940,17 +941,17 @@ read_incoming_link(struct context *c)
c->c2.buf = c->c2.buffers->read_link_buf;
ASSERT(buf_init(&c->c2.buf, c->c2.frame.buf.headroom));

status = link_socket_read(c->c2.link_socket,
status = link_socket_read(sock,
&c->c2.buf,
&c->c2.from);

if (socket_connection_reset(c->c2.link_socket, status))
if (socket_connection_reset(sock, status))
{
#if PORT_SHARE
if (port_share && socket_foreign_protocol_detected(c->c2.link_socket))
if (port_share && socket_foreign_protocol_detected(sock))
{
const struct buffer *fbuf = socket_foreign_protocol_head(c->c2.link_socket);
const int sd = socket_foreign_protocol_sd(c->c2.link_socket);
const struct buffer *fbuf = socket_foreign_protocol_head(sock);
const int sd = socket_foreign_protocol_sd(sock);
port_share_redirect(port_share, fbuf, sd);
register_signal(c->sig, SIGTERM, "port-share-redirect");
}
Expand All @@ -977,15 +978,15 @@ read_incoming_link(struct context *c)
bool dco_win_timeout = tuntap_is_dco_win_timeout(c->c1.tuntap, status);

/* check recvfrom status */
check_status(status, "read", c->c2.link_socket, NULL);
check_status(status, "read", sock, NULL);

if (dco_win_timeout)
{
trigger_ping_timeout_signal(c);
}

/* Remove socks header if applicable */
socks_postprocess_incoming_link(c);
socks_postprocess_incoming_link(c, sock);

perf_pop();
}
Expand Down Expand Up @@ -1222,11 +1223,11 @@ process_incoming_link_part2(struct context *c, struct link_socket_info *lsi, con
}

static void
process_incoming_link(struct context *c)
process_incoming_link(struct context *c, struct link_socket *sock)
{
perf_push(PERF_PROC_IN_LINK);

struct link_socket_info *lsi = get_link_socket_info(c);
struct link_socket_info *lsi = &sock->info;
const uint8_t *orig_buf = c->c2.buf.data;

process_incoming_link_part1(c, lsi, false);
Expand Down Expand Up @@ -1732,7 +1733,7 @@ process_ip_header(struct context *c, unsigned int flags, struct buffer *buf)
*/

void
process_outgoing_link(struct context *c)
process_outgoing_link(struct context *c, struct link_socket *sock)
{
struct gc_arena gc = gc_new();
int error_code = 0;
Expand Down Expand Up @@ -1775,7 +1776,7 @@ process_outgoing_link(struct context *c)

#if PASSTOS_CAPABILITY
/* Set TOS */
link_socket_set_tos(c->c2.link_socket);
link_socket_set_tos(sock);
#endif

/* Log packet send */
Expand All @@ -1786,7 +1787,7 @@ process_outgoing_link(struct context *c)
}
#endif
msg(D_LINK_RW, "%s WRITE [%d] to %s: %s",
proto2ascii(c->c2.link_socket->info.proto, c->c2.link_socket->info.af, true),
proto2ascii(sock->info.proto, sock->info.af, true),
BLEN(&c->c2.to_link),
print_link_socket_actual(c->c2.to_link_addr, &gc),
PROTO_DUMP(&c->c2.to_link, &gc));
Expand All @@ -1797,10 +1798,12 @@ process_outgoing_link(struct context *c)
int size_delta = 0;

/* If Socks5 over UDP, prepend header */
socks_preprocess_outgoing_link(c, &to_addr, &size_delta);
socks_preprocess_outgoing_link(c, sock, &to_addr, &size_delta);

/* Send packet */
size = (int)link_socket_write(c->c2.link_socket, &c->c2.to_link, to_addr);
size = (int)link_socket_write(sock,
&c->c2.to_link,
to_addr);

/* Undo effect of prepend */
link_socket_write_post_size_adjust(&size, size_delta, &c->c2.to_link);
Expand Down Expand Up @@ -1829,7 +1832,7 @@ process_outgoing_link(struct context *c)

/* Check return status */
error_code = openvpn_errno();
check_status(size, "write", c->c2.link_socket, NULL);
check_status(size, "write", sock, NULL);

if (size > 0)
{
Expand Down Expand Up @@ -2272,7 +2275,7 @@ io_wait_dowork(struct context *c, const unsigned int flags)
}

void
process_io(struct context *c)
process_io(struct context *c, struct link_socket *sock)
{
const unsigned int status = c->c2.event_set_status;

Expand All @@ -2287,7 +2290,7 @@ process_io(struct context *c)
/* TCP/UDP port ready to accept write */
if (status & SOCKET_WRITE)
{
process_outgoing_link(c);
process_outgoing_link(c, sock);
}
/* TUN device ready to accept write */
else if (status & TUN_WRITE)
Expand All @@ -2297,10 +2300,10 @@ process_io(struct context *c)
/* Incoming data on TCP/UDP port */
else if (status & SOCKET_READ)
{
read_incoming_link(c);
read_incoming_link(c, sock);
if (!IS_SIG(c))
{
process_incoming_link(c);
process_incoming_link(c, sock);
}
}
/* Incoming data on TUN device */
Expand Down
17 changes: 10 additions & 7 deletions src/openvpn/forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ void io_wait_dowork(struct context *c, const unsigned int flags);

void pre_select(struct context *c);

void process_io(struct context *c);
void process_io(struct context *c, struct link_socket *sock);


/**********************************************************************/
/**
Expand Down Expand Up @@ -128,10 +129,11 @@ int get_server_poll_remaining_time(struct event_timeout *server_poll_timeout);
* context associated with the appropriate VPN tunnel for which data is
* available to be read.
*
* @param c - The context structure which contains the external
* network socket from which to read incoming packets.
* @param c The context structure which contains the external
* network socket from which to read incoming packets.
* @param sock The socket where the packet can be read from.
*/
void read_incoming_link(struct context *c);
void read_incoming_link(struct context *c, struct link_socket *sock);

/**
* Starts processing a packet read from the external network interface.
Expand Down Expand Up @@ -197,10 +199,11 @@ void process_incoming_link_part2(struct context *c, struct link_socket_info *lsi
*
* If an error occurs, it is logged and the packet is dropped.
*
* @param c - The context structure of the VPN tunnel associated with the
* packet.
* @param c The context structure of the VPN tunnel associated with the
* packet.
* @param sock The socket to be used to send the packet.
*/
void process_outgoing_link(struct context *c);
void process_outgoing_link(struct context *c, struct link_socket *sock);


/**************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion src/openvpn/mtcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ multi_tcp_dispatch(struct multi_context *m, struct multi_instance *mi, const int
ASSERT(mi);
ASSERT(mi->context.c2.link_socket);
set_prefix(mi);
read_incoming_link(&mi->context);
read_incoming_link(&mi->context, mi->context.c2.link_socket);
clear_prefix();
if (!IS_SIG(&mi->context))
{
Expand Down
4 changes: 2 additions & 2 deletions src/openvpn/mudp.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ multi_process_outgoing_link(struct multi_context *m, const unsigned int mpp_flag
msg_set_prefix("Connection Attempt");
m->top.c2.to_link = m->hmac_reply;
m->top.c2.to_link_addr = m->hmac_reply_dest;
process_outgoing_link(&m->top);
process_outgoing_link(&m->top, m->top.c2.link_socket);
m->hmac_reply_dest = NULL;
}
}
Expand Down Expand Up @@ -380,7 +380,7 @@ multi_process_io_udp(struct multi_context *m)
/* Incoming data on UDP port */
else if (status & SOCKET_READ)
{
read_incoming_link(&m->top);
read_incoming_link(&m->top, m->top.c2.link_socket);
if (!IS_SIG(&m->top))
{
multi_process_incoming_link(m, NULL, mpp_flags);
Expand Down
2 changes: 1 addition & 1 deletion src/openvpn/multi.h
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ multi_process_outgoing_link_dowork(struct multi_context *m, struct multi_instanc
{
bool ret = true;
set_prefix(mi);
process_outgoing_link(&mi->context);
process_outgoing_link(&mi->context, mi->context.c2.link_socket);
ret = multi_process_post(m, mi, mpp_flags);
clear_prefix();
return ret;
Expand Down
2 changes: 1 addition & 1 deletion src/openvpn/openvpn.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ tunnel_point_to_point(struct context *c)
}

/* process the I/O which triggered select */
process_io(c);
process_io(c, c->c2.link_socket);
P2P_CHECK_SIG();

perf_pop();
Expand Down
19 changes: 10 additions & 9 deletions src/openvpn/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -969,12 +969,12 @@ socket_set_flags(socket_descriptor_t sd, unsigned int sockflags)
}

bool
link_socket_update_flags(struct link_socket *ls, unsigned int sockflags)
link_socket_update_flags(struct link_socket *sock, unsigned int sockflags)
{
if (ls && socket_defined(ls->sd))
if (sock && socket_defined(sock->sd))
{
ls->sockflags |= sockflags;
return socket_set_flags(ls->sd, ls->sockflags);
sock->sockflags |= sockflags;
return socket_set_flags(sock->sd, sock->sockflags);
}
else
{
Expand All @@ -983,13 +983,13 @@ link_socket_update_flags(struct link_socket *ls, unsigned int sockflags)
}

void
link_socket_update_buffer_sizes(struct link_socket *ls, int rcvbuf, int sndbuf)
link_socket_update_buffer_sizes(struct link_socket *sock, int rcvbuf, int sndbuf)
{
if (ls && socket_defined(ls->sd))
if (sock && socket_defined(sock->sd))
{
ls->socket_buffer_sizes.sndbuf = sndbuf;
ls->socket_buffer_sizes.rcvbuf = rcvbuf;
socket_set_buffers(ls->sd, &ls->socket_buffer_sizes, true);
sock->socket_buffer_sizes.sndbuf = sndbuf;
sock->socket_buffer_sizes.rcvbuf = rcvbuf;
socket_set_buffers(sock->sd, &sock->socket_buffer_sizes, true);
}
}

Expand Down Expand Up @@ -1831,6 +1831,7 @@ link_socket_new(void)
sock->sd = SOCKET_UNDEFINED;
sock->ctrl_sd = SOCKET_UNDEFINED;
sock->ev_arg.type = EVENT_ARG_LINK_SOCKET;
sock->ev_arg.u.sock = sock;

return sock;
}
Expand Down
Loading

0 comments on commit 490d132

Please sign in to comment.