From d05cb726bdd50286a72fa23554bb3257ba23cae4 Mon Sep 17 00:00:00 2001 From: Luca Abeni Date: Wed, 14 Nov 2007 08:16:51 +0000 Subject: Simplify set_remote_url(), and remove some code duplication Originally committed as revision 11016 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavformat/udp.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) (limited to 'libavformat/udp.c') diff --git a/libavformat/udp.c b/libavformat/udp.c index 2c40415a1c..c1ff2b181e 100644 --- a/libavformat/udp.c +++ b/libavformat/udp.c @@ -145,18 +145,17 @@ static struct addrinfo* udp_ipv6_resolve_host(const char *hostname, int port, in return res; } -static int udp_ipv6_set_remote_url(URLContext *h, const char *uri) { - UDPContext *s = h->priv_data; - char hostname[256]; - int port; +static int udp_set_url(struct sockaddr_storage *addr, const char *hostname, int port) { struct addrinfo *res0; - url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri); + int addr_len; + res0 = udp_ipv6_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0); if (res0 == 0) return AVERROR(EIO); - memcpy(&s->dest_addr, res0->ai_addr, res0->ai_addrlen); - s->dest_addr_len = res0->ai_addrlen; + memcpy(addr, res0->ai_addr, res0->ai_addrlen); + addr_len = res0->ai_addrlen; freeaddrinfo(res0); - return 0; + + return addr_len; } static int udp_ipv6_set_local(URLContext *h) { @@ -213,6 +212,19 @@ static int udp_ipv6_set_local(URLContext *h) { return -1; } +#else + +static int udp_set_url(struct sockaddr_in *addr, const char *hostname, int port) +{ + /* set the destination address */ + if (resolve_host(&addr->sin_addr, hostname) < 0) + return AVERROR(EIO); + addr->sin_family = AF_INET; + addr->sin_port = htons(port); + + return sizeof(struct sockaddr_in); +} + #endif /* CONFIG_IPV6 */ @@ -234,9 +246,6 @@ static int udp_ipv6_set_local(URLContext *h) { */ int udp_set_remote_url(URLContext *h, const char *uri) { -#ifdef CONFIG_IPV6 - return udp_ipv6_set_remote_url(h, uri); -#else UDPContext *s = h->priv_data; char hostname[256]; int port; @@ -244,13 +253,12 @@ int udp_set_remote_url(URLContext *h, const char *uri) url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri); /* set the destination address */ - if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0) + s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port); + if (s->dest_addr_len < 0) { return AVERROR(EIO); - s->dest_addr.sin_family = AF_INET; - s->dest_addr.sin_port = htons(port); - s->dest_addr_len = sizeof(s->dest_addr); + } + return 0; -#endif } /** -- cgit v1.2.3