You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

371 lines
11KB

  1. /*
  2. * SCTP protocol
  3. * Copyright (c) 2012 Luca Barbato
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. *
  24. * sctp url_protocol
  25. *
  26. * url syntax: sctp://host:port[?option=val...]
  27. * option: 'listen' : listen for an incoming connection
  28. * 'max_streams=n' : set the maximum number of streams
  29. * 'reuse=1' : enable reusing the socket [TBD]
  30. *
  31. * by setting the maximum number of streams the protocol will use the
  32. * first two bytes of the incoming/outgoing buffer to store the
  33. * stream number of the packet being read/written.
  34. * @see sctp_read
  35. * @see sctp_write
  36. */
  37. #include <netinet/in.h>
  38. #include <netinet/sctp.h>
  39. #include "config.h"
  40. #if HAVE_POLL_H
  41. #include <poll.h>
  42. #endif
  43. #include "libavutil/intreadwrite.h"
  44. #include "libavutil/parseutils.h"
  45. #include "libavutil/opt.h"
  46. #include "avformat.h"
  47. #include "internal.h"
  48. #include "network.h"
  49. #include "os_support.h"
  50. #include "url.h"
  51. /*
  52. * The sctp_recvmsg and sctp_sendmsg functions are part of the user
  53. * library that offers support for the SCTP kernel Implementation.
  54. * To avoid build-time clashes the functions sport an ff_-prefix here.
  55. * The main purpose of this code is to provide the SCTP Socket API
  56. * mappings for user applications to interface with SCTP in the kernel.
  57. *
  58. * This implementation is based on the Socket API Extensions for SCTP
  59. * defined in <draft-ietf-tsvwg-sctpsocket-10.txt>
  60. *
  61. * Copyright (c) 2003 International Business Machines, Corp.
  62. *
  63. * Written or modified by:
  64. * Ryan Layer <rmlayer@us.ibm.com>
  65. */
  66. static int ff_sctp_recvmsg(int s, void *msg, size_t len, struct sockaddr *from,
  67. socklen_t *fromlen, struct sctp_sndrcvinfo *sinfo,
  68. int *msg_flags)
  69. {
  70. int recvb;
  71. struct iovec iov;
  72. char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
  73. struct msghdr inmsg = { 0 };
  74. struct cmsghdr *cmsg = NULL;
  75. iov.iov_base = msg;
  76. iov.iov_len = len;
  77. inmsg.msg_name = from;
  78. inmsg.msg_namelen = fromlen ? *fromlen : 0;
  79. inmsg.msg_iov = &iov;
  80. inmsg.msg_iovlen = 1;
  81. inmsg.msg_control = incmsg;
  82. inmsg.msg_controllen = sizeof(incmsg);
  83. if ((recvb = recvmsg(s, &inmsg, msg_flags ? *msg_flags : 0)) < 0)
  84. return recvb;
  85. if (fromlen)
  86. *fromlen = inmsg.msg_namelen;
  87. if (msg_flags)
  88. *msg_flags = inmsg.msg_flags;
  89. for (cmsg = CMSG_FIRSTHDR(&inmsg); cmsg != NULL;
  90. cmsg = CMSG_NXTHDR(&inmsg, cmsg)) {
  91. if ((IPPROTO_SCTP == cmsg->cmsg_level) &&
  92. (SCTP_SNDRCV == cmsg->cmsg_type))
  93. break;
  94. }
  95. /* Copy sinfo. */
  96. if (cmsg)
  97. memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_sndrcvinfo));
  98. return recvb;
  99. }
  100. static int ff_sctp_send(int s, const void *msg, size_t len,
  101. const struct sctp_sndrcvinfo *sinfo, int flags)
  102. {
  103. struct msghdr outmsg = { 0 };
  104. struct iovec iov;
  105. outmsg.msg_name = NULL;
  106. outmsg.msg_namelen = 0;
  107. outmsg.msg_iov = &iov;
  108. iov.iov_base = msg;
  109. iov.iov_len = len;
  110. outmsg.msg_iovlen = 1;
  111. outmsg.msg_controllen = 0;
  112. if (sinfo) {
  113. char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
  114. struct cmsghdr *cmsg;
  115. outmsg.msg_control = outcmsg;
  116. outmsg.msg_controllen = sizeof(outcmsg);
  117. outmsg.msg_flags = 0;
  118. cmsg = CMSG_FIRSTHDR(&outmsg);
  119. cmsg->cmsg_level = IPPROTO_SCTP;
  120. cmsg->cmsg_type = SCTP_SNDRCV;
  121. cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
  122. outmsg.msg_controllen = cmsg->cmsg_len;
  123. memcpy(CMSG_DATA(cmsg), sinfo, sizeof(struct sctp_sndrcvinfo));
  124. }
  125. return sendmsg(s, &outmsg, flags | MSG_NOSIGNAL);
  126. }
  127. typedef struct SCTPContext {
  128. const AVClass *class;
  129. int fd;
  130. int listen;
  131. int timeout;
  132. int listen_timeout;
  133. int max_streams;
  134. struct sockaddr_storage dest_addr;
  135. } SCTPContext;
  136. #define OFFSET(x) offsetof(SCTPContext, x)
  137. #define D AV_OPT_FLAG_DECODING_PARAM
  138. #define E AV_OPT_FLAG_ENCODING_PARAM
  139. static const AVOption options[] = {
  140. { "listen", "Listen for incoming connections", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = D|E },
  141. { "timeout", "Connection timeout (in milliseconds)", OFFSET(timeout), AV_OPT_TYPE_INT, { .i64 = 10000 }, INT_MIN, INT_MAX, .flags = D|E },
  142. { "listen_timeout", "Bind timeout (in milliseconds)", OFFSET(listen_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, INT_MIN, INT_MAX, .flags = D|E },
  143. { "max_streams", "Max stream to allocate", OFFSET(max_streams), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT16_MAX, .flags = D|E },
  144. { NULL }
  145. };
  146. static const AVClass sctp_class = {
  147. .class_name = "sctp",
  148. .item_name = av_default_item_name,
  149. .option = options,
  150. .version = LIBAVUTIL_VERSION_INT,
  151. };
  152. static int sctp_open(URLContext *h, const char *uri, int flags)
  153. {
  154. struct addrinfo *ai, *cur_ai;
  155. struct addrinfo hints = { 0 };
  156. struct sctp_event_subscribe event = { 0 };
  157. struct sctp_initmsg initparams = { 0 };
  158. int port;
  159. int fd = -1;
  160. SCTPContext *s = h->priv_data;
  161. const char *p;
  162. char buf[256];
  163. int ret;
  164. char hostname[1024], proto[1024], path[1024];
  165. char portstr[10];
  166. av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
  167. &port, path, sizeof(path), uri);
  168. if (strcmp(proto, "sctp"))
  169. return AVERROR(EINVAL);
  170. if (port <= 0 || port >= 65536) {
  171. av_log(s, AV_LOG_ERROR, "Port missing in uri\n");
  172. return AVERROR(EINVAL);
  173. }
  174. p = strchr(uri, '?');
  175. if (p) {
  176. if (av_find_info_tag(buf, sizeof(buf), "listen", p))
  177. s->listen = 1;
  178. if (av_find_info_tag(buf, sizeof(buf), "max_streams", p))
  179. s->max_streams = strtol(buf, NULL, 10);
  180. }
  181. hints.ai_family = AF_UNSPEC;
  182. hints.ai_socktype = SOCK_STREAM;
  183. snprintf(portstr, sizeof(portstr), "%d", port);
  184. ret = getaddrinfo(hostname, portstr, &hints, &ai);
  185. if (ret) {
  186. av_log(h, AV_LOG_ERROR, "Failed to resolve hostname %s: %s\n",
  187. hostname, gai_strerror(ret));
  188. return AVERROR(EIO);
  189. }
  190. cur_ai = ai;
  191. restart:
  192. fd = ff_socket(cur_ai->ai_family, SOCK_STREAM, IPPROTO_SCTP);
  193. if (fd < 0) {
  194. ret = ff_neterrno();
  195. goto fail;
  196. }
  197. if (s->listen) {
  198. if ((fd = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  199. s->listen_timeout, h)) < 0) {
  200. ret = fd;
  201. goto fail1;
  202. }
  203. } else {
  204. if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  205. s->timeout, h, !!cur_ai->ai_next)) < 0) {
  206. if (ret == AVERROR_EXIT)
  207. goto fail1;
  208. else
  209. goto fail;
  210. }
  211. }
  212. event.sctp_data_io_event = 1;
  213. /* TODO: Subscribe to more event types and handle them */
  214. if (setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
  215. sizeof(event)) != 0) {
  216. av_log(h, AV_LOG_ERROR,
  217. "SCTP ERROR: Unable to subscribe to events\n");
  218. goto fail1;
  219. }
  220. if (s->max_streams) {
  221. initparams.sinit_max_instreams = s->max_streams;
  222. initparams.sinit_num_ostreams = s->max_streams;
  223. if (setsockopt(fd, IPPROTO_SCTP, SCTP_INITMSG, &initparams,
  224. sizeof(initparams)) < 0) {
  225. av_log(h, AV_LOG_ERROR,
  226. "SCTP ERROR: Unable to initialize socket max streams %d\n",
  227. s->max_streams);
  228. ret = ff_neterrno();
  229. goto fail1;
  230. }
  231. }
  232. h->priv_data = s;
  233. h->is_streamed = 1;
  234. s->fd = fd;
  235. freeaddrinfo(ai);
  236. return 0;
  237. fail:
  238. if (cur_ai->ai_next) {
  239. /* Retry with the next sockaddr */
  240. cur_ai = cur_ai->ai_next;
  241. if (fd >= 0)
  242. closesocket(fd);
  243. ret = 0;
  244. goto restart;
  245. }
  246. fail1:
  247. ret = AVERROR(EIO);
  248. freeaddrinfo(ai);
  249. return ret;
  250. }
  251. static int sctp_wait_fd(int fd, int write)
  252. {
  253. int ev = write ? POLLOUT : POLLIN;
  254. struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
  255. int ret;
  256. ret = poll(&p, 1, 100);
  257. return ret < 0 ? ff_neterrno() : p.revents & ev ? 0 : AVERROR(EAGAIN);
  258. }
  259. static int sctp_read(URLContext *h, uint8_t *buf, int size)
  260. {
  261. SCTPContext *s = h->priv_data;
  262. int ret;
  263. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  264. ret = sctp_wait_fd(s->fd, 0);
  265. if (ret < 0)
  266. return ret;
  267. }
  268. if (s->max_streams) {
  269. /*StreamId is introduced as a 2byte code into the stream*/
  270. struct sctp_sndrcvinfo info = { 0 };
  271. ret = ff_sctp_recvmsg(s->fd, buf + 2, size - 2, NULL, 0, &info, 0);
  272. AV_WB16(buf, info.sinfo_stream);
  273. ret = ret < 0 ? ret : ret + 2;
  274. } else
  275. ret = recv(s->fd, buf, size, 0);
  276. return ret < 0 ? ff_neterrno() : ret;
  277. }
  278. static int sctp_write(URLContext *h, const uint8_t *buf, int size)
  279. {
  280. SCTPContext *s = h->priv_data;
  281. int ret;
  282. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  283. ret = sctp_wait_fd(s->fd, 1);
  284. if (ret < 0)
  285. return ret;
  286. }
  287. if (s->max_streams) {
  288. /*StreamId is introduced as a 2byte code into the stream*/
  289. struct sctp_sndrcvinfo info = { 0 };
  290. info.sinfo_stream = AV_RB16(buf);
  291. if (info.sinfo_stream > s->max_streams)
  292. return AVERROR_BUG;
  293. ret = ff_sctp_send(s->fd, buf + 2, size - 2, &info, MSG_EOR);
  294. } else
  295. ret = send(s->fd, buf, size, MSG_NOSIGNAL);
  296. return ret < 0 ? ff_neterrno() : ret;
  297. }
  298. static int sctp_close(URLContext *h)
  299. {
  300. SCTPContext *s = h->priv_data;
  301. closesocket(s->fd);
  302. return 0;
  303. }
  304. static int sctp_get_file_handle(URLContext *h)
  305. {
  306. SCTPContext *s = h->priv_data;
  307. return s->fd;
  308. }
  309. const URLProtocol ff_sctp_protocol = {
  310. .name = "sctp",
  311. .url_open = sctp_open,
  312. .url_read = sctp_read,
  313. .url_write = sctp_write,
  314. .url_close = sctp_close,
  315. .url_get_file_handle = sctp_get_file_handle,
  316. .priv_data_size = sizeof(SCTPContext),
  317. .flags = URL_PROTOCOL_FLAG_NETWORK,
  318. .priv_data_class = &sctp_class,
  319. };