Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "endian.hpp" |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 4 | #include "slp.hpp" |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 5 | #include "slp_meta.hpp" |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 6 | |
| 7 | #include <arpa/inet.h> |
Ratan Gupta | ead7a3c | 2017-01-05 15:45:09 +0530 | [diff] [blame] | 8 | #include <dirent.h> |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 9 | #include <ifaddrs.h> |
| 10 | #include <net/if.h> |
| 11 | #include <string.h> |
| 12 | |
| 13 | #include <algorithm> |
| 14 | |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 15 | namespace slp |
| 16 | { |
| 17 | namespace handler |
| 18 | { |
| 19 | |
| 20 | namespace internal |
| 21 | { |
| 22 | |
| 23 | buffer prepareHeader(const Message& req) |
| 24 | { |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 25 | uint8_t length = |
| 26 | slp::header::MIN_LEN + /* 14 bytes for header */ |
| 27 | req.header.langtag.length() + /* Actual length of lang tag */ |
| 28 | slp::response::SIZE_ERROR; /* 2 bytes for error code */ |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 29 | |
| 30 | buffer buff(length, 0); |
| 31 | |
| 32 | buff[slp::header::OFFSET_VERSION] = req.header.version; |
| 33 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 34 | // will increment the function id from 1 as reply |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 35 | buff[slp::header::OFFSET_FUNCTION] = req.header.functionID + 1; |
| 36 | |
| 37 | std::copy_n(&length, slp::header::SIZE_LENGTH, |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 38 | buff.data() + slp::header::OFFSET_LENGTH); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 39 | |
| 40 | auto flags = endian::to_network(req.header.flags); |
| 41 | |
| 42 | std::copy_n((uint8_t*)&flags, slp::header::SIZE_FLAGS, |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 43 | buff.data() + slp::header::OFFSET_FLAGS); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 44 | |
| 45 | std::copy_n(req.header.extOffset.data(), slp::header::SIZE_EXT, |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 46 | buff.data() + slp::header::OFFSET_EXT); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 47 | |
| 48 | auto xid = endian::to_network(req.header.xid); |
| 49 | |
| 50 | std::copy_n((uint8_t*)&xid, slp::header::SIZE_XID, |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 51 | buff.data() + slp::header::OFFSET_XID); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 52 | |
| 53 | uint16_t langtagLen = req.header.langtag.length(); |
| 54 | langtagLen = endian::to_network(langtagLen); |
| 55 | std::copy_n((uint8_t*)&langtagLen, slp::header::SIZE_LANG, |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 56 | buff.data() + slp::header::OFFSET_LANG_LEN); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 57 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 58 | std::copy_n((uint8_t*)req.header.langtag.c_str(), |
| 59 | req.header.langtag.length(), |
| 60 | buff.data() + slp::header::OFFSET_LANG); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 61 | return buff; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 62 | } |
| 63 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 64 | std::tuple<int, buffer> processSrvTypeRequest(const Message& req) |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 65 | { |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 66 | /* |
| 67 | 0 1 2 3 |
| 68 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 69 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 70 | | Service Location header (function = SrvTypeRply = 10) | |
| 71 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 72 | | Error Code | length of <srvType-list> | |
| 73 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 74 | | <srvtype--list> \ |
| 75 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 76 | */ |
| 77 | |
| 78 | buffer buff; |
| 79 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 80 | // read the slp service info from conf and create the service type string |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 81 | slp::handler::internal::ServiceList svcList = |
Ratan Gupta | ead7a3c | 2017-01-05 15:45:09 +0530 | [diff] [blame] | 82 | slp::handler::internal::readSLPServiceInfo(); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 83 | if (svcList.size() <= 0) |
| 84 | { |
| 85 | buff.resize(0); |
Ratan Gupta | 0d3e9e3 | 2017-02-10 22:27:12 +0530 | [diff] [blame] | 86 | std::cerr << "SLP unable to read the service info\n"; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 87 | return std::make_tuple((int)slp::Error::INTERNAL_ERROR, buff); |
| 88 | } |
| 89 | |
| 90 | std::string service; |
| 91 | bool firstIteration = true; |
| 92 | for_each(svcList.cbegin(), svcList.cend(), |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 93 | [&service, &firstIteration](const auto& svc) { |
Patrick Williams | aa902c6 | 2023-04-04 19:59:51 -0500 | [diff] [blame^] | 94 | if (firstIteration == true) |
| 95 | { |
| 96 | service = svc.first; |
| 97 | firstIteration = false; |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | service += ","; |
| 102 | service += svc.first; |
| 103 | } |
| 104 | }); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 105 | |
| 106 | buff = prepareHeader(req); |
| 107 | |
| 108 | /* Need to modify the length and the function type field of the header |
| 109 | * as it is dependent on the handler of the service */ |
| 110 | |
| 111 | std::cout << "service=" << service.c_str() << "\n"; |
| 112 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 113 | uint8_t length = buff.size() + /* 14 bytes header + length of langtag */ |
| 114 | slp::response::SIZE_ERROR + /* 2 byte err code */ |
| 115 | slp::response::SIZE_SERVICE + /* 2 byte srvtype len */ |
| 116 | service.length(); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 117 | |
| 118 | buff.resize(length); |
| 119 | |
| 120 | std::copy_n(&length, slp::header::SIZE_LENGTH, |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 121 | buff.data() + slp::header::OFFSET_LENGTH); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 122 | |
| 123 | /* error code is already set to 0 moving to service type len */ |
| 124 | |
| 125 | uint16_t serviceTypeLen = service.length(); |
| 126 | serviceTypeLen = endian::to_network(serviceTypeLen); |
| 127 | |
| 128 | std::copy_n((uint8_t*)&serviceTypeLen, slp::response::SIZE_SERVICE, |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 129 | buff.data() + slp::response::OFFSET_SERVICE_LEN); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 130 | |
| 131 | /* service type data */ |
| 132 | std::copy_n((uint8_t*)service.c_str(), service.length(), |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 133 | (buff.data() + slp::response::OFFSET_SERVICE)); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 134 | |
| 135 | return std::make_tuple(slp::SUCCESS, buff); |
| 136 | } |
| 137 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 138 | std::tuple<int, buffer> processSrvRequest(const Message& req) |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 139 | { |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 140 | /* |
| 141 | Service Reply |
| 142 | 0 1 2 3 |
| 143 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 144 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 145 | | Service Location header (function = SrvRply = 2) | |
| 146 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 147 | | Error Code | URL Entry count | |
| 148 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 149 | | <URL Entry 1> ... <URL Entry N> \ |
| 150 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 151 | |
| 152 | URL Entry |
| 153 | 0 1 2 3 |
| 154 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 155 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 156 | | Reserved | Lifetime | URL Length | |
| 157 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 158 | |URL len, contd.| URL (variable length) \ |
| 159 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 160 | |# of URL auths | Auth. blocks (if any) \ |
| 161 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 162 | */ |
| 163 | |
| 164 | buffer buff; |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 165 | // Get all the services which are registered |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 166 | slp::handler::internal::ServiceList svcList = |
Ratan Gupta | ead7a3c | 2017-01-05 15:45:09 +0530 | [diff] [blame] | 167 | slp::handler::internal::readSLPServiceInfo(); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 168 | if (svcList.size() <= 0) |
| 169 | { |
| 170 | buff.resize(0); |
Ratan Gupta | 0d3e9e3 | 2017-02-10 22:27:12 +0530 | [diff] [blame] | 171 | std::cerr << "SLP unable to read the service info\n"; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 172 | return std::make_tuple((int)slp::Error::INTERNAL_ERROR, buff); |
| 173 | } |
| 174 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 175 | // return error if serice type doesn't match |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 176 | auto& svcName = req.body.srvrqst.srvType; |
| 177 | auto svcIt = svcList.find(svcName); |
| 178 | if (svcIt == svcList.end()) |
| 179 | { |
| 180 | buff.resize(0); |
Ratan Gupta | 0d3e9e3 | 2017-02-10 22:27:12 +0530 | [diff] [blame] | 181 | std::cerr << "SLP unable to find the service=" << svcName << "\n"; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 182 | return std::make_tuple((int)slp::Error::INTERNAL_ERROR, buff); |
| 183 | } |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 184 | // Get all the interface address |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 185 | auto ifaddrList = slp::handler::internal::getIntfAddrs(); |
| 186 | if (ifaddrList.size() <= 0) |
| 187 | { |
| 188 | buff.resize(0); |
Ratan Gupta | 0d3e9e3 | 2017-02-10 22:27:12 +0530 | [diff] [blame] | 189 | std::cerr << "SLP unable to read the interface address\n"; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 190 | return std::make_tuple((int)slp::Error::INTERNAL_ERROR, buff); |
| 191 | } |
| 192 | |
| 193 | buff = prepareHeader(req); |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 194 | // Calculate the length and resize the buffer |
| 195 | uint8_t length = buff.size() + /* 14 bytes header + length of langtag */ |
| 196 | slp::response::SIZE_ERROR + /* 2 bytes error code */ |
| 197 | slp::response::SIZE_URL_COUNT; /* 2 bytes srvtype len */ |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 198 | |
| 199 | buff.resize(length); |
| 200 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 201 | // Populate the url count |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 202 | uint16_t urlCount = ifaddrList.size(); |
| 203 | urlCount = endian::to_network(urlCount); |
| 204 | |
| 205 | std::copy_n((uint8_t*)&urlCount, slp::response::SIZE_URL_COUNT, |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 206 | buff.data() + slp::response::OFFSET_URL_ENTRY); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 207 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 208 | // Find the service |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 209 | const slp::ConfigData& svc = svcIt->second; |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 210 | // Populate the URL Entries |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 211 | auto pos = slp::response::OFFSET_URL_ENTRY + slp::response::SIZE_URL_COUNT; |
| 212 | for (const auto& addr : ifaddrList) |
| 213 | { |
Patrick Williams | aa902c6 | 2023-04-04 19:59:51 -0500 | [diff] [blame^] | 214 | std::string url = svc.name + ':' + svc.type + "//" + addr + ',' + |
| 215 | svc.port; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 216 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 217 | buff.resize(buff.size() + slp::response::SIZE_URL_ENTRY + url.length()); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 218 | |
| 219 | uint8_t reserved = 0; |
| 220 | uint16_t auth = 0; |
| 221 | uint16_t lifetime = endian::to_network<uint16_t>(slp::LIFETIME); |
| 222 | uint16_t urlLength = url.length(); |
| 223 | |
| 224 | std::copy_n((uint8_t*)&reserved, slp::response::SIZE_RESERVED, |
| 225 | buff.data() + pos); |
| 226 | |
| 227 | pos += slp::response::SIZE_RESERVED; |
| 228 | |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 229 | std::copy_n((uint8_t*)&lifetime, slp::response::SIZE_LIFETIME, |
| 230 | buff.data() + pos); |
| 231 | |
| 232 | pos += slp::response::SIZE_LIFETIME; |
| 233 | |
| 234 | urlLength = endian::to_network(urlLength); |
| 235 | std::copy_n((uint8_t*)&urlLength, slp::response::SIZE_URLLENGTH, |
| 236 | buff.data() + pos); |
| 237 | pos += slp::response::SIZE_URLLENGTH; |
| 238 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 239 | std::copy_n((uint8_t*)url.c_str(), url.length(), buff.data() + pos); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 240 | pos += url.length(); |
| 241 | |
| 242 | std::copy_n((uint8_t*)&auth, slp::response::SIZE_AUTH, |
| 243 | buff.data() + pos); |
| 244 | pos += slp::response::SIZE_AUTH; |
| 245 | } |
| 246 | uint8_t packetLength = buff.size(); |
| 247 | std::copy_n((uint8_t*)&packetLength, slp::header::SIZE_VERSION, |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 248 | buff.data() + slp::header::OFFSET_LENGTH); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 249 | |
| 250 | return std::make_tuple((int)slp::SUCCESS, buff); |
| 251 | } |
| 252 | |
| 253 | std::list<std::string> getIntfAddrs() |
| 254 | { |
| 255 | std::list<std::string> addrList; |
| 256 | |
| 257 | struct ifaddrs* ifaddr; |
| 258 | // attempt to fill struct with ifaddrs |
| 259 | if (getifaddrs(&ifaddr) == -1) |
| 260 | { |
| 261 | return addrList; |
| 262 | } |
| 263 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 264 | slp::deleted_unique_ptr<ifaddrs> ifaddrPtr( |
| 265 | ifaddr, [](ifaddrs* addr) { freeifaddrs(addr); }); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 266 | |
| 267 | ifaddr = nullptr; |
| 268 | |
| 269 | for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next) |
| 270 | { |
| 271 | // walk interfaces |
| 272 | if (ifa->ifa_addr == nullptr) |
| 273 | { |
| 274 | continue; |
| 275 | } |
| 276 | |
| 277 | // get only INET interfaces not ipv6 |
| 278 | if (ifa->ifa_addr->sa_family == AF_INET) |
| 279 | { |
| 280 | // if loopback, or not running ignore |
| 281 | if ((ifa->ifa_flags & IFF_LOOPBACK) || |
| 282 | !(ifa->ifa_flags & IFF_RUNNING)) |
| 283 | { |
| 284 | continue; |
| 285 | } |
| 286 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 287 | char tmp[INET_ADDRSTRLEN] = {0}; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 288 | |
| 289 | inet_ntop(AF_INET, |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 290 | &(((struct sockaddr_in*)(ifa->ifa_addr))->sin_addr), tmp, |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 291 | sizeof(tmp)); |
| 292 | addrList.emplace_back(tmp); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | return addrList; |
| 297 | } |
| 298 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 299 | slp::handler::internal::ServiceList readSLPServiceInfo() |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 300 | { |
| 301 | using namespace std::string_literals; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 302 | slp::handler::internal::ServiceList svcLst; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 303 | slp::ConfigData service; |
Ratan Gupta | ead7a3c | 2017-01-05 15:45:09 +0530 | [diff] [blame] | 304 | struct dirent* dent = nullptr; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 305 | |
Ratan Gupta | ead7a3c | 2017-01-05 15:45:09 +0530 | [diff] [blame] | 306 | // Open the services dir and get the service info |
| 307 | // from service files. |
| 308 | // Service File format would be "ServiceName serviceType Port" |
| 309 | DIR* dir = opendir(SERVICE_DIR); |
| 310 | // wrap the pointer into smart pointer. |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 311 | slp::deleted_unique_ptr<DIR> dirPtr(dir, [](DIR* dir) { |
Ratan Gupta | ead7a3c | 2017-01-05 15:45:09 +0530 | [diff] [blame] | 312 | if (!dir) |
| 313 | { |
| 314 | closedir(dir); |
| 315 | } |
| 316 | }); |
| 317 | dir = nullptr; |
| 318 | |
| 319 | if (dirPtr.get()) |
| 320 | { |
| 321 | while ((dent = readdir(dirPtr.get())) != NULL) |
| 322 | { |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 323 | if (dent->d_type == DT_REG) // regular file |
Ratan Gupta | ead7a3c | 2017-01-05 15:45:09 +0530 | [diff] [blame] | 324 | { |
| 325 | auto absFileName = std::string(SERVICE_DIR) + dent->d_name; |
| 326 | std::ifstream readFile(absFileName); |
| 327 | readFile >> service; |
| 328 | service.name = "service:"s + service.name; |
| 329 | svcLst.emplace(service.name, service); |
| 330 | } |
| 331 | } |
Ratan Gupta | ead7a3c | 2017-01-05 15:45:09 +0530 | [diff] [blame] | 332 | } |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 333 | return svcLst; |
| 334 | } |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 335 | } // namespace internal |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 336 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 337 | std::tuple<int, buffer> processRequest(const Message& msg) |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 338 | { |
| 339 | int rc = slp::SUCCESS; |
| 340 | buffer resp(0); |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 341 | std::cout << "SLP Processing Request=" << msg.header.functionID << "\n"; |
Brad Bishop | b5e632a | 2018-02-23 15:22:12 -0500 | [diff] [blame] | 342 | |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 343 | switch (msg.header.functionID) |
| 344 | { |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 345 | case (uint8_t)slp::FunctionType::SRVTYPERQST: |
Patrick Williams | aa902c6 | 2023-04-04 19:59:51 -0500 | [diff] [blame^] | 346 | std::tie(rc, |
| 347 | resp) = slp::handler::internal::processSrvTypeRequest(msg); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 348 | break; |
| 349 | case (uint8_t)slp::FunctionType::SRVRQST: |
| 350 | std::tie(rc, resp) = slp::handler::internal::processSrvRequest(msg); |
| 351 | break; |
| 352 | default: |
| 353 | rc = (uint8_t)slp::Error::MSG_NOT_SUPPORTED; |
| 354 | } |
| 355 | return std::make_tuple(rc, resp); |
| 356 | } |
| 357 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 358 | buffer processError(const Message& req, uint8_t err) |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 359 | { |
| 360 | buffer buff; |
| 361 | buff = slp::handler::internal::prepareHeader(req); |
| 362 | |
| 363 | std::copy_n(&err, slp::response::SIZE_ERROR, |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 364 | buff.data() + slp::response::OFFSET_ERROR); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 365 | return buff; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 366 | } |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 367 | } // namespace handler |
| 368 | } // namespace slp |