Skip to content

Commit

Permalink
NATMap: Bind network interface for port forwarder.
Browse files Browse the repository at this point in the history
  • Loading branch information
heiher committed Dec 27, 2024
1 parent e34c259 commit 5f15bdc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/hev-sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ hev_sock_client_pfwd (int type, const char *addr, const char *port)
}

int
hev_sock_server_pfwd (int fd, int type)
hev_sock_server_pfwd (int fd, int type, const char *iface, unsigned int mark)
{
struct addrinfo ai;
struct sockaddr_storage addr;
Expand All @@ -385,6 +385,10 @@ hev_sock_server_pfwd (int fd, int type)
}

res |= bind (fd, (struct sockaddr *)&addr, addrlen);
res |= bind_iface (fd, addr.ss_family, iface);
if (mark) {
res |= bind_fwmark (fd, mark);
}
if (type == SOCK_STREAM) {
res |= listen (fd, 5);
}
Expand Down
5 changes: 4 additions & 1 deletion src/hev-sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,14 @@ int hev_sock_client_pfwd (int type, const char *addr, const char *port);
* hev_sock_server_pfwd:
* @fd: http socket file descriptor
* @type: socket type
* @iface: network interface
* @mark: fwmark
*
* Create a socket for port forwarding server.
*
* Returns: returns file descriptor on successful, otherwise returns -1.
*/
int hev_sock_server_pfwd (int fd, int type);
int hev_sock_server_pfwd (int fd, int type, const char *iface,
unsigned int mark);

#endif /* __HEV_SOCK_H__ */
6 changes: 5 additions & 1 deletion src/hev-tfwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,14 @@ static void
server_task_entry (void *data)
{
int fd = (intptr_t)data;
const char *iface;
int mode;
int mark;

mode = hev_conf_mode ();
fd = hev_sock_server_pfwd (fd, mode);
mark = hev_conf_mark ();
iface = hev_conf_iface ();
fd = hev_sock_server_pfwd (fd, mode, iface, mark);
if (fd < 0) {
LOGV (E, "%s", "Start TCP forward service failed.");
hev_xnsk_kill ();
Expand Down
6 changes: 5 additions & 1 deletion src/hev-ufwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,17 @@ client_task_entry (void *data)
static void
server_task_entry (void *data)
{
const char *iface;
int mode;
int mark;
int tfd;

tfd = (intptr_t)data;
mode = hev_conf_mode ();
mark = hev_conf_mark ();
iface = hev_conf_iface ();

sfd = hev_sock_server_pfwd (tfd, mode);
sfd = hev_sock_server_pfwd (tfd, mode, iface, mark);
close (tfd);
if (sfd < 0) {
LOGV (E, "%s", "Start UDP forward service failed.");
Expand Down

0 comments on commit 5f15bdc

Please sign in to comment.