Skip to content

Commit

Permalink
Fix UDP relay when plugin enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
madeye committed Jan 14, 2017
1 parent a0c6adb commit 0acc79b
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 44 deletions.
2 changes: 0 additions & 2 deletions src/jconf.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ read_jconf(const char *file)
char *buf;
json_value *obj;

LOGI("test");

FILE *f = fopen(file, "rb");
if (f == NULL) {
FATAL("Invalid config path.");
Expand Down
22 changes: 16 additions & 6 deletions src/local.c
Original file line number Diff line number Diff line change
Expand Up @@ -1475,10 +1475,6 @@ main(int argc, char **argv)
if (err) {
FATAL("failed to start the plugin");
}

remote_num = 1;
remote_addr[0].host = plugin_host;
remote_addr[0].port = plugin_port;
}

#ifdef __MINGW32__
Expand All @@ -1502,12 +1498,18 @@ main(int argc, char **argv)
char *host = remote_addr[i].host;
char *port = remote_addr[i].port == NULL ? remote_port :
remote_addr[i].port;
if (plugin != NULL) {
host = plugin_host;
port = plugin_port;
}
struct sockaddr_storage *storage = ss_malloc(sizeof(struct sockaddr_storage));
memset(storage, 0, sizeof(struct sockaddr_storage));
if (get_sockaddr(host, port, storage, 1, ipv6first) == -1) {
FATAL("failed to resolve the provided hostname");
}
listen_ctx.remote_addr[i] = (struct sockaddr *)storage;

if (plugin != NULL) break;
}
listen_ctx.timeout = atoi(timeout);
listen_ctx.iface = iface;
Expand Down Expand Up @@ -1552,8 +1554,16 @@ main(int argc, char **argv)
// Setup UDP
if (mode != TCP_ONLY) {
LOGI("udprelay enabled");
init_udprelay(local_addr, local_port, listen_ctx.remote_addr[0],
get_sockaddr_len(listen_ctx.remote_addr[0]), mtu, m, auth, listen_ctx.timeout, iface);
char *host = remote_addr[0].host;
char *port = remote_addr[0].port == NULL ? remote_port : remote_addr[0].port;
struct sockaddr_storage *storage = ss_malloc(sizeof(struct sockaddr_storage));
memset(storage, 0, sizeof(struct sockaddr_storage));
if (get_sockaddr(host, port, storage, 1, ipv6first) == -1) {
FATAL("failed to resolve the provided hostname");
}
struct sockaddr *addr = (struct sockaddr *)storage;
init_udprelay(local_addr, local_port, addr,
get_sockaddr_len(addr), mtu, m, auth, listen_ctx.timeout, iface);
}

#ifdef HAVE_LAUNCHD
Expand Down
4 changes: 2 additions & 2 deletions src/manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ construct_command_line(struct manager_ctx *manager, struct server *server)
}
if (manager->plugin_opts) {
int len = strlen(cmd);
snprintf(cmd + len, BUF_SIZE - len, " --plugin_opts \"%s\"", manager->plugin_opts);
snprintf(cmd + len, BUF_SIZE - len, " --plugin-opts \"%s\"", manager->plugin_opts);
}
for (i = 0; i < manager->nameserver_num; i++) {
int len = strlen(cmd);
Expand Down Expand Up @@ -912,7 +912,7 @@ main(int argc, char **argv)
{ "executable", required_argument, 0, 0 },
{ "mtu", required_argument, 0, 0 },
{ "plugin", required_argument, 0, 0 },
{ "plugin_opts", required_argument, 0, 0 },
{ "plugin-opts", required_argument, 0, 0 },
{ "help", no_argument, 0, 0 },
{ 0, 0, 0, 0 }
};
Expand Down
24 changes: 17 additions & 7 deletions src/redir.c
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ main(int argc, char **argv)
{ "mtu", required_argument, 0, 0 },
{ "mptcp", no_argument, 0, 0 },
{ "plugin", required_argument, 0, 0 },
{ "plugin_opts", required_argument, 0, 0 },
{ "plugin-opts", required_argument, 0, 0 },
{ "help", no_argument, 0, 0 },
{ 0, 0, 0, 0 }
};
Expand Down Expand Up @@ -1033,10 +1033,6 @@ main(int argc, char **argv)
if (err) {
FATAL("failed to start the plugin");
}

