| 1 |
/* |
| 2 |
* SRT - Secure, Reliable, Transport |
| 3 |
* Copyright (c) 2018 Haivision Systems Inc. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
#ifndef INC_SRT_APPCOMMON_H |
| 13 |
#define INC_SRT_APPCOMMON_H |
| 14 |
|
| 15 |
#include <string> |
| 16 |
#include <map> |
| 17 |
#include <set> |
| 18 |
#include <vector> |
| 19 |
#include <memory> |
| 20 |
|
| 21 |
#include "netinet_any.h" |
| 22 |
#include "utilities.h" |
| 23 |
#include "srt.h" |
| 24 |
|
| 25 |
#if _WIN32 |
| 26 |
|
| 27 |
// Keep this below commented out. |
| 28 |
// This is for a case when you need cpp debugging on Windows. |
| 29 |
//#ifdef _WINSOCKAPI_ |
| 30 |
//#error "You include <winsock.h> somewhere, remove it. It causes conflicts" |
| 31 |
//#endif |
| 32 |
|
| 33 |
#include <winsock2.h> |
| 34 |
#include <windows.h> |
| 35 |
#include <ws2tcpip.h> |
| 36 |
// WIN32 API does not have sleep() and usleep(), Although MINGW does. |
| 37 |
#ifdef __MINGW32__ |
| 38 |
#include <unistd.h> |
| 39 |
#else |
| 40 |
extern "C" inline int sleep(int seconds) { Sleep(seconds * 1000); return 0; } |
| 41 |
#endif |
| 42 |
|
| 43 |
inline bool SysInitializeNetwork() |
| 44 |
{ |
| 45 |
WORD wVersionRequested = MAKEWORD(2, 2); |
| 46 |
WSADATA wsaData; |
| 47 |
return WSAStartup(wVersionRequested, &wsaData) == 0; |
| 48 |
} |
| 49 |
|
| 50 |
inline void SysCleanupNetwork() |
| 51 |
{ |
| 52 |
WSACleanup(); |
| 53 |
} |
| 54 |
|
| 55 |
#else |
| 56 |
#include <netdb.h> |
| 57 |
#include <sys/socket.h> |
| 58 |
#include <netinet/in.h> |
| 59 |
#include <arpa/inet.h> |
| 60 |
#include <unistd.h> |
| 61 |
|
| 62 |
// Fixes Android build on NDK r16b and earlier. |
| 63 |
#if defined(__ANDROID__) && (__ANDROID__ == 1) |
| 64 |
#include <android/ndk-version.h> |
| 65 |
#if !defined(__NDK_MAJOR__) || (__NDK_MAJOR__ <= 16) |
| 66 |
struct ip_mreq_sourceFIXED { |
| 67 |
struct in_addr imr_multiaddr; |
| 68 |
struct in_addr imr_interface; |
| 69 |
struct in_addr imr_sourceaddr; |
| 70 |
}; |
| 71 |
#define ip_mreq_source ip_mreq_sourceFIXED |
| 72 |
#endif |
| 73 |
#endif |
| 74 |
|
| 75 |
// Nothing needs to be done on POSIX; this is a Windows problem. |
| 76 |
inline bool SysInitializeNetwork() {return true;} |
| 77 |
inline void SysCleanupNetwork() {} |
| 78 |
|
| 79 |
#endif |
| 80 |
|
| 81 |
#ifdef _WIN32 |
| 82 |
inline int SysError() { return ::GetLastError(); } |
| 83 |
const int SysAGAIN = WSAEWOULDBLOCK; |
| 84 |
#else |
| 85 |
inline int SysError() { return errno; } |
| 86 |
const int SysAGAIN = EAGAIN; |
| 87 |
#endif |
| 88 |
|
| 89 |
srt::sockaddr_any CreateAddr(const std::string& name, unsigned short port = 0, int pref_family = AF_UNSPEC); |
| 90 |
std::string Join(const std::vector<std::string>& in, std::string sep); |
| 91 |
|
| 92 |
template <class VarType, class ValType> |
| 93 |
struct OnReturnSetter |
| 94 |
{ |
| 95 |
VarType& var; |
| 96 |
ValType value; |
| 97 |
|
| 98 |
OnReturnSetter(VarType& target, ValType v): var(target), value(v) {} |
| 99 |
~OnReturnSetter() { var = value; } |
| 100 |
}; |
| 101 |
|
| 102 |
template <class VarType, class ValType> |
| 103 |
OnReturnSetter<VarType, ValType> OnReturnSet(VarType& target, ValType v) |
| 104 |
{ return OnReturnSetter<VarType, ValType>(target, v); } |
| 105 |
|
| 106 |
// ---- OPTIONS MODULE |
| 107 |
|
| 108 |
inline bool CheckTrue(const std::vector<std::string>& in) |
| 109 |
{ |
| 110 |
if (in.empty()) |
| 111 |
return true; |
| 112 |
|
| 113 |
const std::set<std::string> false_vals = { "0", "no", "off", "false" }; |
| 114 |
if (false_vals.count(in[0])) |
| 115 |
return false; |
| 116 |
|
| 117 |
return true; |
| 118 |
|
| 119 |
//if (in[0] != "false" && in[0] != "off") |
| 120 |
// return true; |
| 121 |
|
| 122 |
//return false; |
| 123 |
} |
| 124 |
|
| 125 |
template<class Number> |
| 126 |
static inline Number StrToNumber(const std::string& ) |
| 127 |
{ |
| 128 |
typename Number::incorrect_version wrong = Number::incorrect_version; |
| 129 |
return Number(); |
| 130 |
} |
| 131 |
|
| 132 |
#define STON(type, function) \ |
| 133 |
template<> inline type StrToNumber(const std::string& s) { return function (s, 0, 0); } |
| 134 |
|
| 135 |
STON(int, stoi); |
| 136 |
STON(unsigned long, stoul); |
| 137 |
STON(unsigned int, stoul); |
| 138 |
STON(long long, stoll); |
| 139 |
STON(unsigned long long, stoull); |
| 140 |
|
| 141 |
#undef STON |
| 142 |
|
| 143 |
typedef std::map<std::string, std::vector<std::string>> options_t; |
| 144 |
|
| 145 |
struct OutList |
| 146 |
{ |
| 147 |
typedef std::vector<std::string> type; |
| 148 |
static type process(const options_t::mapped_type& i) { return i; } |
| 149 |
}; |
| 150 |
|
| 151 |
struct OutString |
| 152 |
{ |
| 153 |
typedef std::string type; |
| 154 |
static type process(const options_t::mapped_type& i) { return Join(i, " "); } |
| 155 |
}; |
| 156 |
|
| 157 |
struct NumberAutoConvert |
| 158 |
{ |
| 159 |
std::string value; |
| 160 |
|
| 161 |
NumberAutoConvert(): NumberAutoConvert("") {} |
| 162 |
NumberAutoConvert(const std::string& arg): NumberAutoConvert(arg.c_str()) {} |
| 163 |
NumberAutoConvert(const char* arg): value(arg) |
| 164 |
{ |
| 165 |
if (value.empty()) |
| 166 |
value = "0"; // Must convert to a default 0 number |
| 167 |
} |
| 168 |
|
| 169 |
template<class Number> |
| 170 |
operator Number() |
| 171 |
{ |
| 172 |
return StrToNumber<Number>(value); |
| 173 |
} |
| 174 |
}; |
| 175 |
|
| 176 |
struct OutNumber |
| 177 |
{ |
| 178 |
typedef NumberAutoConvert type; |
| 179 |
static type process(const options_t::mapped_type& i) |
| 180 |
{ |
| 181 |
// Numbers can't be joined, use the "last overrides" rule. |
| 182 |
if (i.empty()) |
| 183 |
return {"0"}; |
| 184 |
|
| 185 |
return type { i.back() }; |
| 186 |
} |
| 187 |
}; |
| 188 |
|
| 189 |
template <class Number> |
| 190 |
struct OutNumberAs |
| 191 |
{ |
| 192 |
typedef Number type; |
| 193 |
static type process(const options_t::mapped_type& i) |
| 194 |
{ |
| 195 |
return OutNumber::process(i); |
| 196 |
} |
| 197 |
}; |
| 198 |
|
| 199 |
|
| 200 |
struct OutBool |
| 201 |
{ |
| 202 |
typedef bool type; |
| 203 |
static type process(const options_t::mapped_type& i) { return CheckTrue(i); } |
| 204 |
}; |
| 205 |
|
| 206 |
struct OptionName; |
| 207 |
|
| 208 |
struct OptionScheme |
| 209 |
{ |
| 210 |
const OptionName* pid; |
| 211 |
enum Args { ARG_NONE, ARG_ONE, ARG_VAR } type; |
| 212 |
|
| 213 |
OptionScheme(const OptionScheme&) = default; |
| 214 |
OptionScheme(OptionScheme&& src) |
| 215 |
: pid(src.pid) |
| 216 |
, type(src.type) |
| 217 |
{ |
| 218 |
} |
| 219 |
|
| 220 |
OptionScheme(const OptionName& id, Args tp); |
| 221 |
|
| 222 |
const std::set<std::string>& names() const; |
| 223 |
}; |
| 224 |
|
| 225 |
struct OptionName |
| 226 |
{ |
| 227 |
std::string helptext; |
| 228 |
std::string main_name; |
| 229 |
std::set<std::string> names; |
| 230 |
|
| 231 |
template <class... Args> |
| 232 |
OptionName(std::string ht, std::string first, Args... rest) |
| 233 |
: helptext(ht), main_name(first), |
| 234 |
names {first, rest...} |
| 235 |
{ |
| 236 |
} |
| 237 |
|
| 238 |
template <class... Args> |
| 239 |
OptionName(std::vector<OptionScheme>& sc, OptionScheme::Args type, |
| 240 |
std::string ht, std::string first, Args... rest) |
| 241 |
: helptext(ht), main_name(first), |
| 242 |
names {first, rest...} |
| 243 |
{ |
| 244 |
sc.push_back(OptionScheme(*this, type)); |
| 245 |
} |
| 246 |
|
| 247 |
template <class... Args> |
| 248 |
OptionName(std::vector<OptionScheme>& sc, |
| 249 |
std::string ht, std::string first, Args... rest) |
| 250 |
: helptext(ht), main_name(first), |
| 251 |
names {first, rest...} |
| 252 |
{ |
| 253 |
OptionScheme::Args type = DetermineTypeFromHelpText(ht); |
| 254 |
sc.push_back(OptionScheme(*this, type)); |
| 255 |
} |
| 256 |
|
| 257 |
OptionName(std::initializer_list<std::string> args): main_name(*args.begin()), names(args) {} |
| 258 |
|
| 259 |
operator std::set<std::string>() { return names; } |
| 260 |
operator const std::set<std::string>() const { return names; } |
| 261 |
|
| 262 |
private: |
| 263 |
static OptionScheme::Args DetermineTypeFromHelpText(const std::string& helptext); |
| 264 |
}; |
| 265 |
|
| 266 |
inline OptionScheme::OptionScheme(const OptionName& id, Args tp): pid(&id), type(tp) {} |
| 267 |
inline const std::set<std::string>& OptionScheme::names() const { return pid->names; } |
| 268 |
|
| 269 |
template <class OutType, class OutValue> inline |
| 270 |
typename OutType::type Option(const options_t&, OutValue deflt=OutValue()) { return deflt; } |
| 271 |
|
| 272 |
template <class OutType, class OutValue, class... Args> inline |
| 273 |
typename OutType::type Option(const options_t& options, OutValue deflt, std::string key, Args... further_keys) |
| 274 |
{ |
| 275 |
auto i = options.find(key); |
| 276 |
if ( i == options.end() ) |
| 277 |
return Option<OutType>(options, deflt, further_keys...); |
| 278 |
return OutType::process(i->second); |
| 279 |
} |
| 280 |
|
| 281 |
template<typename TrapType> |
| 282 |
struct OptionTrapType |
| 283 |
{ |
| 284 |
static TrapType pass(TrapType v) { return v; } |
| 285 |
}; |
| 286 |
|
| 287 |
template<> |
| 288 |
struct OptionTrapType<const char*> |
| 289 |
{ |
| 290 |
static std::string pass(const char* v) { return v; } |
| 291 |
}; |
| 292 |
|
| 293 |
template <class OutType, class OutValue> inline |
| 294 |
typename OutType::type Option(const options_t& options, OutValue deflt, const OptionName& oname) |
| 295 |
{ |
| 296 |
(void)OptionTrapType<OutValue>::pass(deflt); |
| 297 |
for (auto key: oname.names) |
| 298 |
{ |
| 299 |
auto i = options.find(key); |
| 300 |
if ( i != options.end() ) |
| 301 |
{ |
| 302 |
return OutType::process(i->second); |
| 303 |
} |
| 304 |
} |
| 305 |
return deflt; |
| 306 |
} |
| 307 |
|
| 308 |
template <class OutType> inline |
| 309 |
typename OutType::type Option(const options_t& options, const OptionName& oname) |
| 310 |
{ |
| 311 |
typedef typename OutType::type out_t; |
| 312 |
for (auto key: oname.names) |
| 313 |
{ |
| 314 |
auto i = options.find(key); |
| 315 |
if ( i != options.end() ) |
| 316 |
{ |
| 317 |
return OutType::process(i->second); |
| 318 |
} |
| 319 |
} |
| 320 |
return out_t(); |
| 321 |
} |
| 322 |
|
| 323 |
inline bool OptionPresent(const options_t& options, const std::set<std::string>& keys) |
| 324 |
{ |
| 325 |
for (auto key: keys) |
| 326 |
{ |
| 327 |
auto i = options.find(key); |
| 328 |
if ( i != options.end() ) |
| 329 |
return true; |
| 330 |
} |
| 331 |
return false; |
| 332 |
} |
| 333 |
|
| 334 |
options_t ProcessOptions(char* const* argv, int argc, std::vector<OptionScheme> scheme); |
| 335 |
std::string OptionHelpItem(const OptionName& o); |
| 336 |
|
| 337 |
const char* SRTClockTypeStr(); |
| 338 |
void PrintLibVersion(); |
| 339 |
|
| 340 |
|
| 341 |
namespace srt |
| 342 |
{ |
| 343 |
|
| 344 |
struct OptionSetterProxy |
| 345 |
{ |
| 346 |
SRTSOCKET s; |
| 347 |
int result = -1; |
| 348 |
|
| 349 |
OptionSetterProxy(SRTSOCKET ss): s(ss) {} |
| 350 |
|
| 351 |
struct OptionProxy |
| 352 |
{ |
| 353 |
OptionSetterProxy& parent; |
| 354 |
SRT_SOCKOPT opt; |
| 355 |
|
| 356 |
#define SPEC(type) \ |
| 357 |
OptionProxy& operator=(const type& val)\ |
| 358 |
{\ |
| 359 |
parent.result = srt_setsockflag(parent.s, opt, &val, sizeof val);\ |
| 360 |
return *this;\ |
| 361 |
} |
| 362 |
|
| 363 |
SPEC(int32_t); |
| 364 |
SPEC(int64_t); |
| 365 |
SPEC(bool); |
| 366 |
#undef SPEC |
| 367 |
|
| 368 |
template<size_t N> |
| 369 |
OptionProxy& operator=(const char (&val)[N]) |
| 370 |
{ |
| 371 |
parent.result = srt_setsockflag(parent.s, opt, val, N-1); |
| 372 |
return *this; |
| 373 |
} |
| 374 |
|
| 375 |
OptionProxy& operator=(const std::string& val) |
| 376 |
{ |
| 377 |
parent.result = srt_setsockflag(parent.s, opt, val.c_str(), val.size()); |
| 378 |
return *this; |
| 379 |
} |
| 380 |
}; |
| 381 |
|
| 382 |
OptionProxy operator[](SRT_SOCKOPT opt) |
| 383 |
{ |
| 384 |
return OptionProxy {*this, opt}; |
| 385 |
} |
| 386 |
|
| 387 |
operator int() { return result; } |
| 388 |
}; |
| 389 |
|
| 390 |
inline OptionSetterProxy setopt(SRTSOCKET socket) |
| 391 |
{ |
| 392 |
return OptionSetterProxy(socket); |
| 393 |
} |
| 394 |
|
| 395 |
} |
| 396 |
#endif // INC_SRT_APPCOMMON_H |