Skip to content

Commit

Permalink
It buildsmake
Browse files Browse the repository at this point in the history
  • Loading branch information
gjedeer committed Mar 15, 2023
1 parent 450509a commit 8011c47
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 30 deletions.
51 changes: 29 additions & 22 deletions client.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,8 @@ int handle_acktunnel_frame(protocol_frame *rcvd_frame)
return -1;
}
LL_DELETE(pending_port_forwards, forward);
/* TODO MULTIPLE_L bind here? i dont think so, bind_sockfd should be already bound. to check. */
LL_APPEND(local_port_forwards, forward);

/* TODO MULTIPLE_L which sockfd? 0? -1? forward->bind_sockfd? see original impl. */
forward->tun = tunnel_create(
0, /* sockfd */
rcvd_frame->connid,
Expand All @@ -172,7 +170,7 @@ int handle_acktunnel_frame(protocol_frame *rcvd_frame)
FD_SET(forward->tun->sockfd, &client_master_fdset);
if(client_local_port_mode)
{
log_printf(L_INFO, "Accepted a new connection on port %d\n", local_port);
log_printf(L_INFO, "Accepted a new connection on port %d\n", forward->local_port);
}
}
else
Expand Down Expand Up @@ -524,42 +522,51 @@ int do_client_loop(uint8_t *tox_id_str)
state = CLIENT_STATE_WAIT_FOR_ACKTUNNEL;
break;
case CLIENT_STATE_WAIT_FOR_ACKTUNNEL:
client_tunnel.sockfd = 0;
/* TODO REMOTE_L */
send_tunnel_request_packet(
remote_host,
remote_port,
friendnumber
);
LL_FOREACH(local_port_forwards, port_forward)
{
port_forward->tun->sockfd = 0;
send_tunnel_request_packet(
port_forward->remote_host,
port_forward->remote_port,
port_forward->forward_id,
friendnumber
);
}
break;
case CLIENT_STATE_FORWARDING:
{
int accept_fd = 0;
int select_rv = 0;
tunnel *tmp = NULL;
tunnel *tun = NULL;
local_port_forward *port_forward;

tv.tv_sec = 0;
tv.tv_usec = 20000;
fds = client_master_fdset;

/* TODO MULTIPLE_L loop over tunnels and sockfds */
/* Handle accepting new connections */
if(!client_pipe_mode &&
client_tunnel.sockfd <= 0) /* Don't accept if we're already waiting to establish a tunnel */
LL_FOREACH(local_port_forwards, port_forward)
{
accept_fd = accept(bind_sockfd, NULL, NULL);
if(accept_fd != -1)
if(!client_pipe_mode &&
port_forward->tun->sockfd <= 0) /* Don't accept if we're already waiting to establish a tunnel */
{
log_printf(L_INFO, "Accepting a new connection - requesting tunnel...\n");

/* Open a new tunnel for this FD */
client_tunnel.sockfd = accept_fd;
send_tunnel_request_packet(
remote_host,
remote_port,
friendnumber
);
accept_fd = accept(port_forward->bind_sockfd, NULL, NULL);
if(accept_fd != -1)
{
log_printf(L_INFO, "Accepting a new connection - requesting tunnel...\n");

/* Open a new tunnel for this FD */
port_forward->tun->sockfd = accept_fd;
send_tunnel_request_packet(
port_forward->remote_host,
port_forward->remote_port,
port_forward->forward_id,
friendnumber
);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion gitversion.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define GITVERSION "71f125f17cd36582e0dc98e6bc9aca560e445c5a"
#define GITVERSION "450509a24e93e855753f9c51dd04dde7e4ad7e3a"
32 changes: 26 additions & 6 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,24 @@ local_port_forward *local_port_forward_create()
}

forward->forward_id = ++last_forward_id;
forward->created = time();
forward->created = time(NULL);

return forward;
}

local_port_forward *find_pending_forward_by_id(uint32_t local_forward_id)
{
local_port_forward *forward;

LL_FOREACH(pending_port_forwards, forward)
{
if(forward->forward_id == local_forward_id)
{
return forward;
}
}

return NULL;
}

/* bootstrap to dht with bootstrap_nodes */
Expand Down Expand Up @@ -465,7 +482,7 @@ int handle_request_tunnel_frame(protocol_frame *rcvd_frame)
return -1;
}

remote_forward_id = UINT32_AT(rcvd_frame->data, 0);
remote_forward_id = INT32_AT(rcvd_frame->data, 0);

strncpy(hostname, ((char *)rcvd_frame->data) + 4, rcvd_frame->data_length - 4);
hostname[rcvd_frame->data_length] = '\0';
Expand Down Expand Up @@ -1296,6 +1313,7 @@ int main(int argc, char *argv[])
size_t save_size = 0;
uint8_t *save_data = NULL;
allowed_toxid *allowed_toxid_obj = NULL;
local_port_forward *port_forward = NULL;

srand(time(NULL));
tcp_relay_port = 1024 + (rand() % 64511);
Expand All @@ -1311,7 +1329,7 @@ int main(int argc, char *argv[])
switch(oc)
{
case 'L':
local_port_forward *port_forward = local_port_forward_create();
port_forward = local_port_forward_create();

if(!port_forward) {
log_printf(L_ERROR, "Could not allocate memory for port forward\n");
Expand All @@ -1335,13 +1353,14 @@ int main(int argc, char *argv[])
{
min_log_level = L_INFO;
}
log_printf(L_DEBUG, "Forwarding remote port %d to local port %d\n", remote_port, local_port);
log_printf(L_DEBUG, "Forwarding remote port %d to local port %d\n", port_forward->remote_port, port_forward->local_port);
break;
case 'W':
/* Pipe forwarding */
port_forward = local_port_forward_create();
client_mode = 1;
client_pipe_mode = 1;
if(parse_pipe_port_forward(optarg, &remote_host, &remote_port) < 0)
if(parse_pipe_port_forward(optarg, &(port_forward->remote_host), &(port_forward->remote_port)) < 0)
{
log_printf(L_ERROR, "Invalid value for -W option - use something like -W 127.0.0.1:22\n");
exit(1);
Expand All @@ -1350,7 +1369,8 @@ int main(int argc, char *argv[])
{
min_log_level = L_ERROR;
}
log_printf(L_INFO, "Forwarding remote port %d to stdin/out\n", remote_port);
LL_APPEND(pending_port_forwards, port_forward);
log_printf(L_INFO, "Forwarding remote port %d to stdin/out\n", port_forward->remote_port);
break;
case 'p':
/* Ping */
Expand Down
4 changes: 3 additions & 1 deletion main.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ typedef struct protocol_frame_t {
uint8_t *data;
} protocol_frame;

/* A list of local port forwards (listen locally, forward to server */
/* A list of local port forwards (listen locally, forward to server) */
typedef struct local_port_forward_t {
int local_port;
char *remote_host;
Expand Down Expand Up @@ -139,6 +139,8 @@ extern tunnel *by_id;
extern local_port_forward *local_port_forwards;
extern local_port_forward *pending_port_forwards;

local_port_forward *find_pending_forward_by_id(uint32_t local_forward_id);

void parse_lossless_packet(Tox *tox, uint32_t friendnumber, const uint8_t *data, size_t len, void *tmp);
tunnel *tunnel_create(int sockfd, int connid, uint32_t friendnumber);
void tunnel_delete(tunnel *t);
Expand Down

0 comments on commit 8011c47

Please sign in to comment.