remote_num = 1;
remote_addr[0].host = plugin_host;
remote_addr[0].port = plugin_port;
}

// ignore SIGPIPE
Expand All @@ -1063,12 +1059,18 @@ main(int argc, char **argv)
char *host = remote_addr[i].host;
char *port = remote_addr[i].port == NULL ? remote_port :
remote_addr[i].port;
if (plugin != NULL) {
host = plugin_host;
port = plugin_port;
}
struct sockaddr_storage *storage = ss_malloc(sizeof(struct sockaddr_storage));
memset(storage, 0, sizeof(struct sockaddr_storage));
if (get_sockaddr(host, port, storage, 1, ipv6first) == -1) {
FATAL("failed to resolve the provided hostname");
}
listen_ctx.remote_addr[i] = (struct sockaddr *)storage;

if (plugin != NULL) break;
}
listen_ctx.timeout = atoi(timeout);
listen_ctx.method = m;
Expand Down Expand Up @@ -1097,8 +1099,16 @@ main(int argc, char **argv)
// Setup UDP
if (mode != TCP_ONLY) {
LOGI("UDP relay enabled");
init_udprelay(local_addr, local_port, listen_ctx.remote_addr[0],
get_sockaddr_len(listen_ctx.remote_addr[0]), mtu, m, auth, listen_ctx.timeout, NULL);
char *host = remote_addr[0].host;
char *port = remote_addr[0].port == NULL ? remote_port : remote_addr[0].port;
struct sockaddr_storage *storage = ss_malloc(sizeof(struct sockaddr_storage));
memset(storage, 0, sizeof(struct sockaddr_storage));
if (get_sockaddr(host, port, storage, 1, ipv6first) == -1) {
FATAL("failed to resolve the provided hostname");
}
struct sockaddr *addr = (struct sockaddr *)storage;
init_udprelay(local_addr, local_port, addr,
get_sockaddr_len(addr), mtu, m, auth, listen_ctx.timeout, NULL);
}

if (mode == UDP_ONLY) {
Expand Down
47 changes: 31 additions & 16 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,7 @@ main(int argc, char **argv)
{ "mtu", required_argument, 0, 0 },
{ "help", no_argument, 0, 0 },
{ "plugin", required_argument, 0, 0 },
{ "plugin_opts", required_argument, 0, 0 },
{ "plugin-opts", required_argument, 0, 0 },
#ifdef __linux__
{ "mptcp", no_argument, 0, 0 },
#endif
Expand Down Expand Up @@ -1902,9 +1902,6 @@ main(int argc, char **argv)
len = strlen(server_str);
}

server_host[0] = "127.0.0.1";
server_num = 1;

