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..aa5678a348c6c4b492831fa42760df292c7b9294 |
--- /dev/null |
+++ b/cloud_print/gcp20/prototype/printer.cc |
@@ -0,0 +1,132 @@ |
+// 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" |
+ |
+namespace { |
+ |
+const char* kServiceNamePrefix = "first_privet_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) { |
+ 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; |
Vitaly Buka (NO REVIEWS)
2013/06/15 01:20:58
interface fits on the first line
maksymb
2013/06/15 01:56:58
Done.
|
+ 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( |
+ new PrivetDnsResponseBuilderFactory(kServiceNamePrefix, |
+ kServiceDomainName, |
+ ip, |
+ ReadHttpPortFromCommandLine()), |
+ ReadTtlFromCommandLine()); |
+ return initialized_; |
+} |
+ |
+void Printer::Stop() { |
+ if (!initialized_) |
+ return; |
+ |
+ dns_server_.Shutdown(); |
+} |
+ |