Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 1 | #include "endian.hpp" |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 2 | #include "slp.hpp" |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 3 | #include "slp_meta.hpp" |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 4 | |
| 5 | #include <arpa/inet.h> |
Ratan Gupta | ead7a3c | 2017-01-05 15:45:09 +0530 | [diff] [blame] | 6 | #include <dirent.h> |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 7 | #include <ifaddrs.h> |
| 8 | #include <net/if.h> |
| 9 | #include <string.h> |
| 10 | |
| 11 | #include <algorithm> |
| 12 | |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 13 | namespace slp |
| 14 | { |
| 15 | namespace handler |
| 16 | { |
| 17 | |
| 18 | namespace internal |
| 19 | { |
| 20 | |
Patrick Williams | f93142e | 2023-04-04 20:06:36 -0500 | [diff] [blame] | 21 | static constexpr auto SERVICE_DIR = "/etc/slp/services/"; |
| 22 | |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 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 | |
Andrew Geissler | eebd081 | 2024-05-24 10:05:27 -0500 | [diff] [blame^] | 113 | // See if total response size exceeds our max |
| 114 | uint32_t totalLength = |
| 115 | buff.size() + /* 14 bytes header + length of langtag */ |
| 116 | slp::response::SIZE_ERROR + /* 2 byte err code */ |
| 117 | slp::response::SIZE_SERVICE + /* 2 byte srvtype len */ |
| 118 | service.length(); |
| 119 | if (totalLength > slp::MAX_LEN) |
| 120 | { |
| 121 | std::cerr << "Message response size exceeds maximum allowed: " |
| 122 | << totalLength << " / " << slp::MAX_LEN << std::endl; |
| 123 | buff.resize(0); |
| 124 | return std::make_tuple((int)slp::Error::PARSE_ERROR, buff); |
| 125 | } |
| 126 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 127 | uint8_t length = buff.size() + /* 14 bytes header + length of langtag */ |
| 128 | slp::response::SIZE_ERROR + /* 2 byte err code */ |
| 129 | slp::response::SIZE_SERVICE + /* 2 byte srvtype len */ |
| 130 | service.length(); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 131 | |
| 132 | buff.resize(length); |
| 133 | |
| 134 | std::copy_n(&length, slp::header::SIZE_LENGTH, |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 135 | buff.data() + slp::header::OFFSET_LENGTH); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 136 | |
| 137 | /* error code is already set to 0 moving to service type len */ |
| 138 | |
| 139 | uint16_t serviceTypeLen = service.length(); |
| 140 | serviceTypeLen = endian::to_network(serviceTypeLen); |
| 141 | |
| 142 | std::copy_n((uint8_t*)&serviceTypeLen, slp::response::SIZE_SERVICE, |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 143 | buff.data() + slp::response::OFFSET_SERVICE_LEN); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 144 | |
| 145 | /* service type data */ |
| 146 | std::copy_n((uint8_t*)service.c_str(), service.length(), |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 147 | (buff.data() + slp::response::OFFSET_SERVICE)); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 148 | |
| 149 | return std::make_tuple(slp::SUCCESS, buff); |
| 150 | } |
| 151 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 152 | std::tuple<int, buffer> processSrvRequest(const Message& req) |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 153 | { |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 154 | /* |
| 155 | Service Reply |
| 156 | 0 1 2 3 |
| 157 | 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 |
| 158 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 159 | | Service Location header (function = SrvRply = 2) | |
| 160 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 161 | | Error Code | URL Entry count | |
| 162 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 163 | | <URL Entry 1> ... <URL Entry N> \ |
| 164 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 165 | |
| 166 | URL Entry |
| 167 | 0 1 2 3 |
| 168 | 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 |
| 169 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 170 | | Reserved | Lifetime | URL Length | |
| 171 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 172 | |URL len, contd.| URL (variable length) \ |
| 173 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 174 | |# of URL auths | Auth. blocks (if any) \ |
| 175 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 176 | */ |
| 177 | |
| 178 | buffer buff; |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 179 | // Get all the services which are registered |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 180 | slp::handler::internal::ServiceList svcList = |
Ratan Gupta | ead7a3c | 2017-01-05 15:45:09 +0530 | [diff] [blame] | 181 | slp::handler::internal::readSLPServiceInfo(); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 182 | if (svcList.size() <= 0) |
| 183 | { |
| 184 | buff.resize(0); |
Ratan Gupta | 0d3e9e3 | 2017-02-10 22:27:12 +0530 | [diff] [blame] | 185 | std::cerr << "SLP unable to read the service info\n"; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 186 | return std::make_tuple((int)slp::Error::INTERNAL_ERROR, buff); |
| 187 | } |
| 188 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 189 | // return error if serice type doesn't match |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 190 | auto& svcName = req.body.srvrqst.srvType; |
| 191 | auto svcIt = svcList.find(svcName); |
| 192 | if (svcIt == svcList.end()) |
| 193 | { |
| 194 | buff.resize(0); |
Ratan Gupta | 0d3e9e3 | 2017-02-10 22:27:12 +0530 | [diff] [blame] | 195 | std::cerr << "SLP unable to find the service=" << svcName << "\n"; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 196 | return std::make_tuple((int)slp::Error::INTERNAL_ERROR, buff); |
| 197 | } |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 198 | // Get all the interface address |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 199 | auto ifaddrList = slp::handler::internal::getIntfAddrs(); |
| 200 | if (ifaddrList.size() <= 0) |
| 201 | { |
| 202 | buff.resize(0); |
Ratan Gupta | 0d3e9e3 | 2017-02-10 22:27:12 +0530 | [diff] [blame] | 203 | std::cerr << "SLP unable to read the interface address\n"; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 204 | return std::make_tuple((int)slp::Error::INTERNAL_ERROR, buff); |
| 205 | } |
| 206 | |
| 207 | buff = prepareHeader(req); |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 208 | // Calculate the length and resize the buffer |
| 209 | uint8_t length = buff.size() + /* 14 bytes header + length of langtag */ |
| 210 | slp::response::SIZE_ERROR + /* 2 bytes error code */ |
| 211 | slp::response::SIZE_URL_COUNT; /* 2 bytes srvtype len */ |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 212 | |
| 213 | buff.resize(length); |
| 214 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 215 | // Populate the url count |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 216 | uint16_t urlCount = ifaddrList.size(); |
| 217 | urlCount = endian::to_network(urlCount); |
| 218 | |
| 219 | std::copy_n((uint8_t*)&urlCount, slp::response::SIZE_URL_COUNT, |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 220 | buff.data() + slp::response::OFFSET_URL_ENTRY); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 221 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 222 | // Find the service |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 223 | const slp::ConfigData& svc = svcIt->second; |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 224 | // Populate the URL Entries |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 225 | auto pos = slp::response::OFFSET_URL_ENTRY + slp::response::SIZE_URL_COUNT; |
| 226 | for (const auto& addr : ifaddrList) |
| 227 | { |
Patrick Williams | aa902c6 | 2023-04-04 19:59:51 -0500 | [diff] [blame] | 228 | std::string url = svc.name + ':' + svc.type + "//" + addr + ',' + |
| 229 | svc.port; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 230 | |
Andrew Geissler | eebd081 | 2024-05-24 10:05:27 -0500 | [diff] [blame^] | 231 | // See if total response size exceeds our max |
| 232 | uint32_t totalLength = buff.size() + slp::response::SIZE_URL_ENTRY + |
| 233 | url.length(); |
| 234 | if (totalLength > slp::MAX_LEN) |
| 235 | { |
| 236 | std::cerr << "Message response size exceeds maximum allowed: " |
| 237 | << totalLength << " / " << slp::MAX_LEN << std::endl; |
| 238 | buff.resize(0); |
| 239 | return std::make_tuple((int)slp::Error::PARSE_ERROR, buff); |
| 240 | } |
| 241 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 242 | buff.resize(buff.size() + slp::response::SIZE_URL_ENTRY + url.length()); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 243 | |
| 244 | uint8_t reserved = 0; |
| 245 | uint16_t auth = 0; |
| 246 | uint16_t lifetime = endian::to_network<uint16_t>(slp::LIFETIME); |
| 247 | uint16_t urlLength = url.length(); |
| 248 | |
| 249 | std::copy_n((uint8_t*)&reserved, slp::response::SIZE_RESERVED, |
| 250 | buff.data() + pos); |
| 251 | |
| 252 | pos += slp::response::SIZE_RESERVED; |
| 253 | |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 254 | std::copy_n((uint8_t*)&lifetime, slp::response::SIZE_LIFETIME, |
| 255 | buff.data() + pos); |
| 256 | |
| 257 | pos += slp::response::SIZE_LIFETIME; |
| 258 | |
| 259 | urlLength = endian::to_network(urlLength); |
| 260 | std::copy_n((uint8_t*)&urlLength, slp::response::SIZE_URLLENGTH, |
| 261 | buff.data() + pos); |
| 262 | pos += slp::response::SIZE_URLLENGTH; |
| 263 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 264 | 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] | 265 | pos += url.length(); |
| 266 | |
| 267 | std::copy_n((uint8_t*)&auth, slp::response::SIZE_AUTH, |
| 268 | buff.data() + pos); |
| 269 | pos += slp::response::SIZE_AUTH; |
| 270 | } |
| 271 | uint8_t packetLength = buff.size(); |
| 272 | std::copy_n((uint8_t*)&packetLength, slp::header::SIZE_VERSION, |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 273 | buff.data() + slp::header::OFFSET_LENGTH); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 274 | |
| 275 | return std::make_tuple((int)slp::SUCCESS, buff); |
| 276 | } |
| 277 | |
| 278 | std::list<std::string> getIntfAddrs() |
| 279 | { |
| 280 | std::list<std::string> addrList; |
| 281 | |
| 282 | struct ifaddrs* ifaddr; |
| 283 | // attempt to fill struct with ifaddrs |
| 284 | if (getifaddrs(&ifaddr) == -1) |
| 285 | { |
| 286 | return addrList; |
| 287 | } |
| 288 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 289 | slp::deleted_unique_ptr<ifaddrs> ifaddrPtr( |
| 290 | ifaddr, [](ifaddrs* addr) { freeifaddrs(addr); }); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 291 | |
| 292 | ifaddr = nullptr; |
| 293 | |
| 294 | for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next) |
| 295 | { |
| 296 | // walk interfaces |
| 297 | if (ifa->ifa_addr == nullptr) |
| 298 | { |
| 299 | continue; |
| 300 | } |
| 301 | |
| 302 | // get only INET interfaces not ipv6 |
| 303 | if (ifa->ifa_addr->sa_family == AF_INET) |
| 304 | { |
| 305 | // if loopback, or not running ignore |
| 306 | if ((ifa->ifa_flags & IFF_LOOPBACK) || |
| 307 | !(ifa->ifa_flags & IFF_RUNNING)) |
| 308 | { |
| 309 | continue; |
| 310 | } |
| 311 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 312 | char tmp[INET_ADDRSTRLEN] = {0}; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 313 | |
| 314 | inet_ntop(AF_INET, |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 315 | &(((struct sockaddr_in*)(ifa->ifa_addr))->sin_addr), tmp, |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 316 | sizeof(tmp)); |
| 317 | addrList.emplace_back(tmp); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return addrList; |
| 322 | } |
| 323 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 324 | slp::handler::internal::ServiceList readSLPServiceInfo() |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 325 | { |
| 326 | using namespace std::string_literals; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 327 | slp::handler::internal::ServiceList svcLst; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 328 | slp::ConfigData service; |
Ratan Gupta | ead7a3c | 2017-01-05 15:45:09 +0530 | [diff] [blame] | 329 | struct dirent* dent = nullptr; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 330 | |
Ratan Gupta | ead7a3c | 2017-01-05 15:45:09 +0530 | [diff] [blame] | 331 | // Open the services dir and get the service info |
| 332 | // from service files. |
| 333 | // Service File format would be "ServiceName serviceType Port" |
| 334 | DIR* dir = opendir(SERVICE_DIR); |
| 335 | // wrap the pointer into smart pointer. |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 336 | slp::deleted_unique_ptr<DIR> dirPtr(dir, [](DIR* dir) { |
Ratan Gupta | ead7a3c | 2017-01-05 15:45:09 +0530 | [diff] [blame] | 337 | if (!dir) |
| 338 | { |
| 339 | closedir(dir); |
| 340 | } |
| 341 | }); |
| 342 | dir = nullptr; |
| 343 | |
| 344 | if (dirPtr.get()) |
| 345 | { |
| 346 | while ((dent = readdir(dirPtr.get())) != NULL) |
| 347 | { |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 348 | if (dent->d_type == DT_REG) // regular file |
Ratan Gupta | ead7a3c | 2017-01-05 15:45:09 +0530 | [diff] [blame] | 349 | { |
| 350 | auto absFileName = std::string(SERVICE_DIR) + dent->d_name; |
| 351 | std::ifstream readFile(absFileName); |
| 352 | readFile >> service; |
| 353 | service.name = "service:"s + service.name; |
| 354 | svcLst.emplace(service.name, service); |
| 355 | } |
| 356 | } |
Ratan Gupta | ead7a3c | 2017-01-05 15:45:09 +0530 | [diff] [blame] | 357 | } |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 358 | return svcLst; |
| 359 | } |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 360 | } // namespace internal |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 361 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 362 | std::tuple<int, buffer> processRequest(const Message& msg) |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 363 | { |
| 364 | int rc = slp::SUCCESS; |
| 365 | buffer resp(0); |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 366 | std::cout << "SLP Processing Request=" << msg.header.functionID << "\n"; |
Brad Bishop | b5e632a | 2018-02-23 15:22:12 -0500 | [diff] [blame] | 367 | |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 368 | switch (msg.header.functionID) |
| 369 | { |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 370 | case (uint8_t)slp::FunctionType::SRVTYPERQST: |
Patrick Williams | aa902c6 | 2023-04-04 19:59:51 -0500 | [diff] [blame] | 371 | std::tie(rc, |
| 372 | resp) = slp::handler::internal::processSrvTypeRequest(msg); |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 373 | break; |
| 374 | case (uint8_t)slp::FunctionType::SRVRQST: |
| 375 | std::tie(rc, resp) = slp::handler::internal::processSrvRequest(msg); |
| 376 | break; |
| 377 | default: |
| 378 | rc = (uint8_t)slp::Error::MSG_NOT_SUPPORTED; |
| 379 | } |
| 380 | return std::make_tuple(rc, resp); |
| 381 | } |
| 382 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 383 | buffer processError(const Message& req, uint8_t err) |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 384 | { |
| 385 | buffer buff; |
| 386 | buff = slp::handler::internal::prepareHeader(req); |
| 387 | |
Patrick Williams | edf88cb | 2023-04-04 20:16:37 -0500 | [diff] [blame] | 388 | static_assert(sizeof(err) == 1, "Errors should be 1 byte."); |
| 389 | |
| 390 | // Since this is network order, the err should go in the 2nd byte of the |
| 391 | // error field. This is immediately after the langtag. |
| 392 | buff[slp::header::MIN_LEN + req.header.langtag.length() + 1] = err; |
| 393 | |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 394 | return buff; |
Ratan Gupta | 07c462a | 2016-12-14 00:40:30 +0530 | [diff] [blame] | 395 | } |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 396 | } // namespace handler |
| 397 | } // namespace slp |