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.

327 lines
9.2KB

  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 "avformat.h"
  46. #include "internal.h"
  47. #include "network.h"
  48. #include "os_support.h"
  49. #include "url.h"
  50. /*
  51. * The sctp_recvmsg and sctp_sendmsg functions are part of the user
  52. * library that offers support
  53. * for the SCTP kernel Implementation. The main purpose of this
  54. * code is to provide the SCTP Socket API mappings for user
  55. * application to interface with the SCTP in kernel.
  56. *
  57. * This implementation is based on the Socket API Extensions for SCTP
  58. * defined in <draft-ietf-tsvwg-sctpsocket-10.txt>
  59. *
  60. * Copyright (c) 2003 International Business Machines, Corp.
  61. *
  62. * Written or modified by:
  63. * Ryan Layer <rmlayer@us.ibm.com>
  64. */
  65. static int ff_sctp_recvmsg(int s, void *msg, size_t len, struct sockaddr *from,
  66. socklen_t *fromlen, struct sctp_sndrcvinfo *sinfo,
  67. int *msg_flags)
  68. {
  69. int recvb;
  70. struct iovec iov;
  71. char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
  72. struct msghdr inmsg = { 0 };
  73. struct cmsghdr *cmsg = NULL;
  74. iov.iov_base = msg;
  75. iov.iov_len = len;
  76. inmsg.msg_name = from;
  77. inmsg.msg_namelen = fromlen ? *fromlen : 0;
  78. inmsg.msg_iov = &iov;
  79. inmsg.msg_iovlen = 1;
  80. inmsg.msg_control = incmsg;
  81. inmsg.msg_controllen = sizeof(incmsg);
  82. if ((recvb = recvmsg(s, &inmsg, msg_flags ? *msg_flags : 0)) < 0)
  83. return recvb;
  84. if (fromlen)
  85. *fromlen = inmsg.msg_namelen;
  86. if (msg_flags)
  87. *msg_flags = inmsg.msg_flags;
  88. for (cmsg = CMSG_FIRSTHDR(&inmsg); cmsg != NULL;
  89. cmsg = CMSG_NXTHDR(&inmsg, cmsg)) {
  90. if ((IPPROTO_SCTP == cmsg->cmsg_level) &&
  91. (SCTP_SNDRCV == cmsg->cmsg_type))
  92. break;
  93. }
  94. /* Copy sinfo. */
  95. if (cmsg)
  96. memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_sndrcvinfo));
  97. return recvb;
  98. }
  99. static int ff_sctp_send(int s, const void *msg, size_t len,
  100. const struct sctp_sndrcvinfo *sinfo, int flags)
  101. {
  102. struct msghdr outmsg;
  103. struct iovec iov;
  104. outmsg.msg_name = NULL;
  105. outmsg.msg_namelen = 0;
  106. outmsg.msg_iov = &iov;
  107. iov.iov_base = msg;
  108. iov.iov_len = len;
  109. outmsg.msg_iovlen = 1;
  110. outmsg.msg_controllen = 0;
  111. if (sinfo) {
  112. char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
  113. struct cmsghdr *cmsg;
  114. outmsg.msg_control = outcmsg;
  115. outmsg.msg_controllen = sizeof(outcmsg);
  116. outmsg.msg_flags = 0;
  117. cmsg = CMSG_FIRSTHDR(&outmsg);
  118. cmsg->cmsg_level = IPPROTO_SCTP;
  119. cmsg->cmsg_type = SCTP_SNDRCV;
  120. cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
  121. outmsg.msg_controllen = cmsg->cmsg_len;
  122. memcpy(CMSG_DATA(cmsg), sinfo, sizeof(struct sctp_sndrcvinfo));
  123. }
  124. return sendmsg(s, &outmsg, flags);
  125. }
  126. typedef struct SCTPContext {
  127. int fd;
  128. int max_streams;
  129. struct sockaddr_storage dest_addr;
  130. socklen_t dest_addr_len;
  131. } SCTPContext;
  132. static int sctp_open(URLContext *h, const char *uri, int flags)
  133. {
  134. struct addrinfo *ai, *cur_ai;
  135. struct addrinfo hints = { 0 };
  136. struct sctp_event_subscribe event = { 0 };
  137. struct sctp_initmsg initparams = { 0 };
  138. int port;
  139. int fd = -1;
  140. SCTPContext *s = h->priv_data;
  141. const char *p;
  142. char buf[256];
  143. int ret, listen_socket = 0;
  144. char hostname[1024], proto[1024], path[1024];
  145. char portstr[10];
  146. av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
  147. &port, path, sizeof(path), uri);
  148. if (strcmp(proto,"sctp") || port <= 0 || port >= 65536)
  149. return AVERROR(EINVAL);
  150. s->max_streams = 0;
  151. p = strchr(uri, '?');
  152. if (p) {
  153. if (av_find_info_tag(buf, sizeof(buf), "listen", p))
  154. listen_socket = 1;
  155. if (av_find_info_tag(buf, sizeof(buf), "max_streams", p))
  156. s->max_streams = strtol(buf, NULL, 10);
  157. }
  158. hints.ai_family = AF_UNSPEC;
  159. hints.ai_socktype = SOCK_STREAM;
  160. snprintf(portstr, sizeof(portstr), "%d", port);
  161. ret = getaddrinfo(hostname, portstr, &hints, &ai);
  162. if (ret) {
  163. av_log(h, AV_LOG_ERROR, "Failed to resolve hostname %s: %s\n",
  164. hostname, gai_strerror(ret));
  165. return AVERROR(EIO);
  166. }
  167. cur_ai = ai;
  168. fd = socket(cur_ai->ai_family, SOCK_STREAM, IPPROTO_SCTP);
  169. if (fd < 0)
  170. goto fail;
  171. s->dest_addr_len = sizeof(s->dest_addr);
  172. if (listen_socket) {
  173. int fd1;
  174. ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  175. listen(fd, 100);
  176. fd1 = accept(fd, NULL, NULL);
  177. closesocket(fd);
  178. fd = fd1;
  179. } else
  180. ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  181. ff_socket_nonblock(fd, 1);
  182. event.sctp_data_io_event = 1;
  183. /* TODO: Subscribe to more event types and handle them */
  184. if (setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
  185. sizeof(event)) != 0) {
  186. av_log(h, AV_LOG_ERROR,
  187. "SCTP ERROR: Unable to subscribe to events\n");
  188. goto fail;
  189. }
  190. if (s->max_streams) {
  191. initparams.sinit_max_instreams = s->max_streams;
  192. initparams.sinit_num_ostreams = s->max_streams;
  193. if (setsockopt(fd, IPPROTO_SCTP, SCTP_INITMSG, &initparams,
  194. sizeof(initparams)) < 0)
  195. av_log(h, AV_LOG_ERROR,
  196. "SCTP ERROR: Unable to initialize socket max streams %d\n",
  197. s->max_streams);
  198. }
  199. h->priv_data = s;
  200. h->is_streamed = 1;
  201. s->fd = fd;
  202. freeaddrinfo(ai);
  203. return 0;
  204. fail:
  205. ret = AVERROR(EIO);
  206. freeaddrinfo(ai);
  207. return ret;
  208. }
  209. static int sctp_wait_fd(int fd, int write)
  210. {
  211. int ev = write ? POLLOUT : POLLIN;
  212. struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
  213. int ret;
  214. ret = poll(&p, 1, 100);
  215. return ret < 0 ? ff_neterrno() : p.revents & ev ? 0 : AVERROR(EAGAIN);
  216. }
  217. static int sctp_read(URLContext *h, uint8_t *buf, int size)
  218. {
  219. SCTPContext *s = h->priv_data;
  220. int ret;
  221. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  222. ret = sctp_wait_fd(s->fd, 0);
  223. if (ret < 0)
  224. return ret;
  225. }
  226. if (s->max_streams) {
  227. /*StreamId is introduced as a 2byte code into the stream*/
  228. struct sctp_sndrcvinfo info = { 0 };
  229. ret = ff_sctp_recvmsg(s->fd, buf + 2, size - 2, NULL, 0, &info, 0);
  230. AV_WB16(buf, info.sinfo_stream);
  231. ret = ret < 0 ? ret : ret + 2;
  232. } else
  233. ret = recv(s->fd, buf, size, 0);
  234. return ret < 0 ? ff_neterrno() : ret;
  235. }
  236. static int sctp_write(URLContext *h, const uint8_t *buf, int size)
  237. {
  238. SCTPContext *s = h->priv_data;
  239. int ret;
  240. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  241. ret = sctp_wait_fd(s->fd, 1);
  242. if (ret < 0)
  243. return ret;
  244. }
  245. if (s->max_streams) {
  246. /*StreamId is introduced as a 2byte code into the stream*/
  247. struct sctp_sndrcvinfo info = { 0 };
  248. info.sinfo_stream = AV_RB16(buf);
  249. if (info.sinfo_stream > s->max_streams)
  250. abort();
  251. ret = ff_sctp_send(s->fd, buf + 2, size - 2, &info, MSG_EOR);
  252. } else
  253. ret = send(s->fd, buf, size, 0);
  254. return ret < 0 ? ff_neterrno() : ret;
  255. }
  256. static int sctp_close(URLContext *h)
  257. {
  258. SCTPContext *s = h->priv_data;
  259. closesocket(s->fd);
  260. return 0;
  261. }
  262. static int sctp_get_file_handle(URLContext *h)
  263. {
  264. SCTPContext *s = h->priv_data;
  265. return s->fd;
  266. }
  267. URLProtocol ff_sctp_protocol = {
  268. .name = "sctp",
  269. .url_open = sctp_open,
  270. .url_read = sctp_read,
  271. .url_write = sctp_write,
  272. .url_close = sctp_close,
  273. .url_get_file_handle = sctp_get_file_handle,
  274. .priv_data_size = sizeof(SCTPContext),
  275. .flags = URL_PROTOCOL_FLAG_NETWORK,
  276. };