1 #pragma once 2 3 #include "slp_service_info.hpp" 4 5 #include <stdio.h> 6 7 #include <array> 8 #include <functional> 9 #include <list> 10 #include <map> 11 #include <memory> 12 #include <string> 13 #include <tuple> 14 #include <vector> 15 16 namespace slp 17 { 18 19 using buffer = std::vector<uint8_t>; 20 21 template <typename T> 22 using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T*)>>; 23 24 namespace request 25 { 26 27 /* 28 * @struct ServiceType 29 * 30 * SLP Message structure for ServiceType Request. 31 */ 32 struct 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 */ 44 struct Service 45 { 46 std::string prList; 47 std::string srvType; 48 std::string scopeList; 49 std::string predicate; 50 std::string spistr; 51 }; 52 } // namespace request 53 54 /* 55 * @enum FunctionType 56 * 57 * SLP Protocol supported Message types. 58 */ 59 enum class FunctionType : uint8_t 60 { 61 SRVRQST = 0x01, 62 SRVRPLY = 0x02, 63 ATTRRQST = 0x06, 64 ATTRRPLY = 0x07, 65 SRVTYPERQST = 0x09, 66 SRVTYPERPLY = 0x0A, 67 SAADV = 0x0B, 68 }; 69 70 /* 71 * @enum Error 72 * 73 * SLP Protocol defined Error Codes. 74 */ 75 enum class Error : uint8_t 76 { 77 LANGUAGE_NOT_SUPPORTED = 0x01, 78 PARSE_ERROR = 0x02, 79 INVALID_REGISTRATION = 0x03, 80 SCOPE_NOT_SUPPORTED = 0x04, 81 AUTHENTICATION_UNKNOWN = 0x05, 82 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, 90 }; 91 92 /* 93 * @struct Header 94 * 95 * SLP Protocol Header 96 */ 97 struct 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; 107 }; 108 109 /* 110 * @struct Payload 111 * This is a payload of the SLP Message currently 112 * we are supporting two request. 113 * 114 */ 115 struct Payload 116 { 117 request::ServiceType srvtyperqst; 118 request::Service srvrqst; 119 }; 120 121 /* 122 * @struct Message 123 * 124 * This will denote the slp Message. 125 */ 126 struct Message 127 { 128 Header header; 129 Payload body; 130 }; 131 132 namespace 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 144 std::tuple<int, Message> parseBuffer(const buffer& buf); 145 146 namespace 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 159 std::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 171 int parseSrvTypeRqst(const buffer& buf, Message& req); 172 173 /** Parse a service request. 174 * 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 */ 182 183 int parseSrvRqst(const buffer& buf, Message& req); 184 185 } // namespace internal 186 } // namespace parser 187 188 namespace 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 201 std::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 211 buffer processError(const Message& req, const uint8_t err); 212 namespace internal 213 { 214 215 using 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 227 std::tuple<int, buffer> processSrvRequest(const Message& msg); 228 229 /** 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 241 std::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 */ 252 ServiceList readSLPServiceInfo(); 253 254 /** Get all the interface address 255 * 256 * @return the list of the interface address. 257 * 258 * @internal 259 * 260 */ 261 262 std::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 */ 272 buffer prepareHeader(const Message& req); 273 274 } // namespace internal 275 } // namespace handler 276 } // namespace slp 277