Index: cloud_print/gcp20/prototype/printer.cc |
diff --git a/cloud_print/gcp20/prototype/printer.cc b/cloud_print/gcp20/prototype/printer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4a6fe0f64521a5d1830de6680640c05214929e61 |
--- /dev/null |
+++ b/cloud_print/gcp20/prototype/printer.cc |
@@ -0,0 +1,147 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "cloud_print/gcp20/prototype/printer.h" |
+ |
+#include <ifaddrs.h> |
+#include <netdb.h> |
+ |
+#include "base/command_line.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "cloud_print/gcp20/prototype/dns_txt_builder.h" |
+#include "cloud_print/gcp20/prototype/service_parameters.h" |
+ |
+namespace { |
+ |
+const char* kServiceType = "_privet._tcp.local"; |
+const char* kServiceNamePrefix = "first_gcp20_device"; |
+const char* kServiceDomainName = "my-privet-device.local"; |
+ |
+const uint16 kDefaultTTL = 60*60; |
+const uint16 kDefaultHttpPort = 10101; |
+ |
+uint16 ReadHttpPortFromCommandLine() { |
+ uint32 http_port_tmp = kDefaultHttpPort; |
+ |
+ std::string http_port_string_tmp = |
+ CommandLine::ForCurrentProcess()->GetSwitchValueASCII("http-port"); |
+ base::StringToUint(http_port_string_tmp, &http_port_tmp); |
+ |
+ if (http_port_tmp > kuint32max) { |
gene
2013/06/19 05:40:51
I think max port number in TCP and UDP is 64k
maksymb
2013/06/19 21:47:21
Yes, this is right. Fixed.
|
+ LOG(ERROR) << "Port " << http_port_tmp << " is too large (maximum is " << |
+ kDefaultHttpPort << "). Using default port."; |
+ |
+ http_port_tmp = kDefaultHttpPort; |
+ } |
+ |
+ VLOG(1) << "HTTP port for responses: " << http_port_tmp; |
+ return static_cast<uint16>(http_port_tmp); |
+} |
+ |
+uint16 ReadTtlFromCommandLine() { |
+ uint32 ttl = kDefaultTTL; |
+ |
+ base::StringToUint( |
+ CommandLine::ForCurrentProcess()->GetSwitchValueASCII("ttl"), &ttl); |
+ |
+ VLOG(1) << "TTL for announcements: " << ttl; |
+ return ttl; |
+} |
+ |
+// Returns pointer to local IP address number. Examples for interface name |
+// are: "wlan0", "eth0", NULL (find first acceptable except "lo"). |
+net::IPAddressNumber GetLocalIp(const char* interface_name) { |
+ net::IPAddressNumber result; // |result| is empty if no interface found. |
+ |
+ ifaddrs* first_interface = NULL; |
+ if (getifaddrs(&first_interface) == -1) { |
+ LOG(ERROR) << "getifaddrs() failed"; |
+ return result; |
+ } |
+ |
+ // Browse all interfaces. |
+ char host[NI_MAXHOST] = {0}; |
+ for (ifaddrs* interface = first_interface; interface; |
+ interface = interface->ifa_next) { |
+ if (!interface->ifa_addr) |
+ continue; |
+ |
+ // Get additional info about current interface. |
+ int rv = getnameinfo(interface->ifa_addr, sizeof(*interface->ifa_addr), |
+ host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); |
+ |
+ if (interface->ifa_addr->sa_family == AF_INET) { |
+ // Find interface we need. |
+ if (interface_name ? (strcmp(interface->ifa_name, interface_name) == 0) |
+ : (strcmp(interface->ifa_name, "lo") != 0)) { |
+ if (rv != 0) { |
+ LOG(ERROR) << "getnameinfo() failed: " << gai_strerror(rv); |
+ break; |
+ } |
+ |
+ net::ParseIPLiteralToNumber(host, &result); |
+ } |
+ } |
+ } |
+ |
+ // Erase memory under list of interfaces. |
+ freeifaddrs(first_interface); |
+ return result; |
+} |
+ |
+} // namespace |
+ |
+Printer::Printer() : initialized_(false) { |
+} |
+ |
+Printer::~Printer() { |
+ Stop(); |
+} |
+ |
+bool Printer::Start() { |
+ if (initialized_) |
+ return true; |
+ |
+ // TODO(maksymb): Add possibility to control interface via command line args. |
+ net::IPAddressNumber ip = GetLocalIp(NULL); |
+ if (ip.empty()) { |
+ LOG(ERROR) << "No local IP found. Cannot start printer."; |
+ return false; |
+ } |
+ VLOG(1) << "Local address: " << net::IPAddressToString(ip); |
+ |
+ // Starting DNS-SD server. |
+ initialized_ = dns_server_.Start( |
+ ServiceParameters(kServiceType, |
+ kServiceNamePrefix, |
+ kServiceDomainName, |
+ ip, |
+ ReadHttpPortFromCommandLine()), |
+ ReadTtlFromCommandLine(), |
+ CreateTxt()); |
+ |
+ return initialized_; |
+} |
+ |
+void Printer::Stop() { |
+ if (!initialized_) |
+ return; |
+ |
+ dns_server_.Shutdown(); |
+} |
+ |
+std::string Printer::CreateTxt() const { |
+ DnsTxtBuilder txt_builder; |
+ |
+ txt_builder.AddRecord("txtvers=1"); |
+ txt_builder.AddRecord("ty=Google Cloud Ready Printer GCP2.0 Prototype"); |
+ txt_builder.AddRecord("note=Virtual printer"); |
+ txt_builder.AddRecord("url=https://www.google.com/cloudprint"); |
+ txt_builder.AddRecord("type=printer"); |
+ txt_builder.AddRecord("id="); |
+ txt_builder.AddRecord("cs=offline"); |
+ |
+ return txt_builder.Build(); |
+} |
+ |