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

Side by Side 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: Reindentation of function calls. 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 unified diff | Download patch
OLDNEW
(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 <ifaddrs.h>
8 #include <netdb.h>
9
10 #include "base/command_line.h"
11 #include "base/strings/string_number_conversions.h"
12
13 namespace {
14
15 const char* kServiceNamePrefix = "first_privet_device";
16 const char* kServiceDomainName = "my.privet.device.local";
gene 2013/06/15 02:13:23 "my.privet.device.local" -> "my-privet-device.loca
maksymb 2013/06/18 01:14:48 Done.
17
18 const uint16 kDefaultTTL = 60*60;
19 const uint16 kDefaultHttpPort = 10101;
20
21 uint16 ReadHttpPortFromCommandLine() {
22 uint32 http_port_tmp = kDefaultHttpPort;
23
24 std::string http_port_string_tmp =
25 CommandLine::ForCurrentProcess()->GetSwitchValueASCII("http-port");
26 base::StringToUint(http_port_string_tmp, &http_port_tmp);
27
28 if (http_port_tmp > kuint32max) {
29 LOG(ERROR) << "Port " << http_port_tmp << " is too large (maximum is " <<
30 kDefaultHttpPort << "). Using default port.";
31
32 http_port_tmp = kDefaultHttpPort;
33 }
34
35 VLOG(1) << "HTTP port for responses: " << http_port_tmp;
36 return static_cast<uint16>(http_port_tmp);
37 }
38
39 uint16 ReadTtlFromCommandLine() {
40 uint32 ttl = kDefaultTTL;
41
42 base::StringToUint(
43 CommandLine::ForCurrentProcess()->GetSwitchValueASCII("ttl"), &ttl);
44
45 VLOG(1) << "TTL for announcements: " << ttl;
46 return ttl;
47 }
48
49 // Returns pointer to local IP address number. Examples for interface name
50 // are: "wlan0", "eth0", NULL (find first acceptable except "lo").
51 net::IPAddressNumber GetLocalIp(const char* interface_name) {
52 net::IPAddressNumber result; // |result| is empty if no interface found.
53
54 ifaddrs* first_interface = NULL;
55 if (getifaddrs(&first_interface) == -1) {
56 LOG(ERROR) << "getifaddrs() failed";
57 return result;
58 }
59
60 // Browse all interfaces.
61 char host[NI_MAXHOST] = {0};
62 for (ifaddrs* interface = first_interface; interface;
63 interface = interface->ifa_next) {
64 if (!interface->ifa_addr)
65 continue;
66
67 // Get additional info about current interface.
68 int rv = getnameinfo(interface->ifa_addr, sizeof(*interface->ifa_addr),
69 host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
70
71 if (interface->ifa_addr->sa_family == AF_INET) {
72 // Find interface we need.
73 if (interface_name ? (strcmp(interface->ifa_name, interface_name) == 0)
74 : (strcmp(interface->ifa_name, "lo") != 0)) {
75 if (rv != 0) {
76 LOG(ERROR) << "getnameinfo() failed: " << gai_strerror(rv);
77 break;
78 }
79
80 net::ParseIPLiteralToNumber(host, &result);
81 }
82 }
83 }
84
85 // Erase memory under list of interfaces.
86 freeifaddrs(first_interface);
87 return result;
88 }
89
90 } // namespace
91
92 Printer::Printer() : initialized_(false) {
93 }
94
95 Printer::~Printer() {
96 Stop();
97 }
98
99 bool Printer::Start() {
100 if (initialized_)
101 return true;
102
103 // TODO(maksymb): Add possibility to control interface via command line args.
104 net::IPAddressNumber ip = GetLocalIp(NULL);
105 if (ip.empty()) {
106 LOG(ERROR) << "No local IP found. Cannot start printer.";
107 return false;
108 }
109 VLOG(1) << "Local address: " << net::IPAddressToString(ip);
110
111 // Starting DNS-SD server.
112 initialized_ = dns_server_.Start(
113 new PrivetDnsResponseBuilderFactory(kServiceNamePrefix,
114 kServiceDomainName,
115 ip, ReadHttpPortFromCommandLine()),
116 ReadTtlFromCommandLine());
117 return initialized_;
118 }
119
120 void Printer::Stop() {
121 if (!initialized_)
122 return;
123
124 dns_server_.Shutdown();
125 }
126
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698