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

Side by Side Diff: remoting/host/gnubby_advertiser.cc

Issue 138753005: Add gnubby authentication to remoting host (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Actually add braces Created 6 years, 10 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 2014 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 "remoting/host/gnubby_advertiser.h"
6
7 #include <string>
8
9 #include "base/bind.h"
10 #include "base/strings/stringprintf.h"
11 #include "net/base/address_list.h"
12 #include "net/base/io_buffer.h"
13 #include "net/base/ip_endpoint.h"
14 #include "net/base/net_errors.h"
15 #include "net/base/net_util.h"
16 #include "remoting/base/logging.h"
17
18 namespace {
19
20 const int kLocalGnubbydPort = 1817;
21 const char kGnubbydProxyHost[] = "localhost";
22 const char kLocalGnubbydAddress[] = "127.0.0.1";
23
24 } // namespace
25
26 namespace remoting {
27
28 GnubbyAdvertiser::GnubbyAdvertiser(
29 net::ClientSocketFactory* client_socket_factory)
30 : client_socket_factory_(client_socket_factory),
31 proxy_port_(0) {
32
33 DCHECK(client_socket_factory_);
34
35 in_buffer_ = new net::IOBufferWithSize(64);
36 }
37
38 void GnubbyAdvertiser::Advertise(int proxy_port) {
39 DCHECK(CalledOnValidThread());
40 proxy_port_ = proxy_port;
41
42 // Increment reference to ensure object isn't deleted before final callback.
43 AddRef();
44
45 net::IPAddressNumber gnubbyd_ip;
46 if (!net::ParseIPLiteralToNumber(kLocalGnubbydAddress, &gnubbyd_ip)) {
47 Close(net::ERR_FAILED);
48 }
49
50 net::AddressList addresses(net::IPEndPoint(gnubbyd_ip, kLocalGnubbydPort));
51 net::NetLog::Source source;
52
53 socket_ = client_socket_factory_->CreateTransportClientSocket(
54 addresses, NULL, source);
55 int result = socket_->Connect(
56 base::Bind(&GnubbyAdvertiser::OnConnect, base::Unretained(this)));
57 if (result != net::ERR_IO_PENDING) {
58 OnConnect(result);
59 }
60 }
61
62 GnubbyAdvertiser::~GnubbyAdvertiser() {}
63
64 void GnubbyAdvertiser::Close(int result) {
65 DCHECK(CalledOnValidThread());
66
67 socket_->Disconnect();
68 // All done. Decrement reference to balance the increment in Advertise.
69 Release();
70 }
71
72 void GnubbyAdvertiser::OnConnect(int result) {
73 DCHECK(CalledOnValidThread());
74
75 if (result == net::OK) {
76 Write();
77 } else {
78 HOST_LOG << "Failed to connect to gnubbyd: " << result;
79 Close(result);
80 }
81 }
82
83 void GnubbyAdvertiser::Write() {
84 DCHECK(CalledOnValidThread());
85
86 std::string listener =
87 base::StringPrintf("%s: %d\r\n\r\n", kGnubbydProxyHost, proxy_port_);
88 HOST_LOG << "Advertising [" << listener << "] with local gnubbyd";
89
90 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(listener);
91 int result = socket_->Write(
92 buffer.get(),
93 buffer->size(),
94 base::Bind(&GnubbyAdvertiser::OnWrite, base::Unretained(this)));
95 if (result != net::ERR_IO_PENDING) {
96 OnWrite(result);
97 }
98 }
99
100 void GnubbyAdvertiser::OnWrite(int result) {
101 DCHECK(CalledOnValidThread());
102
103 if (result > 0) {
104 Read();
105 } else {
106 if (result < 0) {
107 HOST_LOG << "Failed to write to gnubbyd: " << result;
108 }
109 Close(result);
110 }
111 }
112
113 void GnubbyAdvertiser::Read() {
114 DCHECK(CalledOnValidThread());
115
116 int result = socket_->Read(
117 in_buffer_.get(),
118 in_buffer_->size(),
119 base::Bind(&GnubbyAdvertiser::OnRead, base::Unretained(this)));
120 if (result != net::ERR_IO_PENDING) {
121 OnRead(result);
122 }
123 }
124
125 void GnubbyAdvertiser::OnRead(int result) {
126 DCHECK(CalledOnValidThread());
127
128 // The advertiser is done once a reply has been received.
129 Close(result);
130 }
131
132 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698