forked from ibm-openbmc/phosphor-networkd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipaddress.cpp
119 lines (104 loc) · 3.43 KB
/
ipaddress.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "ipaddress.hpp"
#include "ethernet_interface.hpp"
#include "network_manager.hpp"
#include "util.hpp"
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/lg2.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
#include <stdexcept>
#include <string>
#include <string_view>
namespace phosphor
{
namespace network
{
using namespace phosphor::logging;
using namespace sdbusplus::xyz::openbmc_project::Common::Error;
using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
using Reason = xyz::openbmc_project::Common::NotAllowed::REASON;
static auto makeObjPath(std::string_view root, stdplus::SubnetAny addr)
{
auto ret = sdbusplus::message::object_path(std::string(root));
stdplus::ToStrHandle<stdplus::ToStr<stdplus::SubnetAny>> tsh;
ret /= tsh(addr);
return ret;
}
template <typename T>
struct Proto
{};
template <>
struct Proto<stdplus::In4Addr>
{
static inline constexpr auto value = IP::Protocol::IPv4;
};
template <>
struct Proto<stdplus::In6Addr>
{
static inline constexpr auto value = IP::Protocol::IPv6;
};
IPAddress::IPAddress(sdbusplus::bus_t& bus, std::string_view objRoot,
stdplus::PinnedRef<EthernetInterface> parent,
stdplus::SubnetAny addr, AddressOrigin origin) :
IPAddress(bus, makeObjPath(objRoot, addr), parent, addr, origin)
{}
IPAddress::IPAddress(sdbusplus::bus_t& bus,
sdbusplus::message::object_path objPath,
stdplus::PinnedRef<EthernetInterface> parent,
stdplus::SubnetAny addr, AddressOrigin origin) :
IPIfaces(bus, objPath.str.c_str(), IPIfaces::action::defer_emit),
parent(parent), objPath(std::move(objPath))
{
IP::address(stdplus::toStr(addr.getAddr()), true);
IP::prefixLength(addr.getPfx(), true);
IP::type(std::visit([](auto v) { return Proto<decltype(v)>::value; },
addr.getAddr()),
true);
IP::origin(origin, true);
emit_object_added();
}
std::string IPAddress::address(std::string /*ipAddress*/)
{
elog<NotAllowed>(Reason("Property update is not allowed"));
}
uint8_t IPAddress::prefixLength(uint8_t /*value*/)
{
elog<NotAllowed>(Reason("Property update is not allowed"));
}
std::string IPAddress::gateway(std::string /*gateway*/)
{
elog<NotAllowed>(Reason("Property update is not allowed"));
}
IP::Protocol IPAddress::type(IP::Protocol /*type*/)
{
elog<NotAllowed>(Reason("Property update is not allowed"));
}
IP::AddressOrigin IPAddress::origin(IP::AddressOrigin /*origin*/)
{
elog<NotAllowed>(Reason("Property update is not allowed"));
}
void IPAddress::delete_()
{
if (origin() != IP::AddressOrigin::Static)
{
lg2::error("Tried to delete a non-static address {NET_IP} prefix "
"{NET_PFX} interface {NET_INTF}",
"NET_IP", address(), "NET_PFX", prefixLength(), "NET_INTF",
parent.get().interfaceName());
elog<NotAllowed>(Reason("Not allowed to delete a non-static address"));
}
std::unique_ptr<IPAddress> ptr;
auto& addrs = parent.get().addrs;
for (auto it = addrs.begin(); it != addrs.end(); ++it)
{
if (it->second.get() == this)
{
ptr = std::move(it->second);
addrs.erase(it);
break;
}
}
parent.get().writeConfigurationFile();
parent.get().manager.get().reloadConfigs();
}
} // namespace network
} // namespace phosphor