Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Unified Diff: cloud_print/gcp20/prototype/printer.cc

Issue 16975004: Finished DNS-SD server. Finished Privet-specified DNS-SD server. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed Lint errors. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..2f508174660234636f7b499dc59575167eb8ba28
--- /dev/null
+++ b/cloud_print/gcp20/prototype/printer.cc
@@ -0,0 +1,131 @@
+// 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;
Vitaly Buka (NO REVIEWS) 2013/06/14 19:27:07 always initialize variables if it has not default
maksymb 2013/06/14 22:17:14 Done.
+ if (!base::StringToUint(CommandLine::ForCurrentProcess()->
+ GetSwitchValueASCII("http-port"), &http_port_tmp))
Vitaly Buka (NO REVIEWS) 2013/06/14 19:27:07 use temp variable for CommandLine::ForCurrentProc
maksymb 2013/06/14 22:17:14 Done.
+ http_port_tmp = kDefaultHttpPort;
Vitaly Buka (NO REVIEWS) 2013/06/14 19:27:07 If you can't fit "if" in one line please use {} f
maksymb 2013/06/14 22:17:14 Done.
+
+ if (http_port_tmp > kuint32max) {
+ LOG(ERROR) << "Port " << http_port_tmp << " is too large (maximum is " <<
+ kDefaultHttpPort << "). Using default port.";
+ http_port_tmp = kDefaultHttpPort;
+ }
+
+ uint16 http_port = static_cast<uint16>(http_port_tmp);
+ VLOG(1) << "HTTP port for responses: " << http_port;
Vitaly Buka (NO REVIEWS) 2013/06/14 19:27:07 you can do that without http_port variable
maksymb 2013/06/14 22:17:14 Done.
+ return http_port;
+}
+
+uint16 ReadTtlFromCommandLine() {
+ uint32 ttl;
Vitaly Buka (NO REVIEWS) 2013/06/14 19:27:07 initialize
maksymb 2013/06/14 22:17:14 Done.
+ if (!base::StringToUint(CommandLine::ForCurrentProcess()->
+ GetSwitchValueASCII("ttl"), &ttl))
Vitaly Buka (NO REVIEWS) 2013/06/14 19:27:07 same
maksymb 2013/06/14 22:17:14 Done.
+ ttl = kDefaultTTL;
Vitaly Buka (NO REVIEWS) 2013/06/14 19:27:07 same for {}
maksymb 2013/06/14 22:17:14 Done.
+
+ 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) {
Vitaly Buka (NO REVIEWS) 2013/06/14 19:27:07 Header fits one line
maksymb 2013/06/14 22:17:14 Done.
+ struct ifaddrs *ifaddr;
Vitaly Buka (NO REVIEWS) 2013/06/14 19:27:07 uninitialized pointer and datastructor char host[
maksymb 2013/06/14 22:17:14 Done.
+ char host[NI_MAXHOST];
+
+ net::IPAddressNumber result; // |result| is empty if no interface found.
+ if (getifaddrs(&ifaddr) == -1) {
+ LOG(ERROR) << "getifaddrs() failed";
+ return result;
+ }
+
+ // Browse all interfaces.
+ for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
Vitaly Buka (NO REVIEWS) 2013/06/14 19:27:07 don't need struct here
Vitaly Buka (NO REVIEWS) 2013/06/14 19:27:07 ifa != NULL -> ifa
maksymb 2013/06/14 22:17:14 Done.
maksymb 2013/06/14 22:17:14 Done.
+ if (ifa->ifa_addr == NULL)
+ continue;
+
+ // Get additional info about current interface.
+ int rv = getnameinfo(ifa->ifa_addr,
Vitaly Buka (NO REVIEWS) 2013/06/14 19:27:07 missaligned
maksymb 2013/06/14 22:17:14 Done.
+ sizeof(struct sockaddr_in),
Vitaly Buka (NO REVIEWS) 2013/06/14 19:27:07 sizeof(*ifa->ifa_addr)
maksymb 2013/06/14 22:17:14 Done.
+ host,
+ NI_MAXHOST,
+ NULL,
+ 0,
+ NI_NUMERICHOST);
+
+ if (ifa->ifa_addr->sa_family == AF_INET) {
+ // Find interface we need.
+ if (interface_name == NULL ? strcmp(ifa->ifa_name, "lo") != 0
+ : strcmp(ifa->ifa_name, interface_name) == 0) {
Vitaly Buka (NO REVIEWS) 2013/06/14 19:27:07 just "interface_name ? " and add () in ? (...) :
maksymb 2013/06/14 22:17:14 Done.
+ if (rv != 0) {
+ LOG(ERROR) << "getnameinfo() failed: " << gai_strerror(rv);
+ break;
+ }
+
+ net::ParseIPLiteralToNumber(host, &result);
+ }
+ }
+ }
+
+ // Erase memory under list of interfaces.
+ freeifaddrs(ifaddr);
+ 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(2) << "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();
+}
+

Powered by Google App Engine
This is Rietveld 408576698