blob: ed7cbe09434ff0231e2c54cfbfa2a5fe9428ca60 [file] [log] [blame]
Darin Petkove6ca3202012-10-19 14:49:56 +02001// 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
10extern "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 Petkov95f317f2012-10-22 13:37:43 +020021#include "shill/l2tp_ipsec_driver.h"
22#include "shill/rpc_task.h"
Darin Petkove6ca3202012-10-19 14:49:56 +020023#include "shill/shims/environment.h"
24#include "shill/shims/task_proxy.h"
25
26using std::map;
27using std::string;
28
29namespace shill {
30
31namespace shims {
32
33base::AtExitManager *PPP::exit_manager_ = NULL;
34
35PPP::PPP() {}
36
37PPP::~PPP() {}
38
39void 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
49void PPP::Stop() {
50 LOG(INFO) << "PPP stopped.";
51 delete exit_manager_;
52 exit_manager_ = NULL;
53}
54
55bool 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
65void 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 Petkov95f317f2012-10-22 13:37:43 +020072 dict[kL2TPIPSecInterfaceName] = ifname;
73 dict[kL2TPIPSecInternalIP4Address] =
74 ConvertIPToText(&ipcp_gotoptions[0].ouraddr);
75 dict[kL2TPIPSecExternalIP4Address] =
76 ConvertIPToText(&ipcp_hisoptions[0].hisaddr);
Darin Petkove6ca3202012-10-19 14:49:56 +020077 if (ipcp_gotoptions[0].default_route) {
Darin Petkov95f317f2012-10-22 13:37:43 +020078 dict[kL2TPIPSecGatewayAddress] = dict[kL2TPIPSecExternalIP4Address];
Darin Petkove6ca3202012-10-19 14:49:56 +020079 }
80 if (ipcp_gotoptions[0].dnsaddr[0]) {
Darin Petkov95f317f2012-10-22 13:37:43 +020081 dict[kL2TPIPSecDNS1] = ConvertIPToText(&ipcp_gotoptions[0].dnsaddr[0]);
Darin Petkove6ca3202012-10-19 14:49:56 +020082 }
83 if (ipcp_gotoptions[0].dnsaddr[1]) {
Darin Petkov95f317f2012-10-22 13:37:43 +020084 dict[kL2TPIPSecDNS2] = ConvertIPToText(&ipcp_gotoptions[0].dnsaddr[1]);
Darin Petkove6ca3202012-10-19 14:49:56 +020085 }
86 string lns_address;
87 if (Environment::GetInstance()->GetVariable("LNS_ADDRESS", &lns_address)) {
Darin Petkov95f317f2012-10-22 13:37:43 +020088 dict[kL2TPIPSecLNSAddress] = lns_address;
Darin Petkove6ca3202012-10-19 14:49:56 +020089 }
90 if (CreateProxy()) {
Darin Petkov95f317f2012-10-22 13:37:43 +020091 proxy_->Notify(kL2TPIPSecReasonConnect, dict);
Darin Petkove6ca3202012-10-19 14:49:56 +020092 DestroyProxy();
93 }
94}
95
96void PPP::OnDisconnect() {
97 LOG(INFO) << __func__;
98 if (CreateProxy()) {
99 map<string, string> dict;
Darin Petkov95f317f2012-10-22 13:37:43 +0200100 proxy_->Notify(kL2TPIPSecReasonDisconnect, dict);
Darin Petkove6ca3202012-10-19 14:49:56 +0200101 DestroyProxy();
102 }
103}
104
105bool PPP::CreateProxy() {
106 Environment *environment = Environment::GetInstance();
107 string service, path;
Darin Petkov95f317f2012-10-22 13:37:43 +0200108 if (!environment->GetVariable(shill::kRPCTaskServiceVariable, &service) ||
109 !environment->GetVariable(shill::kRPCTaskPathVariable, &path)) {
Darin Petkove6ca3202012-10-19 14:49:56 +0200110 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 Petkov95f317f2012-10-22 13:37:43 +0200118 LOG(INFO) << "Task proxy created: " << service << " - " << path;
Darin Petkove6ca3202012-10-19 14:49:56 +0200119 return true;
120}
121
122void 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
131string 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