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