Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 1 | #include "slp.hpp" |
| 2 | #include "slp_meta.hpp" |
| 3 | #include "slp_server.hpp" |
| 4 | #include "sock_channel.hpp" |
| 5 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 6 | #include <algorithm> |
| 7 | #include <iomanip> |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 8 | |
| 9 | /* Call Back for the sd event loop */ |
Patrick Williams | 3964d55 | 2023-04-04 20:48:49 -0500 | [diff] [blame] | 10 | static int requestHandler(sd_event_source* /*es*/, int fd, uint32_t /*revents*/, |
| 11 | void* /*userdata*/) |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 12 | { |
| 13 | int rc = slp::SUCCESS; |
| 14 | timeval tv{slp::TIMEOUT, 0}; |
| 15 | udpsocket::Channel channel(fd, tv); |
| 16 | std::vector<uint8_t> recvBuff; |
| 17 | slp::Message req; |
| 18 | std::vector<uint8_t> resp; |
| 19 | // Read the packet |
| 20 | std::tie(rc, recvBuff) = channel.read(); |
| 21 | |
| 22 | if (rc < 0) |
| 23 | { |
Ratan Gupta | 0d3e9e3 | 2017-02-10 22:27:12 +0530 | [diff] [blame] | 24 | std::cerr << "SLP Error in Read : " << std::hex << rc << "\n"; |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 25 | return rc; |
| 26 | } |
| 27 | |
Andrew Geissler | eebd081 | 2024-05-24 10:05:27 -0500 | [diff] [blame^] | 28 | // This code currently assume a maximum of 255 bytes in a receive |
| 29 | // or response message. Enforce that here. |
| 30 | if (recvBuff.size() > slp::MAX_LEN) |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 31 | { |
Andrew Geissler | eebd081 | 2024-05-24 10:05:27 -0500 | [diff] [blame^] | 32 | std::cerr << "Message size exceeds maximum allowed: " << recvBuff.size() |
| 33 | << " / " << slp::MAX_LEN << std::endl; |
Ratan Gupta | 0d3e9e3 | 2017-02-10 22:27:12 +0530 | [diff] [blame] | 34 | |
Andrew Geissler | eebd081 | 2024-05-24 10:05:27 -0500 | [diff] [blame^] | 35 | rc = static_cast<uint8_t>(slp::Error::PARSE_ERROR); |
| 36 | } |
| 37 | else |
| 38 | { |
| 39 | switch (recvBuff[0]) |
| 40 | { |
| 41 | case slp::VERSION_2: |
| 42 | { |
| 43 | // Parse the buffer and construct the req object |
| 44 | std::tie(rc, req) = slp::parser::parseBuffer(recvBuff); |
| 45 | if (!rc) |
| 46 | { |
| 47 | // Passing the req object to handler to serve it |
| 48 | std::tie(rc, resp) = slp::handler::processRequest(req); |
| 49 | } |
| 50 | break; |
| 51 | } |
| 52 | default: |
| 53 | std::cout << "SLP Unsupported Request Version=" |
| 54 | << (int)recvBuff[0] << "\n"; |
| 55 | |
| 56 | rc = static_cast<uint8_t>(slp::Error::VER_NOT_SUPPORTED); |
| 57 | break; |
| 58 | } |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 59 | } |
| 60 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 61 | // if there was error during Parsing of request |
| 62 | // or processing of request then handle the error. |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 63 | if (rc) |
| 64 | { |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 65 | resp = slp::handler::processError(req, rc); |
| 66 | } |
| 67 | |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 68 | channel.write(resp); |
| 69 | return slp::SUCCESS; |
| 70 | } |
| 71 | |
Patrick Williams | 3964d55 | 2023-04-04 20:48:49 -0500 | [diff] [blame] | 72 | int main() |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 73 | { |
| 74 | slp::udp::Server svr(slp::PORT, requestHandler); |
| 75 | return svr.run(); |
| 76 | } |