Skip to content

Commit

Permalink
lib: bareos listens on both ipv6 and ipv4 per default
Browse files Browse the repository at this point in the history
made sure that IPV6 sockets only listen for V6 when created.
  • Loading branch information
alaaeddineelamri committed Sep 23, 2021
1 parent 0e2de9c commit 8d3fecc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
12 changes: 6 additions & 6 deletions core/src/lib/address_conf.cc
Expand Up @@ -347,19 +347,19 @@ int AddAddress(dlist<IPADDR>** out,
? IPADDR::R_SINGLE
: type;
if (type != IPADDR::R_DEFAULT) {
IPADDR* def = 0;
IPADDR* default_address = 0;
foreach_dlist (iaddr, addrs) {
if (iaddr->GetType() == IPADDR::R_DEFAULT) {
def = iaddr;
default_address = iaddr;
} else if (iaddr->GetType() != type) {
Bsnprintf(buf, buflen,
_("the old style addresses cannot be mixed with new style"));
return 0;
}
}
if (def) {
addrs->remove(def);
delete def;
if (default_address) {
addrs->remove(default_address);
delete default_address;
}
}

Expand Down Expand Up @@ -427,7 +427,7 @@ void InitDefaultAddresses(dlist<IPADDR>** out, const char* port)
char buf[1024];
unsigned short sport = str_to_int32(port);

if (!AddAddress(out, IPADDR::R_DEFAULT, htons(sport), AF_INET, 0, 0, buf,
if (!AddAddress(out, IPADDR::R_DEFAULT, htons(sport), 0, 0, 0, buf,
sizeof(buf))) {
Emsg1(M_ERROR_TERM, 0, _("Can't add default address (%s)\n"), buf);
}
Expand Down
15 changes: 15 additions & 0 deletions core/src/lib/bnet_server_tcp.cc
Expand Up @@ -190,6 +190,21 @@ static int OpenSocketAndBind(IPADDR* ipaddr,
return -2;
}

if (ipaddr->GetFamily() == AF_INET6) {
int ipv6only_option_value = 1;
socklen_t option_len = sizeof(int);

if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
(sockopt_val_t)&ipv6only_option_value, option_len)
< 0) {
BErrNo be;
Emsg1(M_WARNING, 0, _("Cannot set IPV6_V6ONLY on socket: %s\n"),
be.bstrerror());

return -2;
}
}

tries = 0;

do {
Expand Down
17 changes: 17 additions & 0 deletions core/src/lib/res.cc
Expand Up @@ -1337,12 +1337,29 @@ void ConfigurationParser::StoreAddressesPort(LEX* lc,
|| token == BCT_IDENTIFIER)) {
scan_err1(lc, _("Expected a port number or string, got: %s"), lc->str);
}

#ifdef HAVE_IPV6
if (pass == 1
&& !AddAddress(GetItemVariablePointer<dlist<IPADDR>**>(*item),
IPADDR::R_MULTIPLE, htons(port), AF_INET, 0, lc->str,
errmsg, sizeof(errmsg))) {
scan_err2(lc, _("can't add port (%s) to (%s)"), lc->str, errmsg);
}

if (pass == 1
&& !AddAddress(GetItemVariablePointer<dlist<IPADDR>**>(*item),
IPADDR::R_MULTIPLE, htons(port), AF_INET6, 0, lc->str,
errmsg, sizeof(errmsg))) {
scan_err2(lc, _("can't add port (%s) to (%s)"), lc->str, errmsg);
}
#else
if (pass == 1
&& !AddAddress(GetItemVariablePointer<dlist<IPADDR>**>(*item),
IPADDR::R_SINGLE_PORT, htons(port), AF_INET, 0, lc->str,
errmsg, sizeof(errmsg))) {
scan_err2(lc, _("can't add port (%s) to (%s)"), lc->str, errmsg);
}
#endif
}

// Generic store resource dispatcher.
Expand Down

0 comments on commit 8d3fecc

Please sign in to comment.