Skip to content

Commit

Permalink
Added '--bind' command-line option for specifying local addresses to …
Browse files Browse the repository at this point in the history
…bind to.

tcp_listener_t will now listen on multiple sockets (one for each interface), and will try to have all the sockets on the same port (in the case that port 0 is specified), or will fail if that is impossible.
  • Loading branch information
Marc Hesse committed Nov 20, 2012
1 parent 7108955 commit 0b584ea
Show file tree
Hide file tree
Showing 39 changed files with 687 additions and 239 deletions.
8 changes: 4 additions & 4 deletions scripts/spin_up_cluster
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ else:
hostname = socket.gethostname()
if (i == 0):
if (valgrind):
cl = ["valgrind", "--leak-check=full", "--track-origins=yes", "--suppressions=../scripts/rethinkdb-valgrind-suppressions.supp", binary, "--directory", directory, "--machine-name", machine_name, "--port-offset", str(i+port)]
cl = ["valgrind", "--leak-check=full", "--track-origins=yes", "--suppressions=../scripts/rethinkdb-valgrind-suppressions.supp", binary, "--directory", directory, "--machine-name", machine_name, "--port-offset", str(i+port), "--bind", "all"]
else:
cl = [binary, "--directory", directory, "--machine-name", machine_name, "--port-offset", str(i+port)]
cl = [binary, "--directory", directory, "--machine-name", machine_name, "--port-offset", str(i+port), "--bind", "all"]
else:
if (valgrind):
cl = ["valgrind", "--leak-check=full", "--track-origins=yes", "--suppressions=../scripts/rethinkdb-valgrind-suppressions.supp", binary, "--directory", directory, "--machine-name", machine_name, "--join", "%s:%d" % (hostname, 29015 + port), "--port-offset", str(i+port)]
cl = ["valgrind", "--leak-check=full", "--track-origins=yes", "--suppressions=../scripts/rethinkdb-valgrind-suppressions.supp", binary, "--directory", directory, "--machine-name", machine_name, "--join", "%s:%d" % (hostname, 29015 + port), "--port-offset", str(i+port), "--bind", "all"]
else:
cl = [binary, "--directory", directory, "--machine-name", machine_name, "--join", "%s:%d" % (hostname, 29015 + port), "--port-offset", str(i+port)]
cl = [binary, "--directory", directory, "--machine-name", machine_name, "--join", "%s:%d" % (hostname, 29015 + port), "--port-offset", str(i+port), "--bind", "all"]
print ' '.join(cl)
kids += [subprocess.Popen(cl, stdout=sys.stdout, stderr=sys.stderr)]
time.sleep(1)
Expand Down
4 changes: 2 additions & 2 deletions scripts/spin_up_cluster_with_name_conflict
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ else:
directory = "cluster_directory_%d" % i
os.system("rm -rf " + directory)
if (i == 0):
cl = ["../build/debug/rethinkdb", str(port), "--directory", directory, "--machine-name", machine_name, "--port-offset", str(i+port)]
cl = ["../build/debug/rethinkdb", str(port), "--directory", directory, "--machine-name", machine_name, "--port-offset", str(i+port), "--bind", "all"]
else:
cl = ["../build/debug/rethinkdb", str(port+i), "--directory", directory, "--machine-name", machine_name, "--join", "%s:%d" % (hostname, 29015 + port), "--port-offset", str(i+port)]
cl = ["../build/debug/rethinkdb", str(port+i), "--directory", directory, "--machine-name", machine_name, "--join", "%s:%d" % (hostname, 29015 + port), "--port-offset", str(i+port), "--bind", "all"]
print ' '.join(cl)
kids += [subprocess.Popen(cl)]
time.sleep(1)
Expand Down
31 changes: 24 additions & 7 deletions src/arch/address.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,36 +70,53 @@ std::set<ip_address_t> ip_address_t::from_hostname(const std::string &host) {
return ips;
}

std::set<ip_address_t> ip_address_t::us() {
std::set<ip_address_t> ips;
bool check_address_filter(ip_address_t addr, const std::set<ip_address_t> &filter) {
// The filter is a whitelist, loopback addresses are always whitelisted
return filter.find(addr) != filter.end() || addr.is_loopback();
}

std::set<ip_address_t> ip_address_t::get_local_addresses(const std::set<ip_address_t> &filter, bool get_all) {
std::set<ip_address_t> all_ips;
std::set<ip_address_t> filtered_ips;

try {
ips = from_hostname(str_gethostname());
all_ips = from_hostname(str_gethostname());
} catch (const host_lookup_exc_t &ex) {
// Continue on, this probably means there's no DNS entry for this host
}

struct ifaddrs *addrs;
int res = getifaddrs(&addrs);
guarantee_err(res == 0, "getifaddrs() failed");
guarantee_err(res == 0, "getifaddrs() failed, could not determine local network interfaces");

for (struct ifaddrs *current_addr = addrs; current_addr != NULL; current_addr = current_addr->ifa_next) {
struct sockaddr *addr_data = current_addr->ifa_addr;
if (addr_data == NULL) {
continue;
} else if (addr_data->sa_family == AF_INET) {
ips.insert(ip_address_t(addr_data));
all_ips.insert(ip_address_t(addr_data));
}
}

freeifaddrs(addrs);
return ips;

// Remove any addresses that don't fit the filter
for (std::set<ip_address_t>::const_iterator it = all_ips.begin(); it != all_ips.end(); ++it) {
if (get_all || check_address_filter(*it, filter)) {
filtered_ips.insert(*it);
}
}

return filtered_ips;
}

std::string ip_address_t::as_dotted_decimal() const {
return addr_as_dotted_decimal(get_addr());
}

bool ip_address_t::is_loopback() const {
return (ntohl(s_addr) >> 24) == 127;
}

void debug_print(append_only_printf_buffer_t *buf, const ip_address_t &addr) {
buf->appendf("%s", addr.as_dotted_decimal().c_str());
}
4 changes: 3 additions & 1 deletion src/arch/address.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class host_lookup_exc_t : public std::exception {
class ip_address_t {
public:
static std::set<ip_address_t> from_hostname(const std::string &host);
static std::set<ip_address_t> us();
static std::set<ip_address_t> get_local_addresses(const std::set<ip_address_t> &filter, bool get_all);

ip_address_t() { } //for deserialization

Expand All @@ -64,6 +64,8 @@ class ip_address_t {
/* Returns IP address in `a.b.c.d` form. */
std::string as_dotted_decimal() const;

bool is_loopback() const;

struct in_addr get_addr() const {
struct in_addr addr;
addr.s_addr = s_addr;
Expand Down
Loading

0 comments on commit 0b584ea

Please sign in to comment.