srt_c_api.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. written by
  12. Haivision Systems Inc.
  13. *****************************************************************************/
  14. #include "platform_sys.h"
  15. #include <iterator>
  16. #include <fstream>
  17. #include "srt.h"
  18. #include "common.h"
  19. #include "packet.h"
  20. #include "core.h"
  21. #include "utilities.h"
  22. using namespace std;
  23. using namespace srt;
  24. extern "C" {
  25. int srt_startup() { return CUDT::startup(); }
  26. int srt_cleanup() { return CUDT::cleanup(); }
  27. // Socket creation.
  28. SRTSOCKET srt_socket(int , int , int ) { return CUDT::socket(); }
  29. SRTSOCKET srt_create_socket() { return CUDT::socket(); }
  30. #if ENABLE_BONDING
  31. // Group management.
  32. SRTSOCKET srt_create_group(SRT_GROUP_TYPE gt) { return CUDT::createGroup(gt); }
  33. SRTSOCKET srt_groupof(SRTSOCKET socket) { return CUDT::getGroupOfSocket(socket); }
  34. int srt_group_data(SRTSOCKET socketgroup, SRT_SOCKGROUPDATA* output, size_t* inoutlen)
  35. {
  36. return CUDT::getGroupData(socketgroup, output, inoutlen);
  37. }
  38. SRT_SOCKOPT_CONFIG* srt_create_config()
  39. {
  40. return new SRT_SocketOptionObject;
  41. }
  42. int srt_config_add(SRT_SOCKOPT_CONFIG* config, SRT_SOCKOPT option, const void* contents, int len)
  43. {
  44. if (!config)
  45. return -1;
  46. if (!config->add(option, contents, len))
  47. return -1;
  48. return 0;
  49. }
  50. int srt_connect_group(SRTSOCKET group,
  51. SRT_SOCKGROUPCONFIG name[], int arraysize)
  52. {
  53. return CUDT::connectLinks(group, name, arraysize);
  54. }
  55. #else
  56. SRTSOCKET srt_create_group(SRT_GROUP_TYPE) { return SRT_INVALID_SOCK; }
  57. SRTSOCKET srt_groupof(SRTSOCKET) { return SRT_INVALID_SOCK; }
  58. int srt_group_data(SRTSOCKET, SRT_SOCKGROUPDATA*, size_t*) { return srt::CUDT::APIError(MJ_NOTSUP, MN_INVAL, 0); }
  59. SRT_SOCKOPT_CONFIG* srt_create_config() { return NULL; }
  60. int srt_config_add(SRT_SOCKOPT_CONFIG*, SRT_SOCKOPT, const void*, int) { return srt::CUDT::APIError(MJ_NOTSUP, MN_INVAL, 0); }
  61. int srt_connect_group(SRTSOCKET, SRT_SOCKGROUPCONFIG[], int) { return srt::CUDT::APIError(MJ_NOTSUP, MN_INVAL, 0); }
  62. #endif
  63. SRT_SOCKGROUPCONFIG srt_prepare_endpoint(const struct sockaddr* src, const struct sockaddr* dst, int namelen)
  64. {
  65. SRT_SOCKGROUPCONFIG data;
  66. #if ENABLE_BONDING
  67. data.errorcode = SRT_SUCCESS;
  68. #else
  69. data.errorcode = SRT_EINVOP;
  70. #endif
  71. data.id = -1;
  72. data.token = -1;
  73. data.weight = 0;
  74. data.config = NULL;
  75. if (src)
  76. memcpy(&data.srcaddr, src, namelen);
  77. else
  78. {
  79. memset(&data.srcaddr, 0, sizeof data.srcaddr);
  80. // Still set the family according to the target address
  81. data.srcaddr.ss_family = dst->sa_family;
  82. }
  83. memcpy(&data.peeraddr, dst, namelen);
  84. return data;
  85. }
  86. void srt_delete_config(SRT_SOCKOPT_CONFIG* in)
  87. {
  88. delete in;
  89. }
  90. // Binding and connection management
  91. int srt_bind(SRTSOCKET u, const struct sockaddr * name, int namelen) { return CUDT::bind(u, name, namelen); }
  92. int srt_bind_acquire(SRTSOCKET u, UDPSOCKET udpsock) { return CUDT::bind(u, udpsock); }
  93. int srt_listen(SRTSOCKET u, int backlog) { return CUDT::listen(u, backlog); }
  94. SRTSOCKET srt_accept(SRTSOCKET u, struct sockaddr * addr, int * addrlen) { return CUDT::accept(u, addr, addrlen); }
  95. SRTSOCKET srt_accept_bond(const SRTSOCKET lsns[], int lsize, int64_t msTimeOut) { return CUDT::accept_bond(lsns, lsize, msTimeOut); }
  96. int srt_connect(SRTSOCKET u, const struct sockaddr * name, int namelen) { return CUDT::connect(u, name, namelen, SRT_SEQNO_NONE); }
  97. int srt_connect_debug(SRTSOCKET u, const struct sockaddr * name, int namelen, int forced_isn) { return CUDT::connect(u, name, namelen, forced_isn); }
  98. int srt_connect_bind(SRTSOCKET u,
  99. const struct sockaddr* source,
  100. const struct sockaddr* target, int target_len)
  101. {
  102. return CUDT::connect(u, source, target, target_len);
  103. }
  104. int srt_rendezvous(SRTSOCKET u, const struct sockaddr* local_name, int local_namelen,
  105. const struct sockaddr* remote_name, int remote_namelen)
  106. {
  107. bool yes = 1;
  108. CUDT::setsockopt(u, 0, SRTO_RENDEZVOUS, &yes, sizeof yes);
  109. // Note: PORT is 16-bit and at the same location in both sockaddr_in and sockaddr_in6.
  110. // Just as a safety precaution, check the structs.
  111. if ( (local_name->sa_family != AF_INET && local_name->sa_family != AF_INET6)
  112. || local_name->sa_family != remote_name->sa_family)
  113. return CUDT::APIError(MJ_NOTSUP, MN_INVAL, 0);
  114. const int st = srt_bind(u, local_name, local_namelen);
  115. if (st != 0)
  116. return st;
  117. return srt_connect(u, remote_name, remote_namelen);
  118. }
  119. int srt_close(SRTSOCKET u)
  120. {
  121. SRT_SOCKSTATUS st = srt_getsockstate(u);
  122. if ((st == SRTS_NONEXIST) ||
  123. (st == SRTS_CLOSED) ||
  124. (st == SRTS_CLOSING) )
  125. {
  126. // It's closed already. Do nothing.
  127. return 0;
  128. }
  129. return CUDT::close(u);
  130. }
  131. int srt_getpeername(SRTSOCKET u, struct sockaddr * name, int * namelen) { return CUDT::getpeername(u, name, namelen); }
  132. int srt_getsockname(SRTSOCKET u, struct sockaddr * name, int * namelen) { return CUDT::getsockname(u, name, namelen); }
  133. int srt_getsockopt(SRTSOCKET u, int level, SRT_SOCKOPT optname, void * optval, int * optlen)
  134. { return CUDT::getsockopt(u, level, optname, optval, optlen); }
  135. int srt_setsockopt(SRTSOCKET u, int level, SRT_SOCKOPT optname, const void * optval, int optlen)
  136. { return CUDT::setsockopt(u, level, optname, optval, optlen); }
  137. int srt_getsockflag(SRTSOCKET u, SRT_SOCKOPT opt, void* optval, int* optlen)
  138. { return CUDT::getsockopt(u, 0, opt, optval, optlen); }
  139. int srt_setsockflag(SRTSOCKET u, SRT_SOCKOPT opt, const void* optval, int optlen)
  140. { return CUDT::setsockopt(u, 0, opt, optval, optlen); }
  141. int srt_send(SRTSOCKET u, const char * buf, int len) { return CUDT::send(u, buf, len, 0); }
  142. int srt_recv(SRTSOCKET u, char * buf, int len) { return CUDT::recv(u, buf, len, 0); }
  143. int srt_sendmsg(SRTSOCKET u, const char * buf, int len, int ttl, int inorder) { return CUDT::sendmsg(u, buf, len, ttl, 0!= inorder); }
  144. int srt_recvmsg(SRTSOCKET u, char * buf, int len) { int64_t ign_srctime; return CUDT::recvmsg(u, buf, len, ign_srctime); }
  145. int64_t srt_sendfile(SRTSOCKET u, const char* path, int64_t* offset, int64_t size, int block)
  146. {
  147. if (!path || !offset )
  148. {
  149. return CUDT::APIError(MJ_NOTSUP, MN_INVAL, 0);
  150. }
  151. fstream ifs(path, ios::binary | ios::in);
  152. if (!ifs)
  153. {
  154. return CUDT::APIError(MJ_FILESYSTEM, MN_READFAIL, 0);
  155. }
  156. int64_t ret = CUDT::sendfile(u, ifs, *offset, size, block);
  157. ifs.close();
  158. return ret;
  159. }
  160. int64_t srt_recvfile(SRTSOCKET u, const char* path, int64_t* offset, int64_t size, int block)
  161. {
  162. if (!path || !offset )
  163. {
  164. return CUDT::APIError(MJ_NOTSUP, MN_INVAL, 0);
  165. }
  166. fstream ofs(path, ios::binary | ios::out);
  167. if (!ofs)
  168. {
  169. return CUDT::APIError(MJ_FILESYSTEM, MN_WRAVAIL, 0);
  170. }
  171. int64_t ret = CUDT::recvfile(u, ofs, *offset, size, block);
  172. ofs.close();
  173. return ret;
  174. }
  175. extern const SRT_MSGCTRL srt_msgctrl_default = {
  176. 0, // no flags set
  177. SRT_MSGTTL_INF,
  178. false, // not in order (matters for msg mode only)
  179. PB_SUBSEQUENT,
  180. 0, // srctime: take "now" time
  181. SRT_SEQNO_NONE,
  182. SRT_MSGNO_NONE,
  183. NULL, // grpdata not supplied
  184. 0 // idem
  185. };
  186. void srt_msgctrl_init(SRT_MSGCTRL* mctrl)
  187. {
  188. *mctrl = srt_msgctrl_default;
  189. }
  190. int srt_sendmsg2(SRTSOCKET u, const char * buf, int len, SRT_MSGCTRL *mctrl)
  191. {
  192. // Allow NULL mctrl in the API, but not internally.
  193. if (mctrl)
  194. return CUDT::sendmsg2(u, buf, len, (*mctrl));
  195. SRT_MSGCTRL mignore = srt_msgctrl_default;
  196. return CUDT::sendmsg2(u, buf, len, (mignore));
  197. }
  198. int srt_recvmsg2(SRTSOCKET u, char * buf, int len, SRT_MSGCTRL *mctrl)
  199. {
  200. if (mctrl)
  201. return CUDT::recvmsg2(u, buf, len, (*mctrl));
  202. SRT_MSGCTRL mignore = srt_msgctrl_default;
  203. return CUDT::recvmsg2(u, buf, len, (mignore));
  204. }
  205. const char* srt_getlasterror_str() { return UDT::getlasterror().getErrorMessage(); }
  206. int srt_getlasterror(int* loc_errno)
  207. {
  208. if ( loc_errno )
  209. *loc_errno = UDT::getlasterror().getErrno();
  210. return CUDT::getlasterror().getErrorCode();
  211. }
  212. const char* srt_strerror(int code, int err)
  213. {
  214. static srt::CUDTException e;
  215. e = srt::CUDTException(CodeMajor(code/1000), CodeMinor(code%1000), err);
  216. return(e.getErrorMessage());
  217. }
  218. void srt_clearlasterror()
  219. {
  220. UDT::getlasterror().clear();
  221. }
  222. int srt_bstats(SRTSOCKET u, SRT_TRACEBSTATS * perf, int clear) { return CUDT::bstats(u, perf, 0!= clear); }
  223. int srt_bistats(SRTSOCKET u, SRT_TRACEBSTATS * perf, int clear, int instantaneous) { return CUDT::bstats(u, perf, 0!= clear, 0!= instantaneous); }
  224. SRT_SOCKSTATUS srt_getsockstate(SRTSOCKET u) { return SRT_SOCKSTATUS((int)CUDT::getsockstate(u)); }
  225. // event mechanism
  226. int srt_epoll_create() { return CUDT::epoll_create(); }
  227. int srt_epoll_clear_usocks(int eit) { return CUDT::epoll_clear_usocks(eit); }
  228. // You can use either SRT_EPOLL_* flags or EPOLL* flags from <sys/epoll.h>, both are the same. IN/OUT/ERR only.
  229. // events == NULL accepted, in which case all flags are set.
  230. int srt_epoll_add_usock(int eid, SRTSOCKET u, const int * events) { return CUDT::epoll_add_usock(eid, u, events); }
  231. int srt_epoll_add_ssock(int eid, SYSSOCKET s, const int * events)
  232. {
  233. int flag = 0;
  234. if (events) {
  235. flag = *events;
  236. } else {
  237. flag = SRT_EPOLL_IN | SRT_EPOLL_OUT | SRT_EPOLL_ERR;
  238. }
  239. // call UDT native function
  240. return CUDT::epoll_add_ssock(eid, s, &flag);
  241. }
  242. int srt_epoll_remove_usock(int eid, SRTSOCKET u) { return CUDT::epoll_remove_usock(eid, u); }
  243. int srt_epoll_remove_ssock(int eid, SYSSOCKET s) { return CUDT::epoll_remove_ssock(eid, s); }
  244. int srt_epoll_update_usock(int eid, SRTSOCKET u, const int * events)
  245. {
  246. return CUDT::epoll_update_usock(eid, u, events);
  247. }
  248. int srt_epoll_update_ssock(int eid, SYSSOCKET s, const int * events)
  249. {
  250. int flag = 0;
  251. if (events) {
  252. flag = *events;
  253. } else {
  254. flag = SRT_EPOLL_IN | SRT_EPOLL_OUT | SRT_EPOLL_ERR;
  255. }
  256. // call UDT native function
  257. return CUDT::epoll_update_ssock(eid, s, &flag);
  258. }
  259. int srt_epoll_wait(
  260. int eid,
  261. SRTSOCKET* readfds, int* rnum, SRTSOCKET* writefds, int* wnum,
  262. int64_t msTimeOut,
  263. SYSSOCKET* lrfds, int* lrnum, SYSSOCKET* lwfds, int* lwnum)
  264. {
  265. return UDT::epoll_wait2(
  266. eid,
  267. readfds, rnum, writefds, wnum,
  268. msTimeOut,
  269. lrfds, lrnum, lwfds, lwnum);
  270. }
  271. int srt_epoll_uwait(int eid, SRT_EPOLL_EVENT* fdsSet, int fdsSize, int64_t msTimeOut)
  272. {
  273. return UDT::epoll_uwait(
  274. eid,
  275. fdsSet,
  276. fdsSize,
  277. msTimeOut);
  278. }
  279. // use this function to set flags. Default flags are always "everything unset".
  280. // Pass 0 here to clear everything, or nonzero to set a desired flag.
  281. // Pass -1 to not change anything (but still get the current flag value).
  282. int32_t srt_epoll_set(int eid, int32_t flags) { return CUDT::epoll_set(eid, flags); }
  283. int srt_epoll_release(int eid) { return CUDT::epoll_release(eid); }
  284. void srt_setloglevel(int ll)
  285. {
  286. UDT::setloglevel(srt_logging::LogLevel::type(ll));
  287. }
  288. void srt_addlogfa(int fa)
  289. {
  290. UDT::addlogfa(srt_logging::LogFA(fa));
  291. }
  292. void srt_dellogfa(int fa)
  293. {
  294. UDT::dellogfa(srt_logging::LogFA(fa));
  295. }
  296. void srt_resetlogfa(const int* fara, size_t fara_size)
  297. {
  298. UDT::resetlogfa(fara, fara_size);
  299. }
  300. void srt_setloghandler(void* opaque, SRT_LOG_HANDLER_FN* handler)
  301. {
  302. UDT::setloghandler(opaque, handler);
  303. }
  304. void srt_setlogflags(int flags)
  305. {
  306. UDT::setlogflags(flags);
  307. }
  308. int srt_getsndbuffer(SRTSOCKET sock, size_t* blocks, size_t* bytes)
  309. {
  310. return CUDT::getsndbuffer(sock, blocks, bytes);
  311. }
  312. int srt_getrejectreason(SRTSOCKET sock)
  313. {
  314. return CUDT::rejectReason(sock);
  315. }
  316. int srt_setrejectreason(SRTSOCKET sock, int value)
  317. {
  318. return CUDT::rejectReason(sock, value);
  319. }
  320. int srt_listen_callback(SRTSOCKET lsn, srt_listen_callback_fn* hook, void* opaq)
  321. {
  322. if (!hook)
  323. return CUDT::APIError(MJ_NOTSUP, MN_INVAL);
  324. return CUDT::installAcceptHook(lsn, hook, opaq);
  325. }
  326. int srt_connect_callback(SRTSOCKET lsn, srt_connect_callback_fn* hook, void* opaq)
  327. {
  328. if (!hook)
  329. return CUDT::APIError(MJ_NOTSUP, MN_INVAL);
  330. return CUDT::installConnectHook(lsn, hook, opaq);
  331. }
  332. uint32_t srt_getversion()
  333. {
  334. return SrtVersion(SRT_VERSION_MAJOR, SRT_VERSION_MINOR, SRT_VERSION_PATCH);
  335. }
  336. int64_t srt_time_now()
  337. {
  338. return srt::sync::count_microseconds(srt::sync::steady_clock::now().time_since_epoch());
  339. }
  340. int64_t srt_connection_time(SRTSOCKET sock)
  341. {
  342. return CUDT::socketStartTime(sock);
  343. }
  344. int srt_clock_type()
  345. {
  346. return SRT_SYNC_CLOCK;
  347. }
  348. const char* const srt_rejection_reason_msg [] = {
  349. "Unknown or erroneous",
  350. "Error in system calls",
  351. "Peer rejected connection",
  352. "Resource allocation failure",
  353. "Rogue peer or incorrect parameters",
  354. "Listener's backlog exceeded",
  355. "Internal Program Error",
  356. "Socket is being closed",
  357. "Peer version too old",
  358. "Rendezvous-mode cookie collision",
  359. "Incorrect passphrase",
  360. "Password required or unexpected",
  361. "MessageAPI/StreamAPI collision",
  362. "Congestion controller type collision",
  363. "Packet Filter settings error",
  364. "Group settings collision",
  365. "Connection timeout"
  366. #ifdef ENABLE_AEAD_API_PREVIEW
  367. ,"Crypto mode"
  368. #endif
  369. };
  370. // Deprecated, available in SRT API.
  371. extern const char* const srt_rejectreason_msg[] = {
  372. srt_rejection_reason_msg[0],
  373. srt_rejection_reason_msg[1],
  374. srt_rejection_reason_msg[2],
  375. srt_rejection_reason_msg[3],
  376. srt_rejection_reason_msg[4],
  377. srt_rejection_reason_msg[5],
  378. srt_rejection_reason_msg[6],
  379. srt_rejection_reason_msg[7],
  380. srt_rejection_reason_msg[8],
  381. srt_rejection_reason_msg[9],
  382. srt_rejection_reason_msg[10],
  383. srt_rejection_reason_msg[11],
  384. srt_rejection_reason_msg[12],
  385. srt_rejection_reason_msg[13],
  386. srt_rejection_reason_msg[14],
  387. srt_rejection_reason_msg[15],
  388. srt_rejection_reason_msg[16]
  389. #ifdef ENABLE_AEAD_API_PREVIEW
  390. , srt_rejection_reason_msg[17]
  391. #endif
  392. };
  393. const char* srt_rejectreason_str(int id)
  394. {
  395. if (id >= SRT_REJC_PREDEFINED)
  396. {
  397. return "Application-defined rejection reason";
  398. }
  399. static const size_t ra_size = Size(srt_rejection_reason_msg);
  400. if (size_t(id) >= ra_size)
  401. return srt_rejection_reason_msg[0];
  402. return srt_rejection_reason_msg[id];
  403. }
  404. }