blob: 8417f340a17121f1bc39b0e83f292e1690705dd1 [file] [log] [blame]
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001/*
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
Bernie Innocenti762dcf42019-06-14 19:52:49 +090017#pragma once
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070018
19#include "NetdConstants.h"
20
21#include <set>
22#include <string>
23
Bernie Innocenti762dcf42019-06-14 19:52:49 +090024namespace android::net {
Lorenzo Colitti7035f222017-02-13 18:29:00 +090025
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070026// A Network represents a collection of interfaces participating as a single administrative unit.
27class Network {
28public:
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -070029 enum Type {
Lorenzo Colitti36679362015-02-25 10:26:19 +090030 DUMMY,
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070031 LOCAL,
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -070032 PHYSICAL,
33 VIRTUAL,
34 };
35
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070036 // You MUST ensure that no interfaces are still assigned to this network, say by calling
37 // clearInterfaces(), before deleting it. This is because interface removal may fail. If we
38 // automatically removed interfaces in the destructor, you wouldn't know if it failed.
39 virtual ~Network();
40
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -070041 virtual Type getType() const = 0;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070042 unsigned getNetId() const;
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -070043
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070044 bool hasInterface(const std::string& interface) const;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070045 const std::set<std::string>& getInterfaces() const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070046
47 // These return 0 on success or negative errno on failure.
Bernie Innocenti762dcf42019-06-14 19:52:49 +090048 [[nodiscard]] virtual int addInterface(const std::string& interface) = 0;
49 [[nodiscard]] virtual int removeInterface(const std::string& interface) = 0;
50 [[nodiscard]] int clearInterfaces();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070051
Erik Kline2d3a1632016-03-15 16:33:48 +090052 std::string toString() const;
53
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070054protected:
Sreeram Ramachandran89dad012014-07-02 10:09:49 -070055 explicit Network(unsigned netId);
56
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070057 const unsigned mNetId;
58 std::set<std::string> mInterfaces;
59};
60
Bernie Innocenti762dcf42019-06-14 19:52:49 +090061} // namespace android::net