xref: /openbmc/slpd-lite/main.cpp (revision eebd0815)
1309ac445SRatan Gupta #include "slp.hpp"
2309ac445SRatan Gupta #include "slp_meta.hpp"
3309ac445SRatan Gupta #include "slp_server.hpp"
4309ac445SRatan Gupta #include "sock_channel.hpp"
5309ac445SRatan Gupta 
6537ff140SPatrick Venture #include <algorithm>
7537ff140SPatrick Venture #include <iomanip>
8309ac445SRatan Gupta 
9309ac445SRatan Gupta /* Call Back for the sd event loop */
requestHandler(sd_event_source *,int fd,uint32_t,void *)103964d552SPatrick Williams static int requestHandler(sd_event_source* /*es*/, int fd, uint32_t /*revents*/,
113964d552SPatrick Williams                           void* /*userdata*/)
12309ac445SRatan Gupta {
13309ac445SRatan Gupta     int rc = slp::SUCCESS;
14309ac445SRatan Gupta     timeval tv{slp::TIMEOUT, 0};
15309ac445SRatan Gupta     udpsocket::Channel channel(fd, tv);
16309ac445SRatan Gupta     std::vector<uint8_t> recvBuff;
17309ac445SRatan Gupta     slp::Message req;
18309ac445SRatan Gupta     std::vector<uint8_t> resp;
19309ac445SRatan Gupta     // Read the packet
20309ac445SRatan Gupta     std::tie(rc, recvBuff) = channel.read();
21309ac445SRatan Gupta 
22309ac445SRatan Gupta     if (rc < 0)
23309ac445SRatan Gupta     {
240d3e9e33SRatan Gupta         std::cerr << "SLP Error in Read : " << std::hex << rc << "\n";
25309ac445SRatan Gupta         return rc;
26309ac445SRatan Gupta     }
27309ac445SRatan Gupta 
28*eebd0815SAndrew Geissler     // This code currently assume a maximum of 255 bytes in a receive
29*eebd0815SAndrew Geissler     // or response message. Enforce that here.
30*eebd0815SAndrew Geissler     if (recvBuff.size() > slp::MAX_LEN)
31*eebd0815SAndrew Geissler     {
32*eebd0815SAndrew Geissler         std::cerr << "Message size exceeds maximum allowed: " << recvBuff.size()
33*eebd0815SAndrew Geissler                   << " / " << slp::MAX_LEN << std::endl;
34*eebd0815SAndrew Geissler 
35*eebd0815SAndrew Geissler         rc = static_cast<uint8_t>(slp::Error::PARSE_ERROR);
36*eebd0815SAndrew Geissler     }
37*eebd0815SAndrew Geissler     else
38*eebd0815SAndrew Geissler     {
39309ac445SRatan Gupta         switch (recvBuff[0])
40309ac445SRatan Gupta         {
41309ac445SRatan Gupta             case slp::VERSION_2:
42309ac445SRatan Gupta             {
43309ac445SRatan Gupta                 // Parse the buffer and construct the req object
44309ac445SRatan Gupta                 std::tie(rc, req) = slp::parser::parseBuffer(recvBuff);
45309ac445SRatan Gupta                 if (!rc)
46309ac445SRatan Gupta                 {
47309ac445SRatan Gupta                     // Passing the req object to handler to serve it
48309ac445SRatan Gupta                     std::tie(rc, resp) = slp::handler::processRequest(req);
49309ac445SRatan Gupta                 }
50309ac445SRatan Gupta                 break;
51309ac445SRatan Gupta             }
52309ac445SRatan Gupta             default:
53*eebd0815SAndrew Geissler                 std::cout << "SLP Unsupported Request Version="
54*eebd0815SAndrew Geissler                           << (int)recvBuff[0] << "\n";
550d3e9e33SRatan Gupta 
56309ac445SRatan Gupta                 rc = static_cast<uint8_t>(slp::Error::VER_NOT_SUPPORTED);
57309ac445SRatan Gupta                 break;
58309ac445SRatan Gupta         }
59*eebd0815SAndrew Geissler     }
60309ac445SRatan Gupta 
61309ac445SRatan Gupta     // if there was error during Parsing of request
62309ac445SRatan Gupta     // or processing of request then handle the error.
63309ac445SRatan Gupta     if (rc)
64309ac445SRatan Gupta     {
65309ac445SRatan Gupta         resp = slp::handler::processError(req, rc);
66309ac445SRatan Gupta     }
67309ac445SRatan Gupta 
68309ac445SRatan Gupta     channel.write(resp);
69309ac445SRatan Gupta     return slp::SUCCESS;
70309ac445SRatan Gupta }
71309ac445SRatan Gupta 
main()723964d552SPatrick Williams int main()
73309ac445SRatan Gupta {
74309ac445SRatan Gupta     slp::udp::Server svr(slp::PORT, requestHandler);
75309ac445SRatan Gupta     return svr.run();
76309ac445SRatan Gupta }
77