Browse Source

support bind-to-addr

pull/8/head
Max Lv 12 years ago
parent
commit
655d721c33
2 changed files with 20 additions and 8 deletions
  1. 14
      src/local.c
  2. 14
      src/redir.c

14
src/local.c

@ -54,7 +54,7 @@ int setinterface(int socket_fd, const char* interface_name)
} }
#endif #endif
int create_and_bind(const char *port) {
int create_and_bind(const char *addr, const char *port) {
struct addrinfo hints; struct addrinfo hints;
struct addrinfo *result, *rp; struct addrinfo *result, *rp;
int s, listen_sock; int s, listen_sock;
@ -63,7 +63,7 @@ int create_and_bind(const char *port) {
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */ hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */ hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
s = getaddrinfo("0.0.0.0", port, &hints, &result);
s = getaddrinfo(addr, port, &hints, &result);
if (s != 0) { if (s != 0) {
LOGD("getaddrinfo: %s", gai_strerror(s)); LOGD("getaddrinfo: %s", gai_strerror(s));
return -1; return -1;
@ -601,6 +601,7 @@ int main (int argc, char **argv) {
int i, c; int i, c;
int pid_flags = 0; int pid_flags = 0;
char *local_port = NULL; char *local_port = NULL;
char *local_addr = NULL;
char *password = NULL; char *password = NULL;
char *timeout = NULL; char *timeout = NULL;
char *method = NULL; char *method = NULL;
@ -614,7 +615,7 @@ int main (int argc, char **argv) {
opterr = 0; opterr = 0;
while ((c = getopt (argc, argv, "f:s:p:l:k:t:m:i:c:")) != -1) {
while ((c = getopt (argc, argv, "f:s:p:l:k:t:m:i:c:b:")) != -1) {
switch (c) { switch (c) {
case 's': case 's':
remote_host[remote_num++] = optarg; remote_host[remote_num++] = optarg;
@ -644,6 +645,9 @@ int main (int argc, char **argv) {
case 'i': case 'i':
iface = optarg; iface = optarg;
break; break;
case 'b':
local_addr = optarg;
break;
} }
} }
@ -675,6 +679,8 @@ int main (int argc, char **argv) {
if (timeout == NULL) timeout = "10"; if (timeout == NULL) timeout = "10";
if (local_addr == NULL) local_addr = "0.0.0.0";
if (pid_flags) { if (pid_flags) {
demonize(pid_path); demonize(pid_path);
} }
@ -688,7 +694,7 @@ int main (int argc, char **argv) {
// Setup socket // Setup socket
int listenfd; int listenfd;
listenfd = create_and_bind(local_port);
listenfd = create_and_bind(local_addr, local_port);
if (listenfd < 0) { if (listenfd < 0) {
FATAL("bind() error.."); FATAL("bind() error..");
} }

14
src/redir.c

@ -49,7 +49,7 @@ int setnonblocking(int fd) {
return fcntl(fd, F_SETFL, flags | O_NONBLOCK); return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
} }
int create_and_bind(const char *port) {
int create_and_bind(const char *addr, const char *port) {
struct addrinfo hints; struct addrinfo hints;
struct addrinfo *result, *rp; struct addrinfo *result, *rp;
int s, listen_sock; int s, listen_sock;
@ -58,7 +58,7 @@ int create_and_bind(const char *port) {
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */ hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */ hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
s = getaddrinfo("0.0.0.0", port, &hints, &result);
s = getaddrinfo(addr, port, &hints, &result);
if (s != 0) { if (s != 0) {
LOGD("getaddrinfo: %s", gai_strerror(s)); LOGD("getaddrinfo: %s", gai_strerror(s));
return -1; return -1;
@ -533,6 +533,7 @@ int main (int argc, char **argv) {
int i, c; int i, c;
int pid_flags = 0; int pid_flags = 0;
char *local_port = NULL; char *local_port = NULL;
char *local_addr = NULL;
char *password = NULL; char *password = NULL;
char *timeout = NULL; char *timeout = NULL;
char *method = NULL; char *method = NULL;
@ -545,7 +546,7 @@ int main (int argc, char **argv) {
opterr = 0; opterr = 0;
while ((c = getopt (argc, argv, "f:s:p:l:k:t:m:c:")) != -1) {
while ((c = getopt (argc, argv, "f:s:p:l:k:t:m:c:b:")) != -1) {
switch (c) { switch (c) {
case 's': case 's':
remote_host[remote_num++] = optarg; remote_host[remote_num++] = optarg;
@ -572,6 +573,9 @@ int main (int argc, char **argv) {
case 'c': case 'c':
conf_path = optarg; conf_path = optarg;
break; break;
case 'b':
local_addr = optarg;
break;
} }
} }
@ -603,6 +607,8 @@ int main (int argc, char **argv) {
if (timeout == NULL) timeout = "10"; if (timeout == NULL) timeout = "10";
if (local_addr == NULL) local_addr = "0.0.0.0";
if (pid_flags) { if (pid_flags) {
demonize(pid_path); demonize(pid_path);
} }
@ -616,7 +622,7 @@ int main (int argc, char **argv) {
// Setup socket // Setup socket
int listenfd; int listenfd;
listenfd = create_and_bind(local_port);
listenfd = create_and_bind(local_addr, local_port);
if (listenfd < 0) { if (listenfd < 0) {
FATAL("bind() error.."); FATAL("bind() error..");
} }

Loading…
Cancel
Save