Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | #include <array> |
| 6 | #include <list> |
| 7 | #include <map> |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | #include <tuple> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "slp_service_info.hpp" |
| 14 | |
| 15 | namespace slp |
| 16 | { |
| 17 | |
| 18 | using buffer = std::vector<uint8_t>; |
| 19 | |
| 20 | template<typename T> |
| 21 | using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T*)>>; |
| 22 | |
| 23 | namespace request |
| 24 | { |
| 25 | |
| 26 | /* |
| 27 | * @struct ServiceType |
| 28 | * |
| 29 | * SLP Message structure for ServiceType Request. |
| 30 | */ |
| 31 | struct ServiceType |
| 32 | { |
| 33 | std::string prList; |
| 34 | std::string namingAuth; |
| 35 | std::string scopeList; |
| 36 | }; |
| 37 | |
| 38 | /* |
| 39 | * @struct Service |
| 40 | * |
| 41 | * SLP Message structure for Service Request. |
| 42 | */ |
| 43 | struct Service |
| 44 | { |
| 45 | std::string prList; |
| 46 | std::string srvType; |
| 47 | std::string scopeList; |
| 48 | std::string predicate; |
| 49 | std::string spistr; |
| 50 | }; |
| 51 | }//namespace request |
| 52 | |
| 53 | /* |
| 54 | * @enum FunctionType |
| 55 | * |
| 56 | * SLP Protocol supported Message types. |
| 57 | */ |
| 58 | enum class FunctionType : uint8_t |
| 59 | { |
| 60 | SRVRQST = 0x01, |
| 61 | SRVRPLY = 0x02, |
| 62 | ATTRRQST = 0x06, |
| 63 | ATTRRPLY = 0x07, |
| 64 | SRVTYPERQST = 0x09, |
| 65 | SRVTYPERPLY = 0x0A, |
| 66 | SAADV = 0x0B, |
| 67 | }; |
| 68 | |
| 69 | /* |
| 70 | * @enum Error |
| 71 | * |
| 72 | * SLP Protocol defined Error Codes. |
| 73 | */ |
| 74 | enum class Error : uint8_t |
| 75 | { |
| 76 | LANGUAGE_NOT_SUPPORTED = 0x01, |
| 77 | PARSE_ERROR = 0x02, |
| 78 | INVALID_REGISTRATION = 0x03, |
| 79 | SCOPE_NOT_SUPPORTED = 0x04, |
| 80 | AUTHENTICATION_UNKNOWN = 0x05, |
| 81 | AUTHENTICATION_ABSENT = 0x06, |
| 82 | AUTHENTICATION_FAILED = 0x07, |
| 83 | VER_NOT_SUPPORTED = 0x09, |
| 84 | INTERNAL_ERROR = 0x0A, |
| 85 | DA_BUSY_NOW = 0x0B, |
| 86 | OPTION_NOT_UNDERSTOOD = 0x0C, |
| 87 | INVALID_UPDATE = 0x0D, |
| 88 | MSG_NOT_SUPPORTED = 0x0E, |
| 89 | }; |
| 90 | |
| 91 | /* |
| 92 | * @struct Header |
| 93 | * |
| 94 | * SLP Protocol Header |
| 95 | */ |
| 96 | struct Header |
| 97 | { |
| 98 | uint8_t version = 0; |
| 99 | uint8_t functionID = 0; |
| 100 | std::array<uint8_t, 3> length; |
| 101 | uint16_t flags = 0; |
| 102 | std::array<uint8_t, 3> extOffset; |
| 103 | uint16_t xid = 0; |
| 104 | uint16_t langtagLen = 0; |
| 105 | std::string langtag; |
| 106 | |
| 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 | /* |
| 123 | * @struct Messsage |
| 124 | * |
| 125 | * This will denote the slp Message. |
| 126 | */ |
| 127 | struct Message |
| 128 | { |
| 129 | Header header; |
| 130 | Payload body; |
| 131 | }; |
| 132 | |
| 133 | |
| 134 | namespace parser |
| 135 | { |
| 136 | |
| 137 | /** Parse a buffer and fill the header and the body of the message. |
| 138 | * |
| 139 | * @param[in] buffer - The buffer from which data should be parsed. |
| 140 | * |
| 141 | * @return Zero on success and parsed msg object, |
| 142 | * non-zero on failure and empty msg object. |
| 143 | * |
| 144 | */ |
| 145 | |
| 146 | std::tuple<int, Message> parseBuffer(const buffer& buf); |
| 147 | |
| 148 | namespace internal |
| 149 | { |
| 150 | |
| 151 | /** Parse header data from the buffer. |
| 152 | * |
| 153 | * @param[in] buffer - The buffer from which data should be parsed. |
| 154 | * |
| 155 | * @return Zero on success and fills header object inside message, |
| 156 | * non-zero on failure and empty msg object. |
| 157 | * |
| 158 | * @internal |
| 159 | */ |
| 160 | |
| 161 | std::tuple<int, Message> parseHeader(const buffer& buf); |
| 162 | |
| 163 | /** Parse a srvType request |
| 164 | * |
| 165 | * @param[in] buffer - The buffer from which data should be parsed. |
| 166 | * |
| 167 | * @return Zero on success,and fills the body object inside message. |
| 168 | * non-zero on failure and empty msg object. |
| 169 | * |
| 170 | * @internal |
| 171 | */ |
| 172 | |
| 173 | int parseSrvTypeRqst(const buffer& buf, Message& req); |
| 174 | |
| 175 | /** Parse a service request. |
| 176 | * |
| 177 | * @param[in] buffer - The buffer from which data should be parsed. |
| 178 | * |
| 179 | * @return Zero on success,and fills the body object inside message. |
| 180 | * non-zero on failure and empty msg object. |
| 181 | * |
| 182 | * @internal |
| 183 | */ |
| 184 | |
| 185 | int parseSrvRqst(const buffer& buf, Message& req); |
| 186 | |
| 187 | }//namespace internal |
| 188 | }//namespce parser |
| 189 | |
| 190 | |
| 191 | namespace handler |
| 192 | { |
| 193 | |
| 194 | /** Handle the request message. |
| 195 | * |
| 196 | * @param[in] msg - The message to process. |
| 197 | * |
| 198 | * @return In case of success, the vector is populated with the data |
| 199 | * available on the socket and return code is 0. |
| 200 | * In case of error, nonzero code and vector is set to size 0. |
| 201 | * |
| 202 | */ |
| 203 | |
| 204 | std::tuple<int, buffer> processRequest(const Message& msg); |
| 205 | |
| 206 | /** Handle the error |
| 207 | * |
| 208 | * @param[in] msg - Req message. |
| 209 | * @param[in] err - Error code. |
| 210 | * |
| 211 | * @return the vector populated with the error data |
| 212 | */ |
| 213 | |
| 214 | buffer processError(const Message& req, |
| 215 | const uint8_t err); |
| 216 | namespace internal |
| 217 | { |
| 218 | |
| 219 | using ServiceList = std::map<std::string, slp::ConfigData>; |
| 220 | /** Handle the SrvRequest message. |
| 221 | * |
| 222 | * @param[in] msg - The message to process |
| 223 | * |
| 224 | * @return In case of success, the vector is populated with the data |
| 225 | * available on the socket and return code is 0. |
| 226 | * In case of error, nonzero code and vector is set to size 0. |
| 227 | * |
| 228 | * @internal |
| 229 | */ |
| 230 | |
| 231 | std::tuple<int, buffer> processSrvRequest(const Message& msg); |
| 232 | |
| 233 | |
| 234 | /** Handle the SrvTypeRequest message. |
| 235 | * |
| 236 | * @param[in] msg - The message to process |
| 237 | * |
| 238 | * @return In case of success, the vector is populated with the data |
| 239 | * available on the socket and return code is 0. |
| 240 | * In case of error, nonzero code and vector is set to size 0. |
| 241 | * |
| 242 | * @internal |
| 243 | * |
| 244 | */ |
| 245 | |
| 246 | std::tuple<int, buffer> processSrvTypeRequest(const Message& msg); |
| 247 | |
| 248 | /** Read the SLPinfo from the configuration. |
| 249 | * |
| 250 | * @param[in] filename - Name of the conf file |
| 251 | * |
| 252 | * @return the list of the services |
| 253 | * |
| 254 | * @internal |
| 255 | * |
| 256 | */ |
Ratan Gupta | ead7a3c | 2017-01-05 15:45:09 +0530 | [diff] [blame] | 257 | ServiceList readSLPServiceInfo(); |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 258 | |
| 259 | /** Get all the interface address |
| 260 | * |
| 261 | * @return the list of the interface address. |
| 262 | * |
| 263 | * @internal |
| 264 | * |
| 265 | */ |
| 266 | |
| 267 | std::list<std::string> getIntfAddrs(); |
| 268 | |
| 269 | /** Fill the buffer with the header data from the request object |
| 270 | * |
| 271 | * @param[in] req - Header data will be copied from |
| 272 | * |
| 273 | * @return the vector is populated with the data |
| 274 | * |
| 275 | * @internal |
| 276 | */ |
| 277 | buffer prepareHeader(const Message& req); |
| 278 | |
| 279 | |
| 280 | }//namespace internal |
| 281 | }//namespce handler |
| 282 | }//namespce slp |