Use getaddrinfo() instead of resolve_host(). Patch by Martin Storsjö

<$firstname()$firstname,st>.

Originally committed as revision 21147 to svn://svn.ffmpeg.org/ffmpeg/trunk
release/0.6
Martin Storsjö 15 years ago committed by Ronald S. Bultje
parent f1888474fa
commit fdcdd5396e
  1. 34
      libavformat/tcp.c

@ -34,7 +34,7 @@ typedef struct TCPContext {
/* return non zero if error */ /* return non zero if error */
static int tcp_open(URLContext *h, const char *uri, int flags) static int tcp_open(URLContext *h, const char *uri, int flags)
{ {
struct sockaddr_in dest_addr; struct addrinfo hints, *ai, *cur_ai;
int port, fd = -1; int port, fd = -1;
TCPContext *s = NULL; TCPContext *s = NULL;
fd_set wfds; fd_set wfds;
@ -42,6 +42,7 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
struct timeval tv; struct timeval tv;
socklen_t optlen; socklen_t optlen;
char hostname[1024],proto[1024],path[1024]; char hostname[1024],proto[1024],path[1024];
char portstr[10];
if(!ff_network_init()) if(!ff_network_init())
return AVERROR(EIO); return AVERROR(EIO);
@ -51,19 +52,23 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
if (strcmp(proto,"tcp") || port <= 0 || port >= 65536) if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
return AVERROR(EINVAL); return AVERROR(EINVAL);
dest_addr.sin_family = AF_INET; memset(&hints, 0, sizeof(hints));
dest_addr.sin_port = htons(port); hints.ai_family = PF_UNSPEC;
if (resolve_host(&dest_addr.sin_addr, hostname) < 0) hints.ai_socktype = SOCK_STREAM;
snprintf(portstr, sizeof(portstr), "%d", port);
if (getaddrinfo(hostname, portstr, &hints, &ai))
return AVERROR(EIO); return AVERROR(EIO);
fd = socket(AF_INET, SOCK_STREAM, 0); cur_ai = ai;
restart:
fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
if (fd < 0) if (fd < 0)
return AVERROR(EIO); goto fail;
ff_socket_nonblock(fd, 1); ff_socket_nonblock(fd, 1);
redo: redo:
ret = connect(fd, (struct sockaddr *)&dest_addr, ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
sizeof(dest_addr));
if (ret < 0) { if (ret < 0) {
if (ff_neterrno() == FF_NETERROR(EINTR)) if (ff_neterrno() == FF_NETERROR(EINTR))
goto redo; goto redo;
@ -94,18 +99,29 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
goto fail; goto fail;
} }
s = av_malloc(sizeof(TCPContext)); s = av_malloc(sizeof(TCPContext));
if (!s) if (!s) {
freeaddrinfo(ai);
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
}
h->priv_data = s; h->priv_data = s;
h->is_streamed = 1; h->is_streamed = 1;
s->fd = fd; s->fd = fd;
freeaddrinfo(ai);
return 0; return 0;
fail: fail:
if (cur_ai->ai_next) {
/* Retry with the next sockaddr */
cur_ai = cur_ai->ai_next;
if (fd >= 0)
closesocket(fd);
goto restart;
}
ret = AVERROR(EIO); ret = AVERROR(EIO);
fail1: fail1:
if (fd >= 0) if (fd >= 0)
closesocket(fd); closesocket(fd);
freeaddrinfo(ai);
return ret; return ret;
} }

Loading…
Cancel
Save