2 #include "radproto/packet_codes.h"
7 using boost::system::error_code;
9 Server::Server(boost::asio::io_service& io_service, const std::string& secret, uint16_t port, const std::string& filePath)
10 : m_radius(io_service, secret, port),
11 m_dictionaries(filePath)
16 void Server::startReceive()
18 m_radius.asyncReceive([this](const auto& error, const auto& packet, const boost::asio::ip::udp::endpoint& source){ handleReceive(error, packet, source); });
21 RadProto::Packet Server::makeResponse(const RadProto::Packet& request)
23 std::vector<RadProto::Attribute*> attributes;
24 attributes.push_back(new RadProto::String(m_dictionaries.attributeCode("User-Name"), "test"));
25 attributes.push_back(new RadProto::Integer(m_dictionaries.attributeCode("NAS-Port"), 20));
26 std::array<uint8_t, 4> address {127, 104, 22, 17};
27 attributes.push_back(new RadProto::IpAddress(m_dictionaries.attributeCode("NAS-IP-Address"), address));
28 std::vector<uint8_t> bytes {'1', '2', '3', 'a', 'b', 'c'};
29 attributes.push_back(new RadProto::Bytes(m_dictionaries.attributeCode("Callback-Number"), bytes));
30 std::vector<uint8_t> chapPassword {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
31 attributes.push_back(new RadProto::ChapPassword(m_dictionaries.attributeCode("CHAP-Password"), 1, chapPassword));
33 std::vector<RadProto::VendorSpecific> vendorSpecific;
34 std::vector<uint8_t> vendorValue {0, 0, 0, 3};
35 vendorSpecific.push_back(RadProto::VendorSpecific(m_dictionaries.vendorCode("Dlink"), m_dictionaries.vendorAttributeCode("Dlink", "Dlink-User-Level"), vendorValue));
37 if (request.type() == RadProto::ACCESS_REQUEST)
38 return RadProto::Packet(RadProto::ACCESS_ACCEPT, request.id(), request.auth(), attributes, vendorSpecific);
40 return RadProto::Packet(RadProto::ACCESS_REJECT, request.id(), request.auth(), attributes, vendorSpecific);
43 void Server::handleSend(const error_code& ec)
46 std::cout << "Error asyncSend: " << ec.message() << "\n";
51 void Server::handleReceive(const error_code& error, const std::optional<RadProto::Packet>& packet, const boost::asio::ip::udp::endpoint& source)
55 std::cout << "Error asyncReceive: " << error.message() << "\n";
59 if (packet == std::nullopt)
61 std::cout << "Error asyncReceive: the request packet is missing\n";
66 m_radius.asyncSend(makeResponse(*packet), source, [this](const auto& ec){ handleSend(ec); });