blob: 3e6d968f7cb6774dc66eb261194c7759fba91f48 [file] [log] [blame]
Ratan Gupta309ac442016-12-13 20:40:06 +05301#pragma once
2
Patrick Venture537ff142018-11-01 16:37:09 -07003#include "slp_service_info.hpp"
4
Ratan Gupta309ac442016-12-13 20:40:06 +05305#include <stdio.h>
6
7#include <array>
Brad Bishop7435de02018-02-23 15:20:15 -05008#include <functional>
Ratan Gupta309ac442016-12-13 20:40:06 +05309#include <list>
10#include <map>
11#include <memory>
12#include <string>
13#include <tuple>
14#include <vector>
15
Ratan Gupta309ac442016-12-13 20:40:06 +053016namespace slp
17{
18
19using buffer = std::vector<uint8_t>;
20
Patrick Venture537ff142018-11-01 16:37:09 -070021template <typename T>
Ratan Gupta309ac442016-12-13 20:40:06 +053022using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T*)>>;
23
24namespace request
25{
26
27/*
28 * @struct ServiceType
29 *
30 * SLP Message structure for ServiceType Request.
31 */
32struct ServiceType
33{
34 std::string prList;
35 std::string namingAuth;
36 std::string scopeList;
37};
38
39/*
40 * @struct Service
41 *
42 * SLP Message structure for Service Request.
43 */
44struct Service
45{
46 std::string prList;
47 std::string srvType;
48 std::string scopeList;
49 std::string predicate;
50 std::string spistr;
51};
Patrick Venture537ff142018-11-01 16:37:09 -070052} // namespace request
Ratan Gupta309ac442016-12-13 20:40:06 +053053
54/*
55 * @enum FunctionType
56 *
57 * SLP Protocol supported Message types.
58 */
59enum class FunctionType : uint8_t
60{
Patrick Venture537ff142018-11-01 16:37:09 -070061 SRVRQST = 0x01,
62 SRVRPLY = 0x02,
63 ATTRRQST = 0x06,
64 ATTRRPLY = 0x07,
Ratan Gupta309ac442016-12-13 20:40:06 +053065 SRVTYPERQST = 0x09,
66 SRVTYPERPLY = 0x0A,
Patrick Venture537ff142018-11-01 16:37:09 -070067 SAADV = 0x0B,
Ratan Gupta309ac442016-12-13 20:40:06 +053068};
69
70/*
71 * @enum Error
72 *
73 * SLP Protocol defined Error Codes.
74 */
75enum class Error : uint8_t
76{
77 LANGUAGE_NOT_SUPPORTED = 0x01,
Patrick Venture537ff142018-11-01 16:37:09 -070078 PARSE_ERROR = 0x02,
79 INVALID_REGISTRATION = 0x03,
80 SCOPE_NOT_SUPPORTED = 0x04,
Ratan Gupta309ac442016-12-13 20:40:06 +053081 AUTHENTICATION_UNKNOWN = 0x05,
Patrick Venture537ff142018-11-01 16:37:09 -070082 AUTHENTICATION_ABSENT = 0x06,
83 AUTHENTICATION_FAILED = 0x07,
84 VER_NOT_SUPPORTED = 0x09,
85 INTERNAL_ERROR = 0x0A,
86 DA_BUSY_NOW = 0x0B,
87 OPTION_NOT_UNDERSTOOD = 0x0C,
88 INVALID_UPDATE = 0x0D,
89 MSG_NOT_SUPPORTED = 0x0E,
Ratan Gupta309ac442016-12-13 20:40:06 +053090};
91
92/*
93 * @struct Header
94 *
95 * SLP Protocol Header
96 */
97struct Header
98{
99 uint8_t version = 0;
100 uint8_t functionID = 0;
101 std::array<uint8_t, 3> length;
102 uint16_t flags = 0;
103 std::array<uint8_t, 3> extOffset;
104 uint16_t xid = 0;
105 uint16_t langtagLen = 0;
106 std::string langtag;
Ratan Gupta309ac442016-12-13 20:40:06 +0530107};
108
109/*
110 * @struct Payload
111 * This is a payload of the SLP Message currently
112 * we are supporting two request.
113 *
114 */
115struct Payload
116{
117 request::ServiceType srvtyperqst;
118 request::Service srvrqst;
119};
120
Ratan Gupta309ac442016-12-13 20:40:06 +0530121/*
Gunnar Mills1f12e382018-06-13 16:03:33 -0500122 * @struct Message
Ratan Gupta309ac442016-12-13 20:40:06 +0530123 *
124 * This will denote the slp Message.
125 */
126struct Message
127{
128 Header header;
129 Payload body;
130};
131
Ratan Gupta309ac442016-12-13 20:40:06 +0530132namespace parser
133{
134
135/** Parse a buffer and fill the header and the body of the message.
136 *
137 * @param[in] buffer - The buffer from which data should be parsed.
138 *
139 * @return Zero on success and parsed msg object,
140 * non-zero on failure and empty msg object.
141 *
142 */
143
144std::tuple<int, Message> parseBuffer(const buffer& buf);
145
146namespace internal
147{
148
149/** Parse header data from the buffer.
150 *
151 * @param[in] buffer - The buffer from which data should be parsed.
152 *
153 * @return Zero on success and fills header object inside message,
154 * non-zero on failure and empty msg object.
155 *
156 * @internal
157 */
158
159std::tuple<int, Message> parseHeader(const buffer& buf);
160
161/** Parse a srvType request
162 *
163 * @param[in] buffer - The buffer from which data should be parsed.
164 *
165 * @return Zero on success,and fills the body object inside message.
166 * non-zero on failure and empty msg object.
167 *
168 * @internal
169 */
170
171int parseSrvTypeRqst(const buffer& buf, Message& req);
172
173/** Parse a service request.
Patrick Venture537ff142018-11-01 16:37:09 -0700174 *
175 * @param[in] buffer - The buffer from which data should be parsed.
176 *
177 * @return Zero on success,and fills the body object inside message.
178 * non-zero on failure and empty msg object.
179 *
180 * @internal
181 */
Ratan Gupta309ac442016-12-13 20:40:06 +0530182
183int parseSrvRqst(const buffer& buf, Message& req);
184
Patrick Venture537ff142018-11-01 16:37:09 -0700185} // namespace internal
186} // namespace parser
Ratan Gupta309ac442016-12-13 20:40:06 +0530187
188namespace handler
189{
190
191/** Handle the request message.
192 *
193 * @param[in] msg - The message to process.
194 *
195 * @return In case of success, the vector is populated with the data
196 * available on the socket and return code is 0.
197 * In case of error, nonzero code and vector is set to size 0.
198 *
199 */
200
201std::tuple<int, buffer> processRequest(const Message& msg);
202
203/** Handle the error
204 *
205 * @param[in] msg - Req message.
206 * @param[in] err - Error code.
207 *
208 * @return the vector populated with the error data
209 */
210
Patrick Venture537ff142018-11-01 16:37:09 -0700211buffer processError(const Message& req, const uint8_t err);
Ratan Gupta309ac442016-12-13 20:40:06 +0530212namespace internal
213{
214
215using ServiceList = std::map<std::string, slp::ConfigData>;
216/** Handle the SrvRequest message.
217 *
218 * @param[in] msg - The message to process
219 *
220 * @return In case of success, the vector is populated with the data
221 * available on the socket and return code is 0.
222 * In case of error, nonzero code and vector is set to size 0.
223 *
224 * @internal
225 */
226
227std::tuple<int, buffer> processSrvRequest(const Message& msg);
228
Ratan Gupta309ac442016-12-13 20:40:06 +0530229/** Handle the SrvTypeRequest message.
230 *
231 * @param[in] msg - The message to process
232 *
233 * @return In case of success, the vector is populated with the data
234 * available on the socket and return code is 0.
235 * In case of error, nonzero code and vector is set to size 0.
236 *
237 * @internal
238 *
239 */
240
241std::tuple<int, buffer> processSrvTypeRequest(const Message& msg);
242
243/** Read the SLPinfo from the configuration.
244 *
245 * @param[in] filename - Name of the conf file
246 *
247 * @return the list of the services
248 *
249 * @internal
250 *
251 */
Ratan Guptaead7a3c2017-01-05 15:45:09 +0530252ServiceList readSLPServiceInfo();
Ratan Gupta309ac442016-12-13 20:40:06 +0530253
254/** Get all the interface address
255 *
256 * @return the list of the interface address.
257 *
258 * @internal
259 *
260 */
261
262std::list<std::string> getIntfAddrs();
263
264/** Fill the buffer with the header data from the request object
265 *
266 * @param[in] req - Header data will be copied from
267 *
268 * @return the vector is populated with the data
269 *
270 * @internal
271 */
272buffer prepareHeader(const Message& req);
273
Patrick Venture537ff142018-11-01 16:37:09 -0700274} // namespace internal
275} // namespace handler
276} // namespace slp