OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "cloud_print/gcp20/prototype/printer.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/command_line.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "cloud_print/gcp20/prototype/service_parameters.h" |
| 13 #include "net/base/net_util.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 const char* kServiceType = "_privet._tcp.local"; |
| 18 const char* kServiceNamePrefix = "first_gcp20_device"; |
| 19 const char* kServiceDomainName = "my-privet-device.local"; |
| 20 |
| 21 const uint16 kDefaultTTL = 60*60; |
| 22 const uint16 kDefaultHttpPort = 10101; |
| 23 |
| 24 uint16 ReadHttpPortFromCommandLine() { |
| 25 uint32 http_port_tmp = kDefaultHttpPort; |
| 26 |
| 27 std::string http_port_string_tmp = |
| 28 CommandLine::ForCurrentProcess()->GetSwitchValueASCII("http-port"); |
| 29 base::StringToUint(http_port_string_tmp, &http_port_tmp); |
| 30 |
| 31 if (http_port_tmp > kuint16max) { |
| 32 LOG(ERROR) << "Port " << http_port_tmp << " is too large (maximum is " << |
| 33 kDefaultHttpPort << "). Using default port."; |
| 34 |
| 35 http_port_tmp = kDefaultHttpPort; |
| 36 } |
| 37 |
| 38 VLOG(1) << "HTTP port for responses: " << http_port_tmp; |
| 39 return static_cast<uint16>(http_port_tmp); |
| 40 } |
| 41 |
| 42 uint16 ReadTtlFromCommandLine() { |
| 43 uint32 ttl = kDefaultTTL; |
| 44 |
| 45 base::StringToUint( |
| 46 CommandLine::ForCurrentProcess()->GetSwitchValueASCII("ttl"), &ttl); |
| 47 |
| 48 VLOG(1) << "TTL for announcements: " << ttl; |
| 49 return ttl; |
| 50 } |
| 51 |
| 52 // Returns local IP address number of first interface found (except loopback). |
| 53 // Return value is empty if no interface found. Possible interfaces names are |
| 54 // "eth0", "wlan0" etc. If interface name is empty, function will return IP |
| 55 // address of first interface found. |
| 56 net::IPAddressNumber GetLocalIp(const std::string& interface_name, |
| 57 bool return_ipv6_number) { |
| 58 net::NetworkInterfaceList interfaces; |
| 59 bool success = net::GetNetworkList(&interfaces); |
| 60 DCHECK(success); |
| 61 |
| 62 size_t expected_address_size = return_ipv6_number ? net::kIPv6AddressSize |
| 63 : net::kIPv4AddressSize; |
| 64 |
| 65 for (net::NetworkInterfaceList::iterator iter = interfaces.begin(); |
| 66 iter != interfaces.end(); ++iter) { |
| 67 if (iter->address.size() == expected_address_size && |
| 68 (interface_name.empty() || interface_name == iter->name)) { |
| 69 LOG(INFO) << net::IPAddressToString(iter->address); |
| 70 return iter->address; |
| 71 } |
| 72 } |
| 73 |
| 74 return net::IPAddressNumber(); |
| 75 } |
| 76 |
| 77 } // namespace |
| 78 |
| 79 Printer::Printer() : initialized_(false) { |
| 80 } |
| 81 |
| 82 Printer::~Printer() { |
| 83 Stop(); |
| 84 } |
| 85 |
| 86 bool Printer::Start() { |
| 87 if (initialized_) |
| 88 return true; |
| 89 |
| 90 // TODO(maksymb): Add switch for command line to control interface name. |
| 91 net::IPAddressNumber ip = GetLocalIp("", false); |
| 92 if (ip.empty()) { |
| 93 LOG(ERROR) << "No local IP found. Cannot start printer."; |
| 94 return false; |
| 95 } |
| 96 VLOG(1) << "Local address: " << net::IPAddressToString(ip); |
| 97 |
| 98 // Starting DNS-SD server. |
| 99 initialized_ = dns_server_.Start( |
| 100 ServiceParameters(kServiceType, |
| 101 kServiceNamePrefix, |
| 102 kServiceDomainName, |
| 103 ip, |
| 104 ReadHttpPortFromCommandLine()), |
| 105 ReadTtlFromCommandLine(), |
| 106 CreateTxt()); |
| 107 |
| 108 return initialized_; |
| 109 } |
| 110 |
| 111 void Printer::Stop() { |
| 112 if (!initialized_) |
| 113 return; |
| 114 |
| 115 dns_server_.Shutdown(); |
| 116 } |
| 117 |
| 118 std::vector<std::string> Printer::CreateTxt() const { |
| 119 std::vector<std::string> txt; |
| 120 |
| 121 txt.push_back("txtvers=1"); |
| 122 txt.push_back("ty=Google Cloud Ready Printer GCP2.0 Prototype"); |
| 123 txt.push_back("note=Virtual printer"); |
| 124 txt.push_back("url=https://www.google.com/cloudprint"); |
| 125 txt.push_back("type=printer"); |
| 126 txt.push_back("id="); |
| 127 txt.push_back("cs=offline"); |
| 128 |
| 129 return txt; |
| 130 } |
| 131 |
OLD | NEW |