sctp.c 9.2 KB

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