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