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.

333 lines
9.5KB

  1. /*
  2. * SCTP protocol
  3. * Copyright (c) 2012 Luca Barbato
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 for the SCTP kernel Implementation.
  53. * To avoid build-time clashes the functions sport an ff_-prefix here.
  54. * The main purpose of this code is to provide the SCTP Socket API
  55. * mappings for user applications to interface with SCTP in the 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 = (void*)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"))
  149. return AVERROR(EINVAL);
  150. if (port <= 0 || port >= 65536) {
  151. av_log(s, AV_LOG_ERROR, "Port missing in uri\n");
  152. return AVERROR(EINVAL);
  153. }
  154. s->max_streams = 0;
  155. p = strchr(uri, '?');
  156. if (p) {
  157. if (av_find_info_tag(buf, sizeof(buf), "listen", p))
  158. listen_socket = 1;
  159. if (av_find_info_tag(buf, sizeof(buf), "max_streams", p))
  160. s->max_streams = strtol(buf, NULL, 10);
  161. }
  162. hints.ai_family = AF_UNSPEC;
  163. hints.ai_socktype = SOCK_STREAM;
  164. snprintf(portstr, sizeof(portstr), "%d", port);
  165. ret = getaddrinfo(hostname, portstr, &hints, &ai);
  166. if (ret) {
  167. av_log(h, AV_LOG_ERROR, "Failed to resolve hostname %s: %s\n",
  168. hostname, gai_strerror(ret));
  169. return AVERROR(EIO);
  170. }
  171. cur_ai = ai;
  172. fd = ff_socket(cur_ai->ai_family, SOCK_STREAM, IPPROTO_SCTP);
  173. if (fd < 0)
  174. goto fail;
  175. s->dest_addr_len = sizeof(s->dest_addr);
  176. if (listen_socket) {
  177. int fd1;
  178. ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  179. listen(fd, 100);
  180. fd1 = accept(fd, NULL, NULL);
  181. closesocket(fd);
  182. fd = fd1;
  183. } else
  184. ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  185. ff_socket_nonblock(fd, 1);
  186. event.sctp_data_io_event = 1;
  187. /* TODO: Subscribe to more event types and handle them */
  188. if (setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
  189. sizeof(event)) != 0) {
  190. av_log(h, AV_LOG_ERROR,
  191. "SCTP ERROR: Unable to subscribe to events\n");
  192. goto fail;
  193. }
  194. if (s->max_streams) {
  195. initparams.sinit_max_instreams = s->max_streams;
  196. initparams.sinit_num_ostreams = s->max_streams;
  197. if (setsockopt(fd, IPPROTO_SCTP, SCTP_INITMSG, &initparams,
  198. sizeof(initparams)) < 0)
  199. av_log(h, AV_LOG_ERROR,
  200. "SCTP ERROR: Unable to initialize socket max streams %d\n",
  201. s->max_streams);
  202. }
  203. h->priv_data = s;
  204. h->is_streamed = 1;
  205. s->fd = fd;
  206. freeaddrinfo(ai);
  207. return 0;
  208. fail:
  209. ret = AVERROR(EIO);
  210. freeaddrinfo(ai);
  211. return ret;
  212. }
  213. static int sctp_wait_fd(int fd, int write)
  214. {
  215. int ev = write ? POLLOUT : POLLIN;
  216. struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
  217. int ret;
  218. ret = poll(&p, 1, 100);
  219. return ret < 0 ? ff_neterrno() : p.revents & ev ? 0 : AVERROR(EAGAIN);
  220. }
  221. static int sctp_read(URLContext *h, uint8_t *buf, int size)
  222. {
  223. SCTPContext *s = h->priv_data;
  224. int ret;
  225. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  226. ret = sctp_wait_fd(s->fd, 0);
  227. if (ret < 0)
  228. return ret;
  229. }
  230. if (s->max_streams) {
  231. /*StreamId is introduced as a 2byte code into the stream*/
  232. struct sctp_sndrcvinfo info = { 0 };
  233. ret = ff_sctp_recvmsg(s->fd, buf + 2, size - 2, NULL, 0, &info, 0);
  234. AV_WB16(buf, info.sinfo_stream);
  235. ret = ret < 0 ? ret : ret + 2;
  236. } else
  237. ret = recv(s->fd, buf, size, 0);
  238. return ret < 0 ? ff_neterrno() : ret;
  239. }
  240. static int sctp_write(URLContext *h, const uint8_t *buf, int size)
  241. {
  242. SCTPContext *s = h->priv_data;
  243. int ret;
  244. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  245. ret = sctp_wait_fd(s->fd, 1);
  246. if (ret < 0)
  247. return ret;
  248. }
  249. if (s->max_streams) {
  250. /*StreamId is introduced as a 2byte code into the stream*/
  251. struct sctp_sndrcvinfo info = { 0 };
  252. info.sinfo_stream = AV_RB16(buf);
  253. if (info.sinfo_stream > s->max_streams) {
  254. av_log(h, AV_LOG_ERROR, "bad input data\n");
  255. return AVERROR(EINVAL);
  256. }
  257. ret = ff_sctp_send(s->fd, buf + 2, size - 2, &info, MSG_EOR);
  258. } else
  259. ret = send(s->fd, buf, size, 0);
  260. return ret < 0 ? ff_neterrno() : ret;
  261. }
  262. static int sctp_close(URLContext *h)
  263. {
  264. SCTPContext *s = h->priv_data;
  265. closesocket(s->fd);
  266. return 0;
  267. }
  268. static int sctp_get_file_handle(URLContext *h)
  269. {
  270. SCTPContext *s = h->priv_data;
  271. return s->fd;
  272. }
  273. URLProtocol ff_sctp_protocol = {
  274. .name = "sctp",
  275. .url_open = sctp_open,
  276. .url_read = sctp_read,
  277. .url_write = sctp_write,
  278. .url_close = sctp_close,
  279. .url_get_file_handle = sctp_get_file_handle,
  280. .priv_data_size = sizeof(SCTPContext),
  281. .flags = URL_PROTOCOL_FLAG_NETWORK,
  282. };