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.

328 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 <unistd.h>
  40. #include "config.h"
  41. #if HAVE_POLL_H
  42. #include <poll.h>
  43. #endif
  44. #include "libavutil/intreadwrite.h"
  45. #include "libavutil/parseutils.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
  54. * for the SCTP kernel Implementation. The main purpose of this
  55. * code is to provide the SCTP Socket API mappings for user
  56. * application to interface with the SCTP in 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;
  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);
  126. }
  127. typedef struct SCTPContext {
  128. int fd;
  129. int max_streams;
  130. struct sockaddr_storage dest_addr;
  131. socklen_t dest_addr_len;
  132. } SCTPContext;
  133. static int sctp_open(URLContext *h, const char *uri, int flags)
  134. {
  135. struct addrinfo *ai, *cur_ai;
  136. struct addrinfo hints = { 0 };
  137. struct sctp_event_subscribe event = { 0 };
  138. struct sctp_initmsg initparams = { 0 };
  139. int port;
  140. int fd = -1;
  141. SCTPContext *s = h->priv_data;
  142. const char *p;
  143. char buf[256];
  144. int ret, listen_socket = 0;
  145. char hostname[1024], proto[1024], path[1024];
  146. char portstr[10];
  147. av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
  148. &port, path, sizeof(path), uri);
  149. if (strcmp(proto,"sctp") || port <= 0 || port >= 65536)
  150. return AVERROR(EINVAL);
  151. s->max_streams = 0;
  152. p = strchr(uri, '?');
  153. if (p) {
  154. if (av_find_info_tag(buf, sizeof(buf), "listen", p))
  155. listen_socket = 1;
  156. if (av_find_info_tag(buf, sizeof(buf), "max_streams", p))
  157. s->max_streams = strtol(buf, NULL, 10);
  158. }
  159. hints.ai_family = AF_UNSPEC;
  160. hints.ai_socktype = SOCK_STREAM;
  161. snprintf(portstr, sizeof(portstr), "%d", port);
  162. ret = getaddrinfo(hostname, portstr, &hints, &ai);
  163. if (ret) {
  164. av_log(h, AV_LOG_ERROR, "Failed to resolve hostname %s: %s\n",
  165. hostname, gai_strerror(ret));
  166. return AVERROR(EIO);
  167. }
  168. cur_ai = ai;
  169. fd = socket(cur_ai->ai_family, SOCK_STREAM, IPPROTO_SCTP);
  170. if (fd < 0)
  171. goto fail;
  172. s->dest_addr_len = sizeof(s->dest_addr);
  173. if (listen_socket) {
  174. int fd1;
  175. ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  176. listen(fd, 100);
  177. fd1 = accept(fd, NULL, NULL);
  178. closesocket(fd);
  179. fd = fd1;
  180. } else
  181. ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  182. ff_socket_nonblock(fd, 1);
  183. event.sctp_data_io_event = 1;
  184. /* TODO: Subscribe to more event types and handle them */
  185. if (setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
  186. sizeof(event)) != 0) {
  187. av_log(h, AV_LOG_ERROR,
  188. "SCTP ERROR: Unable to subscribe to events\n");
  189. goto fail;
  190. }
  191. if (s->max_streams) {
  192. initparams.sinit_max_instreams = s->max_streams;
  193. initparams.sinit_num_ostreams = s->max_streams;
  194. if (setsockopt(fd, IPPROTO_SCTP, SCTP_INITMSG, &initparams,
  195. sizeof(initparams)) < 0)
  196. av_log(h, AV_LOG_ERROR,
  197. "SCTP ERROR: Unable to initialize socket max streams %d\n",
  198. s->max_streams);
  199. }
  200. h->priv_data = s;
  201. h->is_streamed = 1;
  202. s->fd = fd;
  203. freeaddrinfo(ai);
  204. return 0;
  205. fail:
  206. ret = AVERROR(EIO);
  207. freeaddrinfo(ai);
  208. return ret;
  209. }
  210. static int sctp_wait_fd(int fd, int write)
  211. {
  212. int ev = write ? POLLOUT : POLLIN;
  213. struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
  214. int ret;
  215. ret = poll(&p, 1, 100);
  216. return ret < 0 ? ff_neterrno() : p.revents & ev ? 0 : AVERROR(EAGAIN);
  217. }
  218. static int sctp_read(URLContext *h, uint8_t *buf, int size)
  219. {
  220. SCTPContext *s = h->priv_data;
  221. int ret;
  222. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  223. ret = sctp_wait_fd(s->fd, 0);
  224. if (ret < 0)
  225. return ret;
  226. }
  227. if (s->max_streams) {
  228. /*StreamId is introduced as a 2byte code into the stream*/
  229. struct sctp_sndrcvinfo info = { 0 };
  230. ret = ff_sctp_recvmsg(s->fd, buf + 2, size - 2, NULL, 0, &info, 0);
  231. AV_WB16(buf, info.sinfo_stream);
  232. ret = ret < 0 ? ret : ret + 2;
  233. } else
  234. ret = recv(s->fd, buf, size, 0);
  235. return ret < 0 ? ff_neterrno() : ret;
  236. }
  237. static int sctp_write(URLContext *h, const uint8_t *buf, int size)
  238. {
  239. SCTPContext *s = h->priv_data;
  240. int ret;
  241. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  242. ret = sctp_wait_fd(s->fd, 1);
  243. if (ret < 0)
  244. return ret;
  245. }
  246. if (s->max_streams) {
  247. /*StreamId is introduced as a 2byte code into the stream*/
  248. struct sctp_sndrcvinfo info = { 0 };
  249. info.sinfo_stream = AV_RB16(buf);
  250. if (info.sinfo_stream > s->max_streams)
  251. abort();
  252. ret = ff_sctp_send(s->fd, buf + 2, size - 2, &info, MSG_EOR);
  253. } else
  254. ret = send(s->fd, buf, size, 0);
  255. return ret < 0 ? ff_neterrno() : ret;
  256. }
  257. static int sctp_close(URLContext *h)
  258. {
  259. SCTPContext *s = h->priv_data;
  260. closesocket(s->fd);
  261. return 0;
  262. }
  263. static int sctp_get_file_handle(URLContext *h)
  264. {
  265. SCTPContext *s = h->priv_data;
  266. return s->fd;
  267. }
  268. URLProtocol ff_sctp_protocol = {
  269. .name = "sctp",
  270. .url_open = sctp_open,
  271. .url_read = sctp_read,
  272. .url_write = sctp_write,
  273. .url_close = sctp_close,
  274. .url_get_file_handle = sctp_get_file_handle,
  275. .priv_data_size = sizeof(SCTPContext),
  276. .flags = URL_PROTOCOL_FLAG_NETWORK,
  277. };