Skip to content

Commit

Permalink
Added --disable-ipv4 and --disable-ipv6 switches.
Browse files Browse the repository at this point in the history
By default both enabled.

TBR=gene@chromium.org

Review URL: https://codereview.chromium.org/598433002

Cr-Commit-Position: refs/heads/master@{#296150}
  • Loading branch information
vitalybuka authored and Commit bot committed Sep 23, 2014
1 parent a032d59 commit 0b33e27
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
2 changes: 0 additions & 2 deletions cloud_print/gcp20/prototype/dns_response_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ void DnsResponseBuilder::AppendA(const std::string& service_domain_name,
// TODO(maksymb): IP to send must depends on interface from where query was
// received.
if (http_ipv4.empty()) {
LOG(ERROR) << "Invalid IP";
return;
}

Expand All @@ -96,7 +95,6 @@ void DnsResponseBuilder::AppendAAAA(const std::string& service_domain_name,
// TODO(maksymb): IP to send must depends on interface from where query was
// received.
if (http_ipv6.empty()) {
LOG(ERROR) << "Invalid IP";
return;
}

Expand Down
6 changes: 5 additions & 1 deletion cloud_print/gcp20/prototype/gcp20_switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace switches {

const char kDisableConfirmation[] = "disable-confirmation";
const char kDisableIpv4[] = "disable-ipv4";
const char kDisableIpv6[] = "disable-ipv6";
const char kDisableMethodCheck[] = "disable-method-check";
const char kDisableXTocken[] = "disable-x-token";
const char kDomainName[] = "domain-name";
Expand All @@ -30,13 +32,15 @@ const struct {
const char* const arg;
} kHelpStrings[] = {
{kDisableConfirmation, "disables confirmation of registration", NULL},
{kDisableIpv4, "disables IPv4 support", NULL},
{kDisableIpv6, "disables IPv6 support", NULL},
{kDisableMethodCheck, "disables HTTP method checking (POST, GET)", NULL},
{kDisableXTocken, "disables checking of X-Privet-Token HTTP header", NULL},
{kNoAnnouncement, "disables DNS announcements", NULL},
{kExtendedResponce, "responds to PTR with additional records", NULL},
{kSimulatePrintingErrors, "simulates some errors for local printing", NULL},
{kUnicastRespond,
"DNS responses will be sent in unicast instead of multicast", NULL},
"DNS responses will be sent in unicast instead of multicast", NULL},
{kDomainName, "sets, should ends with '.local'", "DOMAIN"},
{kHttpPort, "sets port for HTTP server", "PORT"},
{kServiceName, "sets DNS service name", "SERVICE"},
Expand Down
2 changes: 2 additions & 0 deletions cloud_print/gcp20/prototype/gcp20_switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
namespace switches {

extern const char kDisableConfirmation[];
extern const char kDisableIpv4[];
extern const char kDisableIpv6[];
extern const char kDisableMethodCheck[];
extern const char kDisableXTocken[];
extern const char kDomainName[];
Expand Down
44 changes: 33 additions & 11 deletions cloud_print/gcp20/prototype/printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "cloud_print/gcp20/prototype/command_line_reader.h"
#include "cloud_print/gcp20/prototype/gcp20_switches.h"
#include "cloud_print/gcp20/prototype/local_settings.h"
#include "cloud_print/gcp20/prototype/service_parameters.h"
#include "cloud_print/gcp20/prototype/special_io.h"
Expand Down Expand Up @@ -832,30 +833,51 @@ bool Printer::StartLocalDiscoveryServers() {
bool Printer::StartDnsServer() {
DCHECK(state_.local_settings.local_discovery);

net::IPAddressNumber ipv4;
net::IPAddressNumber ipv6;

if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableIpv4)) {
ipv4 = GetLocalIp("", false);
}
if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableIpv6)) {
ipv6 = GetLocalIp("", true);
}

// TODO(maksymb): Add switch for command line to control interface name.
net::IPAddressNumber ip = GetLocalIp("", false);
if (ip.empty()) {
if (ipv4.empty() && ipv6.empty()) {
LOG(ERROR) << "No local IP found. Cannot start printer.";
return false;
}
VLOG(0) << "Local address: " << net::IPAddressToString(ip);

uint16 port = command_line_reader::ReadHttpPort(kHttpPortDefault);

std::string service_name_prefix =
command_line_reader::ReadServiceNamePrefix(kServiceNamePrefixDefault +
net::IPAddressToString(ip));
std::replace(service_name_prefix .begin(), service_name_prefix .end(),
'.', '_');
VLOG_IF(0, !ipv4.empty())
<< "Local IPv4 address: " << net::IPAddressToStringWithPort(ipv4, port);
VLOG_IF(0, !ipv6.empty())
<< "Local IPv6 address: " << net::IPAddressToStringWithPort(ipv6, port);

std::string service_name_prefix = kServiceNamePrefixDefault;
if (!ipv4.empty())
service_name_prefix += net::IPAddressToString(ipv4);
service_name_prefix =
command_line_reader::ReadServiceNamePrefix(service_name_prefix);
std::replace(
service_name_prefix.begin(), service_name_prefix.end(), '.', '_');

std::string service_domain_name =
command_line_reader::ReadDomainName(
base::StringPrintf(kServiceDomainNameFormatDefault,
base::RandInt(0, INT_MAX)));

ServiceParameters params(kServiceType, kSecondaryServiceType,
service_name_prefix, service_domain_name,
ip, GetLocalIp("", true), port);
ServiceParameters params(kServiceType,
kSecondaryServiceType,
service_name_prefix,
service_domain_name,
ipv4,
ipv6,
port);

return dns_server_.Start(params,
command_line_reader::ReadTtl(kTtlDefault),
Expand Down
5 changes: 4 additions & 1 deletion cloud_print/gcp20/prototype/privet_http_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ bool PrivetHttpServer::Start(uint16 port) {

scoped_ptr<net::ServerSocket> server_socket(
new net::TCPServerSocket(NULL, net::NetLog::Source()));
server_socket->ListenWithAddressAndPort("0.0.0.0", port, 1);
std::string listen_address = "::";
if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableIpv6))
listen_address = "0.0.0.0";
server_socket->ListenWithAddressAndPort(listen_address, port, 1);
server_.reset(new net::HttpServer(server_socket.Pass(), this));

net::IPEndPoint address;
Expand Down

0 comments on commit 0b33e27

Please sign in to comment.