Darin Petkov | e6ca320 | 2012-10-19 14:49:56 +0200 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS 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 "shill/shims/ppp.h" |
| 6 | |
| 7 | #include <arpa/inet.h> |
| 8 | #include <netinet/in.h> |
| 9 | |
| 10 | extern "C" { |
| 11 | #include <pppd/fsm.h> |
| 12 | #include <pppd/ipcp.h> |
| 13 | } |
| 14 | |
| 15 | #include <base/at_exit.h> |
| 16 | #include <base/command_line.h> |
| 17 | #include <base/logging.h> |
| 18 | #include <chromeos/syslog_logging.h> |
| 19 | #include <dbus-c++/eventloop-integration.h> |
| 20 | |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 21 | #include "shill/l2tp_ipsec_driver.h" |
| 22 | #include "shill/rpc_task.h" |
Darin Petkov | e6ca320 | 2012-10-19 14:49:56 +0200 | [diff] [blame] | 23 | #include "shill/shims/environment.h" |
| 24 | #include "shill/shims/task_proxy.h" |
| 25 | |
| 26 | using std::map; |
| 27 | using std::string; |
| 28 | |
| 29 | namespace shill { |
| 30 | |
| 31 | namespace shims { |
| 32 | |
| 33 | base::AtExitManager *PPP::exit_manager_ = NULL; |
| 34 | |
| 35 | PPP::PPP() {} |
| 36 | |
| 37 | PPP::~PPP() {} |
| 38 | |
| 39 | void PPP::Start() { |
| 40 | if (exit_manager_) { |
| 41 | return; |
| 42 | } |
| 43 | exit_manager_ = new base::AtExitManager(); |
| 44 | CommandLine::Init(0, NULL); |
| 45 | chromeos::InitLog(chromeos::kLogToSyslog | chromeos::kLogHeader); |
| 46 | LOG(INFO) << "PPP started."; |
| 47 | } |
| 48 | |
| 49 | void PPP::Stop() { |
| 50 | LOG(INFO) << "PPP stopped."; |
| 51 | delete exit_manager_; |
| 52 | exit_manager_ = NULL; |
| 53 | } |
| 54 | |
| 55 | bool PPP::GetSecret(string *username, string *password) { |
| 56 | LOG(INFO) << __func__; |
| 57 | if (!CreateProxy()) { |
| 58 | return false; |
| 59 | } |
| 60 | bool success = proxy_->GetSecret(username, password); |
| 61 | DestroyProxy(); |
| 62 | return success; |
| 63 | } |
| 64 | |
| 65 | void PPP::OnConnect(const string &ifname) { |
| 66 | LOG(INFO) << __func__ << "(" << ifname << ")"; |
| 67 | if (!ipcp_gotoptions[0].ouraddr) { |
| 68 | LOG(ERROR) << "ouraddr not set."; |
| 69 | return; |
| 70 | } |
| 71 | map<string, string> dict; |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 72 | dict[kL2TPIPSecInterfaceName] = ifname; |
| 73 | dict[kL2TPIPSecInternalIP4Address] = |
| 74 | ConvertIPToText(&ipcp_gotoptions[0].ouraddr); |
| 75 | dict[kL2TPIPSecExternalIP4Address] = |
| 76 | ConvertIPToText(&ipcp_hisoptions[0].hisaddr); |
Darin Petkov | e6ca320 | 2012-10-19 14:49:56 +0200 | [diff] [blame] | 77 | if (ipcp_gotoptions[0].default_route) { |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 78 | dict[kL2TPIPSecGatewayAddress] = dict[kL2TPIPSecExternalIP4Address]; |
Darin Petkov | e6ca320 | 2012-10-19 14:49:56 +0200 | [diff] [blame] | 79 | } |
| 80 | if (ipcp_gotoptions[0].dnsaddr[0]) { |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 81 | dict[kL2TPIPSecDNS1] = ConvertIPToText(&ipcp_gotoptions[0].dnsaddr[0]); |
Darin Petkov | e6ca320 | 2012-10-19 14:49:56 +0200 | [diff] [blame] | 82 | } |
| 83 | if (ipcp_gotoptions[0].dnsaddr[1]) { |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 84 | dict[kL2TPIPSecDNS2] = ConvertIPToText(&ipcp_gotoptions[0].dnsaddr[1]); |
Darin Petkov | e6ca320 | 2012-10-19 14:49:56 +0200 | [diff] [blame] | 85 | } |
| 86 | string lns_address; |
| 87 | if (Environment::GetInstance()->GetVariable("LNS_ADDRESS", &lns_address)) { |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 88 | dict[kL2TPIPSecLNSAddress] = lns_address; |
Darin Petkov | e6ca320 | 2012-10-19 14:49:56 +0200 | [diff] [blame] | 89 | } |
| 90 | if (CreateProxy()) { |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 91 | proxy_->Notify(kL2TPIPSecReasonConnect, dict); |
Darin Petkov | e6ca320 | 2012-10-19 14:49:56 +0200 | [diff] [blame] | 92 | DestroyProxy(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void PPP::OnDisconnect() { |
| 97 | LOG(INFO) << __func__; |
| 98 | if (CreateProxy()) { |
| 99 | map<string, string> dict; |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 100 | proxy_->Notify(kL2TPIPSecReasonDisconnect, dict); |
Darin Petkov | e6ca320 | 2012-10-19 14:49:56 +0200 | [diff] [blame] | 101 | DestroyProxy(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | bool PPP::CreateProxy() { |
| 106 | Environment *environment = Environment::GetInstance(); |
| 107 | string service, path; |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 108 | if (!environment->GetVariable(shill::kRPCTaskServiceVariable, &service) || |
| 109 | !environment->GetVariable(shill::kRPCTaskPathVariable, &path)) { |
Darin Petkov | e6ca320 | 2012-10-19 14:49:56 +0200 | [diff] [blame] | 110 | LOG(ERROR) << "Environment variables not available."; |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | dispatcher_.reset(new DBus::BusDispatcher()); |
| 115 | DBus::default_dispatcher = dispatcher_.get(); |
| 116 | connection_.reset(new DBus::Connection(DBus::Connection::SystemBus())); |
| 117 | proxy_.reset(new TaskProxy(connection_.get(), path, service)); |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 118 | LOG(INFO) << "Task proxy created: " << service << " - " << path; |
Darin Petkov | e6ca320 | 2012-10-19 14:49:56 +0200 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | |
| 122 | void PPP::DestroyProxy() { |
| 123 | proxy_.reset(); |
| 124 | connection_.reset(); |
| 125 | DBus::default_dispatcher = NULL; |
| 126 | dispatcher_.reset(); |
| 127 | LOG(INFO) << "Task proxy destroyed."; |
| 128 | } |
| 129 | |
| 130 | // static |
| 131 | string PPP::ConvertIPToText(const void *addr) { |
| 132 | char text[INET_ADDRSTRLEN]; |
| 133 | inet_ntop(AF_INET, addr, text, INET_ADDRSTRLEN); |
| 134 | return text; |
| 135 | } |
| 136 | |
| 137 | } // namespace shims |
| 138 | |
| 139 | } // namespace shill |