Skip to content

Commit

Permalink
Merge branch 'master' of github.com:gjedeer/tuntox
Browse files Browse the repository at this point in the history
  • Loading branch information
gjedeer committed Nov 1, 2017
2 parents 8abb89f + 4f509dc commit 034d079
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 80 deletions.
2 changes: 1 addition & 1 deletion BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ One reason to do so may be if you'd like to resolve hostnames on the tuntox serv

Static linking breaks hostname resolution, but IMHO the pros overweight the cons.

c-toxcore is the only direct dependency. c-toxcore requires libsodium and libevent_pthreads at the time of writing this, please refer to their install instructions for the current dependencies.
c-toxcore is the only direct dependency. c-toxcore requires libsodium and libevent_pthreads at the time of writing this, please refer to their install instructions for the current dependencies. Also pkg-config is required.

## MacOS build
Basically the same as above but:
Expand Down
49 changes: 40 additions & 9 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ fd_set master_server_fds;
/* We keep two hash tables: one indexed by sockfd and another by "connection id" */
tunnel *by_id = NULL;

/* Tunnels need to be delete safely, outside FD_ISSET polling */
/* See: tunnel_queue_delete() */
tunnel_list *tunnels_to_delete = NULL;

/* Highest used fd + 1 for select() */
int select_nfds = 4;

Expand Down Expand Up @@ -144,9 +148,10 @@ tunnel *tunnel_create(int sockfd, int connid, uint32_t friendnumber)
return t;
}

/* Please use tunnel_queue_delete() instead */
void tunnel_delete(tunnel *t)
{
log_printf(L_INFO, "Deleting tunnel #%d\n", t->connid);
log_printf(L_INFO, "Deleting tunnel #%d ptr %p\n", t->connid, t);
if(t->sockfd)
{
close(t->sockfd);
Expand All @@ -156,6 +161,17 @@ void tunnel_delete(tunnel *t)
free(t);
}

void tunnel_queue_delete(tunnel *t)
{
tunnel_list *tunnel_list_entry = NULL;

log_printf(L_DEBUG2, "Queued deleting tunnel #%d ptr %p\n", t->connid, t);

tunnel_list_entry = calloc(sizeof(tunnel_list), 1);
tunnel_list_entry->tun = t;
LL_APPEND(tunnels_to_delete, tunnel_list_entry);
}

/* bootstrap to dht with bootstrap_nodes */
/* From uTox/tox.c */
static void do_bootstrap(Tox *tox)
Expand Down Expand Up @@ -342,7 +358,7 @@ int send_frame(protocol_frame *frame, uint8_t *data)

if(i > 0 && rv >= 0)
{
log_printf(L_DEBUG, "Packet succeeded at try %d\n", try);
log_printf(L_DEBUG, "Packet succeeded at try %d (friend %d tunnel %d)\n", try, frame->friendnumber, frame->connid);
}

return rv;
Expand Down Expand Up @@ -533,7 +549,8 @@ int handle_client_tcp_fin_frame(protocol_frame *rcvd_frame)
return -1;
}

tunnel_delete(tun);
log_printf(L_DEBUG2, "Deleting tunnel #%d (%p) in handle_client_tcp_fin_frame(), socket %d", rcvd_frame->connid, tun, tun->sockfd);
tunnel_queue_delete(tun);

return 0;
}
Expand Down Expand Up @@ -920,8 +937,8 @@ int do_server_loop()
int select_rv = 0;
sent_data = 0;

/* Let tox do its stuff */
tox_iterate(tox, NULL);
/* Let tox do its stuff */
tox_iterate(tox, NULL);

/* Get the desired sleep time, used in select() later */
tox_do_interval_ms = tox_iteration_interval(tox);
Expand All @@ -947,8 +964,8 @@ int do_server_loop()

fds = master_server_fds;

/* Poll for data from our client connection */
select_rv = select(select_nfds, &fds, NULL, NULL, &tv);
/* Poll for data from our client connection */
select_rv = select(select_nfds, &fds, NULL, NULL, &tv);
if(select_rv == -1 || select_rv == 0)
{
if(select_rv == -1)
Expand All @@ -963,8 +980,14 @@ int do_server_loop()
}
else
{
tunnel_list *tunnel_list_entry = NULL, *list_tmp = NULL;
tmp = NULL;
tun = NULL;

log_printf(L_DEBUG, "Starting tunnel iteration...");
HASH_ITER(hh, by_id, tun, tmp)
{
log_printf(L_DEBUG, "Current tunnel: %p", tun);
if(FD_ISSET(tun->sockfd, &fds))
{
int nbytes = recv(tun->sockfd,
Expand Down Expand Up @@ -996,7 +1019,7 @@ int do_server_loop()
send_frame(frame, data);
sent_data = 1;

tunnel_delete(tun);
tunnel_queue_delete(tun);

continue;
}
Expand All @@ -1015,6 +1038,14 @@ int do_server_loop()
}
}
}
log_printf(L_DEBUG, "Tunnel iteration done");

LL_FOREACH_SAFE(tunnels_to_delete, tunnel_list_entry, list_tmp)
{
tunnel_delete(tunnel_list_entry->tun);
LL_DELETE(tunnels_to_delete, tunnel_list_entry);
free(tunnel_list_entry);
}
}

gettimeofday(&tv_end, NULL);
Expand Down Expand Up @@ -1218,7 +1249,7 @@ int main(int argc, char *argv[])

log_init();

while ((oc = getopt(argc, argv, "L:pi:C:s:f:P:dqhSF:DU:t:u:")) != -1)
while ((oc = getopt(argc, argv, "L:pi:C:s:f:W:dqhSF:DU:t:u:")) != -1)
{
switch(oc)
{
Expand Down
5 changes: 5 additions & 0 deletions main.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ typedef struct tunnel_t {
UT_hash_handle hh;
} tunnel;

typedef struct tunnel_list_t {
tunnel *tun;
struct tunnel_list_t *next;
} tunnel_list;

typedef struct allowed_toxid {
uint8_t toxid[TOX_ADDRESS_SIZE];
struct allowed_toxid *next;
Expand Down
Loading

0 comments on commit 034d079

Please sign in to comment.