1 /*
2  * Copyright (C) 2014 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 NETD_SERVER_NETWORK_H
18 #define NETD_SERVER_NETWORK_H
19 
20 #include "NetdConstants.h"
21 
22 #include <set>
23 #include <string>
24 
25 namespace android {
26 namespace net {
27 
28 // A Network represents a collection of interfaces participating as a single administrative unit.
29 class Network {
30 public:
31     enum Type {
32         DUMMY,
33         LOCAL,
34         PHYSICAL,
35         VIRTUAL,
36     };
37 
38     // You MUST ensure that no interfaces are still assigned to this network, say by calling
39     // clearInterfaces(), before deleting it. This is because interface removal may fail. If we
40     // automatically removed interfaces in the destructor, you wouldn't know if it failed.
41     virtual ~Network();
42 
43     virtual Type getType() const = 0;
44     unsigned getNetId() const;
45 
46     bool hasInterface(const std::string& interface) const;
47     const std::set<std::string>& getInterfaces() const;
48 
49     // These return 0 on success or negative errno on failure.
50     virtual int addInterface(const std::string& interface) WARN_UNUSED_RESULT = 0;
51     virtual int removeInterface(const std::string& interface) WARN_UNUSED_RESULT = 0;
52     int clearInterfaces() WARN_UNUSED_RESULT;
53 
54     std::string toString() const;
55 
56 protected:
57     explicit Network(unsigned netId);
58 
59     const unsigned mNetId;
60     std::set<std::string> mInterfaces;
61 };
62 
63 }  // namespace net
64 }  // namespace android
65 
66 #endif  // NETD_SERVER_NETWORK_H
67