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