blob: 85c4f14d66b66ac7097fc45529ffddddb139b908 [file] [log] [blame]
Eric Shienbrood3e20a232012-02-16 11:35:56 -05001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewartf65320c2011-10-13 14:34:52 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/async_connection.h"
6
Eric Shienbrood3e20a232012-02-16 11:35:56 -05007#include <base/bind.h>
Paul Stewartf65320c2011-10-13 14:34:52 -07008#include <errno.h>
9#include <netinet/in.h>
10
11#include <string>
12
13#include "shill/event_dispatcher.h"
14#include "shill/ip_address.h"
15#include "shill/sockets.h"
16
Eric Shienbrood3e20a232012-02-16 11:35:56 -050017using base::Bind;
18using base::Callback;
19using base::Unretained;
Paul Stewartf65320c2011-10-13 14:34:52 -070020using std::string;
21
22namespace shill {
23
24AsyncConnection::AsyncConnection(const string &interface_name,
25 EventDispatcher *dispatcher,
26 Sockets *sockets,
Eric Shienbrood3e20a232012-02-16 11:35:56 -050027 const Callback<void(bool, int)> &callback)
Paul Stewartf65320c2011-10-13 14:34:52 -070028 : interface_name_(interface_name),
29 dispatcher_(dispatcher),
30 sockets_(sockets),
31 callback_(callback),
32 fd_(-1),
33 connect_completion_callback_(
Eric Shienbrood3e20a232012-02-16 11:35:56 -050034 Bind(&AsyncConnection::OnConnectCompletion, Unretained(this))) { }
Paul Stewartf65320c2011-10-13 14:34:52 -070035
36AsyncConnection::~AsyncConnection() {
37 Stop();
38}
39
40bool AsyncConnection::Start(const IPAddress &address, int port) {
41 DCHECK(fd_ < 0);
42
43 fd_ = sockets_->Socket(PF_INET, SOCK_STREAM, 0);
44 if (fd_ < 0 ||
45 sockets_->SetNonBlocking(fd_) < 0) {
46 error_ = sockets_->ErrorString();
47 PLOG(ERROR) << "Async socket setup failed";
48 Stop();
49 return false;
50 }
51
52 if (!interface_name_.empty() &&
53 sockets_->BindToDevice(fd_, interface_name_) < 0) {
54 error_ = sockets_->ErrorString();
55 PLOG(ERROR) << "Async socket failed to bind to device";
56 Stop();
57 return false;
58 }
59
60 struct sockaddr_in iaddr;
61 CHECK_EQ(sizeof(iaddr.sin_addr.s_addr), address.GetLength());
62
63 memset(&iaddr, 0, sizeof(iaddr));
64 iaddr.sin_family = AF_INET;
65 memcpy(&iaddr.sin_addr.s_addr, address.address().GetConstData(),
66 sizeof(iaddr.sin_addr.s_addr));
67 iaddr.sin_port = htons(port);
68
69 socklen_t addrlen = sizeof(iaddr);
70 int ret = sockets_->Connect(fd_,
71 reinterpret_cast<struct sockaddr *>(&iaddr),
72 addrlen);
73 if (ret == 0) {
Eric Shienbrood3e20a232012-02-16 11:35:56 -050074 callback_.Run(true, fd_); // Passes ownership
Paul Stewartf65320c2011-10-13 14:34:52 -070075 fd_ = -1;
76 return true;
77 }
78
79 if (sockets_->Error() != EINPROGRESS) {
80 error_ = sockets_->ErrorString();
81 PLOG(ERROR) << "Async socket connection failed";
82 Stop();
83 return false;
84 }
85
86 connect_completion_handler_.reset(
87 dispatcher_->CreateReadyHandler(fd_,
88 IOHandler::kModeOutput,
Eric Shienbrood3e20a232012-02-16 11:35:56 -050089 connect_completion_callback_));
Paul Stewartf65320c2011-10-13 14:34:52 -070090 error_ = string();
91
92 return true;
93}
94
95void AsyncConnection::Stop() {
96 connect_completion_handler_.reset();
97 if (fd_ >= 0) {
98 sockets_->Close(fd_);
99 fd_ = -1;
100 }
101}
102
103void AsyncConnection::OnConnectCompletion(int fd) {
104 CHECK_EQ(fd_, fd);
Paul Stewart8585b202012-09-21 14:03:01 -0700105 bool success = false;
106 int returned_fd = -1;
Paul Stewartf65320c2011-10-13 14:34:52 -0700107
108 if (sockets_->GetSocketError(fd_) != 0) {
109 error_ = sockets_->ErrorString();
110 PLOG(ERROR) << "Async GetSocketError returns failure";
Paul Stewartf65320c2011-10-13 14:34:52 -0700111 } else {
Paul Stewart8585b202012-09-21 14:03:01 -0700112 returned_fd = fd_;
Paul Stewartf65320c2011-10-13 14:34:52 -0700113 fd_ = -1;
Paul Stewart8585b202012-09-21 14:03:01 -0700114 success = true;
Paul Stewartf65320c2011-10-13 14:34:52 -0700115 }
116 Stop();
Paul Stewart8585b202012-09-21 14:03:01 -0700117
118 // Run the callback last, since it may end up freeing this instance.
119 callback_.Run(success, returned_fd); // Passes ownership
Paul Stewartf65320c2011-10-13 14:34:52 -0700120}
121
122} // namespace shill