00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00040 #include <netinet/in.h>
00041 #include <netinet/sctp.h>
00042 
00043 #include "config.h"
00044 
00045 #if HAVE_POLL_H
00046 #include <poll.h>
00047 #endif
00048 
00049 #include "libavutil/intreadwrite.h"
00050 #include "libavutil/parseutils.h"
00051 #include "avformat.h"
00052 #include "internal.h"
00053 #include "network.h"
00054 #include "os_support.h"
00055 #include "url.h"
00056 
00057 
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 static int ff_sctp_recvmsg(int s, void *msg, size_t len, struct sockaddr *from,
00074                            socklen_t *fromlen, struct sctp_sndrcvinfo *sinfo,
00075                            int *msg_flags)
00076 {
00077     int recvb;
00078     struct iovec iov;
00079     char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
00080     struct msghdr inmsg  = { 0 };
00081     struct cmsghdr *cmsg = NULL;
00082 
00083     iov.iov_base = msg;
00084     iov.iov_len  = len;
00085 
00086     inmsg.msg_name       = from;
00087     inmsg.msg_namelen    = fromlen ? *fromlen : 0;
00088     inmsg.msg_iov        = &iov;
00089     inmsg.msg_iovlen     = 1;
00090     inmsg.msg_control    = incmsg;
00091     inmsg.msg_controllen = sizeof(incmsg);
00092 
00093     if ((recvb = recvmsg(s, &inmsg, msg_flags ? *msg_flags : 0)) < 0)
00094         return recvb;
00095 
00096     if (fromlen)
00097         *fromlen   = inmsg.msg_namelen;
00098     if (msg_flags)
00099         *msg_flags = inmsg.msg_flags;
00100 
00101     for (cmsg = CMSG_FIRSTHDR(&inmsg); cmsg != NULL;
00102          cmsg = CMSG_NXTHDR(&inmsg, cmsg)) {
00103         if ((IPPROTO_SCTP == cmsg->cmsg_level) &&
00104             (SCTP_SNDRCV  == cmsg->cmsg_type))
00105             break;
00106     }
00107 
00108     
00109     if (cmsg)
00110         memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_sndrcvinfo));
00111 
00112     return recvb;
00113 }
00114 
00115 static int ff_sctp_send(int s, const void *msg, size_t len,
00116                         const struct sctp_sndrcvinfo *sinfo, int flags)
00117 {
00118     struct msghdr outmsg;
00119     struct iovec iov;
00120 
00121     outmsg.msg_name       = NULL;
00122     outmsg.msg_namelen    = 0;
00123     outmsg.msg_iov        = &iov;
00124     iov.iov_base          = msg;
00125     iov.iov_len           = len;
00126     outmsg.msg_iovlen     = 1;
00127     outmsg.msg_controllen = 0;
00128 
00129     if (sinfo) {
00130         char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
00131         struct cmsghdr *cmsg;
00132 
00133         outmsg.msg_control    = outcmsg;
00134         outmsg.msg_controllen = sizeof(outcmsg);
00135         outmsg.msg_flags      = 0;
00136 
00137         cmsg             = CMSG_FIRSTHDR(&outmsg);
00138         cmsg->cmsg_level = IPPROTO_SCTP;
00139         cmsg->cmsg_type  = SCTP_SNDRCV;
00140         cmsg->cmsg_len   = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
00141 
00142         outmsg.msg_controllen = cmsg->cmsg_len;
00143         memcpy(CMSG_DATA(cmsg), sinfo, sizeof(struct sctp_sndrcvinfo));
00144     }
00145 
00146     return sendmsg(s, &outmsg, flags);
00147 }
00148 
00149 typedef struct SCTPContext {
00150     int fd;
00151     int max_streams;
00152     struct sockaddr_storage dest_addr;
00153     socklen_t dest_addr_len;
00154 } SCTPContext;
00155 
00156 static int sctp_open(URLContext *h, const char *uri, int flags)
00157 {
00158     struct addrinfo *ai, *cur_ai;
00159     struct addrinfo hints             = { 0 };
00160     struct sctp_event_subscribe event = { 0 };
00161     struct sctp_initmsg initparams    = { 0 };
00162     int port;
00163     int fd         = -1;
00164     SCTPContext *s = h->priv_data;
00165     const char *p;
00166     char buf[256];
00167     int ret, listen_socket = 0;
00168     char hostname[1024], proto[1024], path[1024];
00169     char portstr[10];
00170 
00171     av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
00172                  &port, path, sizeof(path), uri);
00173     if (strcmp(proto, "sctp"))
00174         return AVERROR(EINVAL);
00175     if (port <= 0 || port >= 65536) {
00176         av_log(s, AV_LOG_ERROR, "Port missing in uri\n");
00177         return AVERROR(EINVAL);
00178     }
00179 
00180     s->max_streams = 0;
00181     p = strchr(uri, '?');
00182     if (p) {
00183         if (av_find_info_tag(buf, sizeof(buf), "listen", p))
00184             listen_socket = 1;
00185         if (av_find_info_tag(buf, sizeof(buf), "max_streams", p))
00186             s->max_streams = strtol(buf, NULL, 10);
00187     }
00188 
00189     hints.ai_family   = AF_UNSPEC;
00190     hints.ai_socktype = SOCK_STREAM;
00191     snprintf(portstr, sizeof(portstr), "%d", port);
00192     ret = getaddrinfo(hostname, portstr, &hints, &ai);
00193     if (ret) {
00194         av_log(h, AV_LOG_ERROR, "Failed to resolve hostname %s: %s\n",
00195                hostname, gai_strerror(ret));
00196         return AVERROR(EIO);
00197     }
00198 
00199     cur_ai = ai;
00200 
00201     fd = socket(cur_ai->ai_family, SOCK_STREAM, IPPROTO_SCTP);
00202     if (fd < 0)
00203         goto fail;
00204 
00205     s->dest_addr_len = sizeof(s->dest_addr);
00206 
00207     if (listen_socket) {
00208         int fd1;
00209         ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
00210         listen(fd, 100);
00211         fd1 = accept(fd, NULL, NULL);
00212         closesocket(fd);
00213         fd  = fd1;
00214     } else
00215         ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
00216 
00217     ff_socket_nonblock(fd, 1);
00218 
00219     event.sctp_data_io_event = 1;
00220     
00221 
00222     if (setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
00223                    sizeof(event)) != 0) {
00224         av_log(h, AV_LOG_ERROR,
00225                "SCTP ERROR: Unable to subscribe to events\n");
00226         goto fail;
00227     }
00228 
00229     if (s->max_streams) {
00230         initparams.sinit_max_instreams = s->max_streams;
00231         initparams.sinit_num_ostreams  = s->max_streams;
00232         if (setsockopt(fd, IPPROTO_SCTP, SCTP_INITMSG, &initparams,
00233                        sizeof(initparams)) < 0)
00234             av_log(h, AV_LOG_ERROR,
00235                    "SCTP ERROR: Unable to initialize socket max streams %d\n",
00236                    s->max_streams);
00237     }
00238 
00239     h->priv_data   = s;
00240     h->is_streamed = 1;
00241     s->fd          = fd;
00242     freeaddrinfo(ai);
00243     return 0;
00244 
00245 fail:
00246     ret = AVERROR(EIO);
00247     freeaddrinfo(ai);
00248     return ret;
00249 }
00250 
00251 static int sctp_wait_fd(int fd, int write)
00252 {
00253     int ev          = write ? POLLOUT : POLLIN;
00254     struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
00255     int ret;
00256 
00257     ret = poll(&p, 1, 100);
00258     return ret < 0 ? ff_neterrno() : p.revents & ev ? 0 : AVERROR(EAGAIN);
00259 }
00260 
00261 static int sctp_read(URLContext *h, uint8_t *buf, int size)
00262 {
00263     SCTPContext *s = h->priv_data;
00264     int ret;
00265 
00266     if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
00267         ret = sctp_wait_fd(s->fd, 0);
00268         if (ret < 0)
00269             return ret;
00270     }
00271 
00272     if (s->max_streams) {
00273         
00274         struct sctp_sndrcvinfo info = { 0 };
00275         ret = ff_sctp_recvmsg(s->fd, buf + 2, size - 2, NULL, 0, &info, 0);
00276         AV_WB16(buf, info.sinfo_stream);
00277         ret = ret < 0 ? ret : ret + 2;
00278     } else
00279         ret = recv(s->fd, buf, size, 0);
00280 
00281     return ret < 0 ? ff_neterrno() : ret;
00282 }
00283 
00284 static int sctp_write(URLContext *h, const uint8_t *buf, int size)
00285 {
00286     SCTPContext *s = h->priv_data;
00287     int ret;
00288 
00289     if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
00290         ret = sctp_wait_fd(s->fd, 1);
00291         if (ret < 0)
00292             return ret;
00293     }
00294 
00295     if (s->max_streams) {
00296         
00297         struct sctp_sndrcvinfo info = { 0 };
00298         info.sinfo_stream           = AV_RB16(buf);
00299         if (info.sinfo_stream > s->max_streams)
00300             abort();
00301         ret = ff_sctp_send(s->fd, buf + 2, size - 2, &info, MSG_EOR);
00302     } else
00303         ret = send(s->fd, buf, size, 0);
00304 
00305     return ret < 0 ? ff_neterrno() : ret;
00306 }
00307 
00308 static int sctp_close(URLContext *h)
00309 {
00310     SCTPContext *s = h->priv_data;
00311     closesocket(s->fd);
00312     return 0;
00313 }
00314 
00315 static int sctp_get_file_handle(URLContext *h)
00316 {
00317     SCTPContext *s = h->priv_data;
00318     return s->fd;
00319 }
00320 
00321 URLProtocol ff_sctp_protocol = {
00322     .name                = "sctp",
00323     .url_open            = sctp_open,
00324     .url_read            = sctp_read,
00325     .url_write           = sctp_write,
00326     .url_close           = sctp_close,
00327     .url_get_file_handle = sctp_get_file_handle,
00328     .priv_data_size      = sizeof(SCTPContext),
00329     .flags               = URL_PROTOCOL_FLAG_NETWORK,
00330 };