00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avformat.h"
00022 #include "libavutil/parseutils.h"
00023 #include "internal.h"
00024 #include "network.h"
00025 #include "os_support.h"
00026 #include "url.h"
00027 #if HAVE_POLL_H
00028 #include <poll.h>
00029 #endif
00030
00031 typedef struct TCPContext {
00032 int fd;
00033 } TCPContext;
00034
00035
00036 static int tcp_open(URLContext *h, const char *uri, int flags)
00037 {
00038 struct addrinfo hints = { 0 }, *ai, *cur_ai;
00039 int port, fd = -1;
00040 TCPContext *s = h->priv_data;
00041 int listen_socket = 0;
00042 const char *p;
00043 char buf[256];
00044 int ret;
00045 socklen_t optlen;
00046 int timeout = 50, listen_timeout = -1;
00047 char hostname[1024],proto[1024],path[1024];
00048 char portstr[10];
00049
00050 av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
00051 &port, path, sizeof(path), uri);
00052 if (strcmp(proto, "tcp"))
00053 return AVERROR(EINVAL);
00054 if (port <= 0 || port >= 65536) {
00055 av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
00056 return AVERROR(EINVAL);
00057 }
00058 p = strchr(uri, '?');
00059 if (p) {
00060 if (av_find_info_tag(buf, sizeof(buf), "listen", p))
00061 listen_socket = 1;
00062 if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
00063 timeout = strtol(buf, NULL, 10);
00064 }
00065 if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
00066 listen_timeout = strtol(buf, NULL, 10);
00067 }
00068 }
00069 hints.ai_family = AF_UNSPEC;
00070 hints.ai_socktype = SOCK_STREAM;
00071 snprintf(portstr, sizeof(portstr), "%d", port);
00072 if (listen_socket)
00073 hints.ai_flags |= AI_PASSIVE;
00074 if (!hostname[0])
00075 ret = getaddrinfo(NULL, portstr, &hints, &ai);
00076 else
00077 ret = getaddrinfo(hostname, portstr, &hints, &ai);
00078 if (ret) {
00079 av_log(h, AV_LOG_ERROR,
00080 "Failed to resolve hostname %s: %s\n",
00081 hostname, gai_strerror(ret));
00082 return AVERROR(EIO);
00083 }
00084
00085 cur_ai = ai;
00086
00087 restart:
00088 ret = AVERROR(EIO);
00089 fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
00090 if (fd < 0)
00091 goto fail;
00092
00093 if (listen_socket) {
00094 int fd1;
00095 int reuse = 1;
00096 struct pollfd lp = { fd, POLLIN, 0 };
00097 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
00098 ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
00099 if (ret) {
00100 ret = ff_neterrno();
00101 goto fail1;
00102 }
00103 ret = listen(fd, 1);
00104 if (ret) {
00105 ret = ff_neterrno();
00106 goto fail1;
00107 }
00108 ret = poll(&lp, 1, listen_timeout >= 0 ? listen_timeout : -1);
00109 if (ret <= 0) {
00110 ret = AVERROR(ETIMEDOUT);
00111 goto fail1;
00112 }
00113 fd1 = accept(fd, NULL, NULL);
00114 if (fd1 < 0) {
00115 ret = ff_neterrno();
00116 goto fail1;
00117 }
00118 closesocket(fd);
00119 fd = fd1;
00120 ff_socket_nonblock(fd, 1);
00121 } else {
00122 redo:
00123 ff_socket_nonblock(fd, 1);
00124 ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
00125 }
00126
00127 if (ret < 0) {
00128 struct pollfd p = {fd, POLLOUT, 0};
00129 ret = ff_neterrno();
00130 if (ret == AVERROR(EINTR)) {
00131 if (ff_check_interrupt(&h->interrupt_callback)) {
00132 ret = AVERROR_EXIT;
00133 goto fail1;
00134 }
00135 goto redo;
00136 }
00137 if (ret != AVERROR(EINPROGRESS) &&
00138 ret != AVERROR(EAGAIN))
00139 goto fail;
00140
00141
00142 while(timeout--) {
00143 if (ff_check_interrupt(&h->interrupt_callback)) {
00144 ret = AVERROR_EXIT;
00145 goto fail1;
00146 }
00147 ret = poll(&p, 1, 100);
00148 if (ret > 0)
00149 break;
00150 }
00151 if (ret <= 0) {
00152 ret = AVERROR(ETIMEDOUT);
00153 goto fail;
00154 }
00155
00156 optlen = sizeof(ret);
00157 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
00158 ret = AVUNERROR(ff_neterrno());
00159 if (ret != 0) {
00160 char errbuf[100];
00161 ret = AVERROR(ret);
00162 av_strerror(ret, errbuf, sizeof(errbuf));
00163 av_log(h, AV_LOG_ERROR,
00164 "TCP connection to %s:%d failed: %s\n",
00165 hostname, port, errbuf);
00166 goto fail;
00167 }
00168 }
00169 h->is_streamed = 1;
00170 s->fd = fd;
00171 freeaddrinfo(ai);
00172 return 0;
00173
00174 fail:
00175 if (cur_ai->ai_next) {
00176
00177 cur_ai = cur_ai->ai_next;
00178 if (fd >= 0)
00179 closesocket(fd);
00180 goto restart;
00181 }
00182 fail1:
00183 if (fd >= 0)
00184 closesocket(fd);
00185 freeaddrinfo(ai);
00186 return ret;
00187 }
00188
00189 static int tcp_read(URLContext *h, uint8_t *buf, int size)
00190 {
00191 TCPContext *s = h->priv_data;
00192 int ret;
00193
00194 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
00195 ret = ff_network_wait_fd(s->fd, 0);
00196 if (ret < 0)
00197 return ret;
00198 }
00199 ret = recv(s->fd, buf, size, 0);
00200 return ret < 0 ? ff_neterrno() : ret;
00201 }
00202
00203 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
00204 {
00205 TCPContext *s = h->priv_data;
00206 int ret;
00207
00208 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
00209 ret = ff_network_wait_fd(s->fd, 1);
00210 if (ret < 0)
00211 return ret;
00212 }
00213 ret = send(s->fd, buf, size, 0);
00214 return ret < 0 ? ff_neterrno() : ret;
00215 }
00216
00217 static int tcp_shutdown(URLContext *h, int flags)
00218 {
00219 TCPContext *s = h->priv_data;
00220 int how;
00221
00222 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
00223 how = SHUT_RDWR;
00224 } else if (flags & AVIO_FLAG_WRITE) {
00225 how = SHUT_WR;
00226 } else {
00227 how = SHUT_RD;
00228 }
00229
00230 return shutdown(s->fd, how);
00231 }
00232
00233 static int tcp_close(URLContext *h)
00234 {
00235 TCPContext *s = h->priv_data;
00236 closesocket(s->fd);
00237 return 0;
00238 }
00239
00240 static int tcp_get_file_handle(URLContext *h)
00241 {
00242 TCPContext *s = h->priv_data;
00243 return s->fd;
00244 }
00245
00246 URLProtocol ff_tcp_protocol = {
00247 .name = "tcp",
00248 .url_open = tcp_open,
00249 .url_read = tcp_read,
00250 .url_write = tcp_write,
00251 .url_close = tcp_close,
00252 .url_get_file_handle = tcp_get_file_handle,
00253 .url_shutdown = tcp_shutdown,
00254 .priv_data_size = sizeof(TCPContext),
00255 .flags = URL_PROTOCOL_FLAG_NETWORK,
00256 };