int err = start_plugin(plugin, plugin_opts, server_str,
plugin_port, server_host[0], server_port);
if (err) {
Expand All @@ -1916,10 +1913,14 @@ main(int argc, char **argv)
listen_ctx_t listen_ctx_list[server_num];

// bind to each interface
for (int i = 0; i < server_num; i++) {
const char *host = server_host[i];
if (mode != UDP_ONLY) {
for (int i = 0; i < server_num; i++) {
const char *host = server_host[i];

if (plugin != NULL) {
host = "127.0.0.1";
}

if (mode != UDP_ONLY) {
// Bind to port
int listenfd;
listenfd = create_and_bind(host, server_port, mptcp);
Expand All @@ -1942,18 +1943,31 @@ main(int argc, char **argv)

ev_io_init(&listen_ctx->io, accept_cb, listenfd, EV_READ);
ev_io_start(loop, &listen_ctx->io);
}

// Setup UDP
if (mode != TCP_ONLY) {
init_udprelay(server_host[i], server_port, mtu, m,
auth, atoi(timeout), iface);
if (host && strcmp(host, ":") > 0)
LOGI("tcp server listening at [%s]:%s", host, server_port);
else
LOGI("tcp server listening at %s:%s", host ? host : "*", server_port);

if (plugin != NULL) break;
}
}

if (host && strcmp(host, ":") > 0)
LOGI("listening at [%s]:%s", host, server_port);
else
LOGI("listening at %s:%s", host ? host : "*", server_port);
if (mode != TCP_ONLY) {
for (int i = 0; i < server_num; i++) {
const char *host = server_host[i];
const char *port = server_port;
if (plugin != NULL) {
port = plugin_port;
}
// Setup UDP
init_udprelay(host, port, mtu, m,
auth, atoi(timeout), iface);
if (host && strcmp(host, ":") > 0)
LOGI("udp server listening at [%s]:%s", host, port);
else
LOGI("udp server listening at %s:%s", host ? host : "*", port);
}
}

if (manager_address != NULL) {
Expand Down Expand Up @@ -2008,6 +2022,7 @@ main(int argc, char **argv)
ev_io_stop(loop, &listen_ctx->io);
close(listen_ctx->fd);
}
if (plugin != NULL) break;
}

if (mode != UDP_ONLY) {
Expand Down
28 changes: 18 additions & 10 deletions src/tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ main(int argc, char **argv)
{ "mtu", required_argument, 0, 0 },
{ "mptcp", no_argument, 0, 0 },
{ "plugin", required_argument, 0, 0 },
{ "plugin_opts", required_argument, 0, 0 },
{ "plugin-opts", required_argument, 0, 0 },
{ "help", no_argument, 0, 0 },
{ 0, 0, 0, 0 }
};
Expand Down Expand Up @@ -1033,10 +1033,6 @@ main(int argc, char **argv)
if (err) {
FATAL("failed to start the plugin");
}

remote_num = 1;
remote_addr[0].host = plugin_host;
remote_addr[0].port = plugin_port;
}

#ifdef __MINGW32__
Expand Down Expand Up @@ -1067,14 +1063,19 @@ main(int argc, char **argv)
memset(listen_ctx.remote_addr, 0, sizeof(struct sockaddr *) * remote_num);
for (i = 0; i < remote_num; i++) {
char *host = remote_addr[i].host;
char *port = remote_addr[i].port == NULL ? remote_port :
remote_addr[i].port;
char *port = remote_addr[i].port == NULL ? remote_port : remote_addr[i].port;
if (plugin != NULL) {
host = plugin_host;
port = plugin_port;
}
struct sockaddr_storage *storage = ss_malloc(sizeof(struct sockaddr_storage));
memset(storage, 0, sizeof(struct sockaddr_storage));
if (get_sockaddr(host, port, storage, 1, ipv6first) == -1) {
FATAL("failed to resolve the provided hostname");
}
listen_ctx.remote_addr[i] = (struct sockaddr *)storage;

if (plugin != NULL) break;
}
listen_ctx.timeout = atoi(timeout);
listen_ctx.iface = iface;
Expand Down Expand Up @@ -1104,9 +1105,16 @@ main(int argc, char **argv)
// Setup UDP
if (mode != TCP_ONLY) {
LOGI("UDP relay enabled");
init_udprelay(local_addr, local_port, listen_ctx.remote_addr[0],
get_sockaddr_len(listen_ctx.remote_addr[0]),
tunnel_addr, mtu, m, auth, listen_ctx.timeout, iface);
char *host = remote_addr[0].host;
char *port = remote_addr[0].port == NULL ? remote_port : remote_addr[0].port;
struct sockaddr_storage *storage = ss_malloc(sizeof(struct sockaddr_storage));
memset(storage, 0, sizeof(struct sockaddr_storage));
if (get_sockaddr(host, port, storage, 1, ipv6first) == -1) {
FATAL("failed to resolve the provided hostname");
}
struct sockaddr *addr = (struct sockaddr *)storage;
init_udprelay(local_addr, local_port, addr, get_sockaddr_len(addr),
tunnel_addr, mtu, m, auth, listen_ctx.timeout, iface);
}

if (mode == UDP_ONLY) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ usage()
printf(
" [--plugin <name>] Enable SIP003 plugin. (Experimental)\n");
printf(
" [--plugin_opts <options>] Set SIP003 plugin options. (Experimental)\n");
" [--plugin-opts <options>] Set SIP003 plugin options. (Experimental)\n");
#endif
printf("\n");
printf(
Expand Down

0 comments on commit 0acc79b

Please sign in to comment.