Skip to content

Commit

Permalink
NATMap: Delete file descriptors upon task exit.
Browse files Browse the repository at this point in the history
  • Loading branch information
heiher committed Feb 13, 2025
1 parent 1e2dcc8 commit 44d8db7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/hev-sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ hev_sock_client_tcp (int family, const char *saddr, const char *sport,
const char *daddr, const char *dport, const char *iface,
unsigned int mark)
{
HevTask *task = hev_task_self ();
struct addrinfo *sai;
struct addrinfo *dai;
int timeout = 30000;
Expand Down Expand Up @@ -173,7 +174,7 @@ hev_sock_client_tcp (int family, const char *saddr, const char *sport,
}
freeaddrinfo (sai);

hev_task_add_fd (hev_task_self (), fd, POLLIN | POLLOUT);
hev_task_add_fd (task, fd, POLLIN | POLLOUT);

res = hev_task_io_socket_connect (fd, dai->ai_addr, dai->ai_addrlen,
io_yielder, &timeout);
Expand All @@ -187,6 +188,7 @@ hev_sock_client_tcp (int family, const char *saddr, const char *sport,
} else {
LOGV (E, "%s", strerror (errno));
}
hev_task_del_fd (task, fd);
close (fd);
return -1;
}
Expand Down Expand Up @@ -247,6 +249,7 @@ hev_sock_client_stun (int fd, int type, const char *daddr, const char *dport,
const char *iface, unsigned int mark,
unsigned int baddr[4], int *bport)
{
HevTask *task = hev_task_self ();
struct addrinfo sai;
struct addrinfo *dai;
struct sockaddr_storage saddr;
Expand Down Expand Up @@ -295,20 +298,22 @@ hev_sock_client_stun (int fd, int type, const char *daddr, const char *dport,
return -1;
}

hev_task_add_fd (hev_task_self (), fd, POLLIN | POLLOUT);
hev_task_add_fd (task, fd, POLLIN | POLLOUT);

res = hev_task_io_socket_connect (fd, dai->ai_addr, dai->ai_addrlen,
io_yielder, &timeout);
freeaddrinfo (dai);
if (res < 0) {
LOG (E);
hev_task_del_fd (task, fd);
close (fd);
return -1;
}

res = getsockname (fd, (struct sockaddr *)&saddr, &saddrlen);
if (res < 0) {
LOG (E);
hev_task_del_fd (task, fd);
close (fd);
return -1;
}
Expand All @@ -329,6 +334,7 @@ hev_sock_client_stun (int fd, int type, const char *daddr, const char *dport,
int
hev_sock_client_pfwd (int type, const char *addr, const char *port)
{
HevTask *task = hev_task_self ();
struct addrinfo *ai;
int timeout = 30000;
int res;
Expand All @@ -347,13 +353,14 @@ hev_sock_client_pfwd (int type, const char *addr, const char *port)
return -1;
}

hev_task_add_fd (hev_task_self (), fd, POLLIN | POLLOUT);
hev_task_add_fd (task, fd, POLLIN | POLLOUT);

res = hev_task_io_socket_connect (fd, ai->ai_addr, ai->ai_addrlen,
io_yielder, &timeout);
freeaddrinfo (ai);
if (res < 0) {
LOG (E);
hev_task_del_fd (task, fd);
close (fd);
return -1;
}
Expand Down
1 change: 1 addition & 0 deletions src/hev-stun.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ task_entry (void *data)
}
}

hev_task_del_fd (hev_task_self (), fd);
close (fd);
task = NULL;
}
Expand Down
5 changes: 4 additions & 1 deletion src/hev-tfwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ yielder (HevTaskYieldType type, void *data)
static void
client_task_entry (void *data)
{
HevTask *task = hev_task_self ();
const char *addr;
const char *port;
int timeout;
Expand All @@ -59,9 +60,11 @@ client_task_entry (void *data)
return;
}

hev_task_add_fd (hev_task_self (), sfd, POLLIN | POLLOUT);
hev_task_add_fd (task, sfd, POLLIN | POLLOUT);
hev_task_io_splice (sfd, sfd, dfd, dfd, 8192, io_yielder, &timeout);

hev_task_del_fd (task, sfd);
hev_task_del_fd (task, dfd);
close (sfd);
close (dfd);
}
Expand Down
6 changes: 5 additions & 1 deletion src/hev-ufwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ session_new (struct sockaddr *saddr, socklen_t len)
s = hev_malloc0 (sizeof (Session));
if (!s) {
LOG (E);
hev_task_del_fd (hev_task_self (), fd);
close (fd);
return NULL;
}
Expand All @@ -150,14 +151,15 @@ session_free (Session *s)
static void
client_task_entry (void *data)
{
HevTask *task = hev_task_self ();
const int bufsize = 2048;
struct sockaddr *pa;
char buf[bufsize];
Session *s = data;
int timeout;

pa = (struct sockaddr *)&s->addr;
hev_task_add_fd (hev_task_self (), s->fd, POLLIN);
hev_task_add_fd (task, s->fd, POLLIN);
timeout = hev_conf_tmsec ();

for (;;) {
Expand All @@ -179,6 +181,7 @@ client_task_entry (void *data)
}
}

hev_task_del_fd (task, s->fd);
session_del (s);
session_free (s);
}
Expand Down Expand Up @@ -238,6 +241,7 @@ server_task_entry (void *data)
s->active = 1;
}

hev_task_del_fd (hev_task_self (), sfd);
close (sfd);
task = NULL;
sfd = -1;
Expand Down

0 comments on commit 44d8db7

Please sign in to comment.