Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | #include <array> |
Brad Bishop | 7435de0 | 2018-02-23 15:20:15 -0500 | [diff] [blame] | 6 | #include <functional> |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 7 | #include <list> |
| 8 | #include <map> |
| 9 | #include <memory> |
| 10 | #include <string> |
| 11 | #include <tuple> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include "slp_service_info.hpp" |
| 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 | /* |
| 111 | * @struct Payload |
| 112 | * This is a payload of the SLP Message currently |
| 113 | * we are supporting two request. |
| 114 | * |
| 115 | */ |
| 116 | struct Payload |
| 117 | { |
| 118 | request::ServiceType srvtyperqst; |
| 119 | request::Service srvrqst; |
| 120 | }; |
| 121 | |
| 122 | |
| 123 | /* |
Gunnar Mills | 1f12e38 | 2018-06-13 16:03:33 -0500 | [diff] [blame] | 124 | * @struct Message |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 125 | * |
| 126 | * This will denote the slp Message. |
| 127 | */ |
| 128 | struct Message |
| 129 | { |
| 130 | Header header; |
| 131 | Payload body; |
| 132 | }; |
| 133 | |
| 134 | |
| 135 | namespace parser |
| 136 | { |
| 137 | |
| 138 | /** Parse a buffer and fill the header and the body of the message. |
| 139 | * |
| 140 | * @param[in] buffer - The buffer from which data should be parsed. |
| 141 | * |
| 142 | * @return Zero on success and parsed msg object, |
| 143 | * non-zero on failure and empty msg object. |
| 144 | * |
| 145 | */ |
| 146 | |
| 147 | std::tuple<int, Message> parseBuffer(const buffer& buf); |
| 148 | |
| 149 | namespace internal |
| 150 | { |
| 151 | |
| 152 | /** Parse header data from the buffer. |
| 153 | * |
| 154 | * @param[in] buffer - The buffer from which data should be parsed. |
| 155 | * |
| 156 | * @return Zero on success and fills header object inside message, |
| 157 | * non-zero on failure and empty msg object. |
| 158 | * |
| 159 | * @internal |
| 160 | */ |
| 161 | |
| 162 | std::tuple<int, Message> parseHeader(const buffer& buf); |
| 163 | |
| 164 | /** Parse a srvType request |
| 165 | * |
| 166 | * @param[in] buffer - The buffer from which data should be parsed. |
| 167 | * |
| 168 | * @return Zero on success,and fills the body object inside message. |
| 169 | * non-zero on failure and empty msg object. |
| 170 | * |
| 171 | * @internal |
| 172 | */ |
| 173 | |
| 174 | int parseSrvTypeRqst(const buffer& buf, Message& req); |
| 175 | |
| 176 | /** Parse a service request. |
| 177 | * |
| 178 | * @param[in] buffer - The buffer from which data should be parsed. |
| 179 | * |
| 180 | * @return Zero on success,and fills the body object inside message. |
| 181 | * non-zero on failure and empty msg object. |
| 182 | * |
| 183 | * @internal |
| 184 | */ |
| 185 | |
| 186 | int parseSrvRqst(const buffer& buf, Message& req); |
| 187 | |
| 188 | }//namespace internal |
| 189 | }//namespce parser |
| 190 | |
| 191 | |
| 192 | namespace handler |
| 193 | { |
| 194 | |
| 195 | /** Handle the request message. |
| 196 | * |
| 197 | * @param[in] msg - The message to process. |
| 198 | * |
| 199 | * @return In case of success, the vector is populated with the data |
| 200 | * available on the socket and return code is 0. |
| 201 | * In case of error, nonzero code and vector is set to size 0. |
| 202 | * |
| 203 | */ |
| 204 | |
| 205 | std::tuple<int, buffer> processRequest(const Message& msg); |
| 206 | |
| 207 | /** Handle the error |
| 208 | * |
| 209 | * @param[in] msg - Req message. |
| 210 | * @param[in] err - Error code. |
| 211 | * |
| 212 | * @return the vector populated with the error data |
| 213 | */ |
| 214 | |
| 215 | buffer processError(const Message& req, |
| 216 | const uint8_t err); |
| 217 | namespace internal |
| 218 | { |
| 219 | |
| 220 | using ServiceList = std::map<std::string, slp::ConfigData>; |
| 221 | /** Handle the SrvRequest message. |
| 222 | * |
| 223 | * @param[in] msg - The message to process |
| 224 | * |
| 225 | * @return In case of success, the vector is populated with the data |
| 226 | * available on the socket and return code is 0. |
| 227 | * In case of error, nonzero code and vector is set to size 0. |
| 228 | * |
| 229 | * @internal |
| 230 | */ |
| 231 | |
| 232 | std::tuple<int, buffer> processSrvRequest(const Message& msg); |
| 233 | |
| 234 | |
| 235 | /** Handle the SrvTypeRequest message. |
| 236 | * |
| 237 | * @param[in] msg - The message to process |
| 238 | * |
| 239 | * @return In case of success, the vector is populated with the data |
| 240 | * available on the socket and return code is 0. |
| 241 | * In case of error, nonzero code and vector is set to size 0. |
| 242 | * |
| 243 | * @internal |
| 244 | * |
| 245 | */ |
| 246 | |
| 247 | std::tuple<int, buffer> processSrvTypeRequest(const Message& msg); |
| 248 | |
| 249 | /** Read the SLPinfo from the configuration. |
| 250 | * |
| 251 | * @param[in] filename - Name of the conf file |
| 252 | * |
| 253 | * @return the list of the services |
| 254 | * |
| 255 | * @internal |
| 256 | * |
| 257 | */ |
Ratan Gupta | ead7a3c | 2017-01-05 15:45:09 +0530 | [diff] [blame] | 258 | ServiceList readSLPServiceInfo(); |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 259 | |
| 260 | /** Get all the interface address |
| 261 | * |
| 262 | * @return the list of the interface address. |
| 263 | * |
| 264 | * @internal |
| 265 | * |
| 266 | */ |
| 267 | |
| 268 | std::list<std::string> getIntfAddrs(); |
| 269 | |
| 270 | /** Fill the buffer with the header data from the request object |
| 271 | * |
| 272 | * @param[in] req - Header data will be copied from |
| 273 | * |
| 274 | * @return the vector is populated with the data |
| 275 | * |
| 276 | * @internal |
| 277 | */ |
| 278 | buffer prepareHeader(const Message& req); |
| 279 | |
| 280 | |
| 281 | }//namespace internal |
| 282 | }//namespce handler |
| 283 | }//namespce slp |