40 #include <netinet/in.h>
41 #include <netinet/sctp.h>
75 socklen_t *fromlen,
struct sctp_sndrcvinfo *sinfo,
80 char incmsg[CMSG_SPACE(
sizeof(
struct sctp_sndrcvinfo))];
81 struct msghdr inmsg = { 0 };
82 struct cmsghdr *cmsg =
NULL;
87 inmsg.msg_name =
from;
88 inmsg.msg_namelen = fromlen ? *fromlen : 0;
91 inmsg.msg_control = incmsg;
92 inmsg.msg_controllen =
sizeof(incmsg);
94 if ((recvb = recvmsg(
s, &inmsg, msg_flags ? *msg_flags : 0)) < 0)
98 *fromlen = inmsg.msg_namelen;
100 *msg_flags = inmsg.msg_flags;
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))
111 memcpy(sinfo, CMSG_DATA(cmsg),
sizeof(
struct sctp_sndrcvinfo));
117 const struct sctp_sndrcvinfo *sinfo,
int flags)
119 struct msghdr outmsg = { 0 };
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;
131 char outcmsg[CMSG_SPACE(
sizeof(
struct sctp_sndrcvinfo))];
132 struct cmsghdr *cmsg;
134 outmsg.msg_control = outcmsg;
135 outmsg.msg_controllen =
sizeof(outcmsg);
136 outmsg.msg_flags = 0;
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));
143 outmsg.msg_controllen = cmsg->cmsg_len;
144 memcpy(CMSG_DATA(cmsg), sinfo,
sizeof(
struct sctp_sndrcvinfo));
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 },
182 struct sctp_event_subscribe event = { 0 };
183 struct sctp_initmsg initparams = { 0 };
190 char hostname[1024], proto[1024], path[1024];
194 &port, path,
sizeof(path), uri);
195 if (strcmp(proto,
"sctp"))
197 if (port <= 0 || port >= 65536) {
202 p = strchr(uri,
'?');
207 s->max_streams = strtol(buf,
NULL, 10);
212 snprintf(portstr,
sizeof(portstr),
"%d", port);
231 s->listen_timeout,
h)) < 0) {
237 s->timeout,
h, !!cur_ai->
ai_next)) < 0) {
246 event.sctp_data_io_event = 1;
249 if (setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
250 sizeof(event)) != 0) {
252 "SCTP ERROR: Unable to subscribe to events\n");
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",
292 int ev = write ? POLLOUT : POLLIN;
293 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
296 ret = poll(&p, 1, 100);
311 if (
s->max_streams) {
313 struct sctp_sndrcvinfo
info = { 0 };
334 if (
s->max_streams) {
336 struct sctp_sndrcvinfo
info = { 0 };
338 if (
info.sinfo_stream >
s->max_streams) {