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 /**
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
43 #include "config.h"
44
45 #if HAVE_POLL_H
46 #include <poll.h>
47 #endif
48
57
58 /*
59 * The sctp_recvmsg and sctp_sendmsg functions are part of the user
60 * library that offers support for the SCTP kernel Implementation.
61 * To avoid build-time clashes the functions sport an ff_-prefix here.
62 * The main purpose of this code is to provide the SCTP Socket API
63 * mappings for user applications to interface with SCTP in the kernel.
64 *
65 * This implementation is based on the Socket API Extensions for SCTP
66 * defined in <draft-ietf-tsvwg-sctpsocket-10.txt>
67 *
68 * Copyright (c) 2003 International Business Machines, Corp.
69 *
70 * Written or modified by:
71 * Ryan Layer <rmlayer@us.ibm.com>
72 */
73
75 socklen_t *fromlen, struct sctp_sndrcvinfo *sinfo,
76 int *msg_flags)
77 {
78 int recvb;
79 struct iovec iov;
80 char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
81 struct msghdr inmsg = { 0 };
82 struct cmsghdr *cmsg =
NULL;
83
84 iov.iov_base = msg;
86
87 inmsg.msg_name =
from;
88 inmsg.msg_namelen = fromlen ? *fromlen : 0;
89 inmsg.msg_iov = &iov;
90 inmsg.msg_iovlen = 1;
91 inmsg.msg_control = incmsg;
92 inmsg.msg_controllen = sizeof(incmsg);
93
94 if ((recvb = recvmsg(
s, &inmsg, msg_flags ? *msg_flags : 0)) < 0)
95 return recvb;
96
97 if (fromlen)
98 *fromlen = inmsg.msg_namelen;
99 if (msg_flags)
100 *msg_flags = inmsg.msg_flags;
101
102 for (cmsg = CMSG_FIRSTHDR(&inmsg); cmsg;
103 cmsg = CMSG_NXTHDR(&inmsg, cmsg)) {
104 if ((IPPROTO_SCTP == cmsg->cmsg_level) &&
105 (SCTP_SNDRCV == cmsg->cmsg_type))
106 break;
107 }
108
109 /* Copy sinfo. */
110 if (cmsg)
111 memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_sndrcvinfo));
112
113 return recvb;
114 }
115
117 const struct sctp_sndrcvinfo *sinfo,
int flags)
118 {
119 struct msghdr outmsg = { 0 };
120 struct iovec iov;
121
122 outmsg.msg_name =
NULL;
123 outmsg.msg_namelen = 0;
124 outmsg.msg_iov = &iov;
125 iov.iov_base = (void*)msg;
127 outmsg.msg_iovlen = 1;
128 outmsg.msg_controllen = 0;
129
130 if (sinfo) {
131 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
132 struct cmsghdr *cmsg;
133
134 outmsg.msg_control = outcmsg;
135 outmsg.msg_controllen = sizeof(outcmsg);
136 outmsg.msg_flags = 0;
137
138 cmsg = CMSG_FIRSTHDR(&outmsg);
139 cmsg->cmsg_level = IPPROTO_SCTP;
140 cmsg->cmsg_type = SCTP_SNDRCV;
141 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
142
143 outmsg.msg_controllen = cmsg->cmsg_len;
144 memcpy(CMSG_DATA(cmsg), sinfo, sizeof(struct sctp_sndrcvinfo));
145 }
146
148 }
149
159
160 #define OFFSET(x) offsetof(SCTPContext, x)
161 #define D AV_OPT_FLAG_DECODING_PARAM
162 #define E AV_OPT_FLAG_ENCODING_PARAM
165 {
"timeout",
"Connection timeout (in milliseconds)",
OFFSET(timeout),
AV_OPT_TYPE_INT, { .i64 = 10000 }, INT_MIN, INT_MAX, .flags =
D|
E },
166 {
"listen_timeout",
"Bind timeout (in milliseconds)",
OFFSET(listen_timeout),
AV_OPT_TYPE_INT, { .i64 = -1 }, INT_MIN, INT_MAX, .flags =
D|
E },
167 {
"max_streams",
"Max stream to allocate",
OFFSET(max_streams),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT16_MAX, .flags =
D|
E },
169 };
170
176 };
177
179 {
182 struct sctp_event_subscribe event = { 0 };
183 struct sctp_initmsg initparams = { 0 };
184 int port;
185 int fd = -1;
187 const char *p;
188 char buf[256];
190 char hostname[1024], proto[1024], path[1024];
191 char portstr[10];
192
194 &port, path, sizeof(path), uri);
195 if (strcmp(proto, "sctp"))
197 if (port <= 0 || port >= 65536) {
200 }
201
202 p = strchr(uri, '?');
203 if (p) {
207 s->max_streams = strtol(buf,
NULL, 10);
208 }
209
212 snprintf(portstr,
sizeof(portstr),
"%d", port);
218 }
219
220 cur_ai = ai;
221
222 restart:
224 if (fd < 0) {
227 }
228
231 s->listen_timeout,
h)) < 0) {
233 goto fail1;
234 }
235 } else {
237 s->timeout,
h, !!cur_ai->
ai_next)) < 0) {
238
240 goto fail1;
241 else
243 }
244 }
245
246 event.sctp_data_io_event = 1;
247 /* TODO: Subscribe to more event types and handle them */
248
249 if (setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
250 sizeof(event)) != 0) {
252 "SCTP ERROR: Unable to subscribe to events\n");
253 goto fail1;
254 }
255
256 if (
s->max_streams) {
257 initparams.sinit_max_instreams =
s->max_streams;
258 initparams.sinit_num_ostreams =
s->max_streams;
259 if (setsockopt(fd, IPPROTO_SCTP, SCTP_INITMSG, &initparams,
260 sizeof(initparams)) < 0) {
262 "SCTP ERROR: Unable to initialize socket max streams %d\n",
265 goto fail1;
266 }
267 }
268
273 return 0;
274
277 /* Retry with the next sockaddr */
279 if (fd >= 0)
280 closesocket(fd);
282 goto restart;
283 }
284 fail1:
285 if (fd >= 0)
286 closesocket(fd);
290 }
291
293 {
294 int ev = write ? POLLOUT : POLLIN;
295 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
297
298 ret = poll(&p, 1, 100);
300 }
301
303 {
306
311 }
312
313 if (
s->max_streams) {
314 /*StreamId is introduced as a 2byte code into the stream*/
315 struct sctp_sndrcvinfo
info = { 0 };
319 } else
321
323 }
324
326 {
329
334 }
335
336 if (
s->max_streams) {
337 /*StreamId is introduced as a 2byte code into the stream*/
338 struct sctp_sndrcvinfo
info = { 0 };
340 if (
info.sinfo_stream >
s->max_streams) {
343 }
345 } else
347
349 }
350
352 {
355 return 0;
356 }
357
359 {
362 }
363
374 };