1 //
2 // Copyright (C) 2015 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef SHILL_PPPOE_PPPOE_SERVICE_H_
18 #define SHILL_PPPOE_PPPOE_SERVICE_H_
19 
20 #include <map>
21 #include <string>
22 
23 #include <gtest/gtest_prod.h>
24 
25 #include "shill/ethernet/ethernet.h"
26 #include "shill/ethernet/ethernet_service.h"
27 #include "shill/refptr_types.h"
28 #include "shill/rpc_task.h"
29 
30 namespace shill {
31 
32 class ControlInterface;
33 class Error;
34 class EventDispatcher;
35 class ExternalTask;
36 class Manager;
37 class Metrics;
38 class PPPDeviceFactory;
39 class ProcessManager;
40 class StoreInterface;
41 
42 // PPPoEService is an EthernetService that manages PPPoE connectivity on a
43 // single Ethernet device.  To do this it spawns and manages pppd instances.
44 // When pppX interfaces are created in the course of a connection they are
45 // wrapped with a PPPDevice, and are made to SelectService the PPPoEService that
46 // created them.
47 class PPPoEService : public EthernetService, public RPCTaskDelegate {
48  public:
49   PPPoEService(ControlInterface* control_interface,
50                EventDispatcher* dispatcher,
51                Metrics* metrics,
52                Manager* manager,
53                base::WeakPtr<Ethernet> ethernet);
54   ~PPPoEService() override;
55 
56   // Inherited from EthernetService.
57   void Connect(Error* error, const char* reason) override;
58   void Disconnect(Error* error, const char* reason) override;
59   bool Load(StoreInterface* storage) override;
60   bool Save(StoreInterface* storage) override;
61   bool Unload() override;
62 
63   // Inherited from Service.
64   std::string GetInnerDeviceRpcIdentifier() const override;
65 
66   // Inherited from RPCTaskDelegate.
67   void GetLogin(std::string* user, std::string* password) override;
68   void Notify(const std::string& reason,
69               const std::map<std::string, std::string>& dict) override;
70 
71  private:
72   friend class PPPoEServiceTest;
73   FRIEND_TEST(PPPoEServiceTest, Disconnect);
74   FRIEND_TEST(PPPoEServiceTest, OnPPPConnected);
75 
76   static const int kDefaultLCPEchoInterval;
77   static const int kDefaultLCPEchoFailure;
78   static const int kDefaultMaxAuthFailure;
79 
80   void OnPPPAuthenticating();
81   void OnPPPAuthenticated();
82   void OnPPPConnected(const std::map<std::string, std::string>& params);
83   void OnPPPDisconnected();
84   void OnPPPDied(pid_t pid, int exit);
85 
86   ControlInterface* control_interface_;
87   PPPDeviceFactory* ppp_device_factory_;
88   ProcessManager* process_manager_;
89 
90   std::string username_;
91   std::string password_;
92   int lcp_echo_interval_;
93   int lcp_echo_failure_;
94   int max_auth_failure_;
95 
96   bool authenticating_;
97   std::unique_ptr<ExternalTask> pppd_;
98   PPPDeviceRefPtr ppp_device_;
99 
100   base::WeakPtrFactory<PPPoEService> weak_ptr_factory_;
101 
102   DISALLOW_COPY_AND_ASSIGN(PPPoEService);
103 };
104 
105 }  // namespace shill
106 
107 #endif  // SHILL_PPPOE_PPPOE_SERVICE_H_
108