Skip to content

Commit

Permalink
implement --listen <address> option (icecc#427)
Browse files Browse the repository at this point in the history
This can help to listen on the right network interface if e.g. by
default a slower VPN interface is selected.
  • Loading branch information
llunak committed Jul 15, 2019
1 parent 24e9198 commit 5e275a5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
19 changes: 16 additions & 3 deletions daemon/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ void usage(const char *reason = 0)
}

cerr << "usage: iceccd [-n <netname>] [-m <max_processes>] [--no-remote] [-d|--daemonize] [-l logfile] [-s <schedulerhost[:port]>]"
" [-v[v[v]]] [-u|--user-uid <user_uid>] [-b <env-basedir>] [--cache-limit <MB>] [-N <node_name>]" << endl;
" [-v[v[v]]] [-u|--user-uid <user_uid>] [-b <env-basedir>] [--cache-limit <MB>] [-N <node_name>] [--listen <address>]" << endl;
exit(1);
}

Expand Down Expand Up @@ -467,6 +467,7 @@ struct Daemon {
string nodename;
bool noremote;
bool custom_nodename;
const char* listen_addr;
size_t cache_size;
map<int, MsgChannel *> fd2chan;
int new_client_id;
Expand Down Expand Up @@ -514,6 +515,7 @@ struct Daemon {
cache_size = 0;
noremote = false;
custom_nodename = false;
listen_addr = NULL;
icecream_load = 0;
icecream_usage.tv_sec = icecream_usage.tv_usec = 0;
current_load = - 1000;
Expand Down Expand Up @@ -591,7 +593,13 @@ bool Daemon::setup_listen_fds()
struct sockaddr_in myaddr;
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(daemon_port);
myaddr.sin_addr.s_addr = INADDR_ANY;
if( listen_addr ) {
if( !inet_aton( listen_addr, &myaddr.sin_addr )) {
log_error() << "Address passed to --listen (" << listen_addr << ") is not valid" << endl;
return false;
}
} else
myaddr.sin_addr.s_addr = INADDR_ANY;

if (::bind(tcp_listen_fd, (struct sockaddr *)&myaddr,
sizeof(myaddr)) < 0) {
Expand Down Expand Up @@ -2210,6 +2218,7 @@ int main(int argc, char **argv)
{ "cache-limit", 1, NULL, 0},
{ "no-remote", 0, NULL, 0},
{ "port", 1, NULL, 'p'},
{ "listen", 1, NULL, 0},
{ 0, 0, 0, 0 }
};

Expand Down Expand Up @@ -2253,8 +2262,12 @@ int main(int argc, char **argv)
}
} else if (optname == "no-remote") {
d.noremote = true;
} else if (optname == "listen") {
if (optarg && *optarg)
d.listen_addr = optarg;
else
usage("Error: --listen requires argument");
}

}
break;
case 'd':
Expand Down
40 changes: 31 additions & 9 deletions scheduler/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,7 @@ static bool handle_activity(CompileServer *cs)
return ret;
}

static int open_broad_listener(int port)
static int open_broad_listener(int port, const char* listen_addr)
{
int listen_fd;
struct sockaddr_in myaddr;
Expand All @@ -1766,7 +1766,13 @@ static int open_broad_listener(int port)

myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(port);
myaddr.sin_addr.s_addr = INADDR_ANY;
if( listen_addr ) {
if( !inet_aton( listen_addr, &myaddr.sin_addr )) {
log_error() << "Address passed to --listen (" << listen_addr << ") is not valid" << endl;
return -1;
}
} else
myaddr.sin_addr.s_addr = INADDR_ANY;

if (::bind(listen_fd, (struct sockaddr *) &myaddr, sizeof(myaddr)) < 0) {
log_perror("bind()");
Expand All @@ -1776,7 +1782,7 @@ static int open_broad_listener(int port)
return listen_fd;
}

static int open_tcp_listener(short port)
static int open_tcp_listener(short port, const char* listen_addr)
{
int fd;
struct sockaddr_in myaddr;
Expand All @@ -1803,7 +1809,13 @@ static int open_tcp_listener(short port)

myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(port);
myaddr.sin_addr.s_addr = INADDR_ANY;
if( listen_addr ) {
if( !inet_aton( listen_addr, &myaddr.sin_addr )) {
log_error() << "Address passed to --listen (" << listen_addr << ") is not valid" << endl;
return -1;
}
} else
myaddr.sin_addr.s_addr = INADDR_ANY;

if (::bind(fd, (struct sockaddr *) &myaddr, sizeof(myaddr)) < 0) {
log_perror("bind()");
Expand Down Expand Up @@ -1835,6 +1847,7 @@ static void usage(const char *reason = 0)
<< " -u, --user-uid\n"
<< " -v[v[v]]]\n"
<< " -r, --persistent-client-connection\n"
<< " --listen <address>\n"
<< endl;

exit(1);
Expand Down Expand Up @@ -1907,6 +1920,7 @@ int main(int argc, char *argv[])
uid_t user_uid;
gid_t user_gid;
int warn_icecc_user_errno = 0;
const char* listen_addr = NULL;

if (getuid() == 0) {
struct passwd *pw = getpwnam("icecc");
Expand Down Expand Up @@ -1934,6 +1948,7 @@ int main(int argc, char *argv[])
{ "daemonize", 0, NULL, 'd'},
{ "log-file", 1, NULL, 'l'},
{ "user-uid", 1, NULL, 'u'},
{ "listen", 1, NULL, 0 },
{ 0, 0, 0, 0 }
};

Expand All @@ -1944,8 +1959,15 @@ int main(int argc, char *argv[])
}

switch (c) {
case 0:
(void) long_options[option_index].name;
case 0: {
string optname = long_options[option_index].name;
if (optname == "listen") {
if (optarg && *optarg)
listen_addr = optarg;
else
usage("Error: --listen requires argument");
}
}
break;
case 'd':
detach = true;
Expand Down Expand Up @@ -2065,19 +2087,19 @@ int main(int argc, char *argv[])
}
}

listen_fd = open_tcp_listener(scheduler_port);
listen_fd = open_tcp_listener(scheduler_port, listen_addr);

if (listen_fd < 0) {
return 1;
}

text_fd = open_tcp_listener(scheduler_port + 1);
text_fd = open_tcp_listener(scheduler_port + 1, listen_addr);

if (text_fd < 0) {
return 1;
}

broad_fd = open_broad_listener(scheduler_port);
broad_fd = open_broad_listener(scheduler_port, listen_addr);

if (broad_fd < 0) {
return 1;
Expand Down

0 comments on commit 5e275a5

Please sign in to comment.