From 57b5555c91a9792e3ac99102b3d1a5c44b45fdd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Fri, 5 Mar 2010 22:35:21 +0000 Subject: [PATCH] Use ff_url_join for assembling URLs, instead of snprintf This ensures proper escaping of numerical IPv6 addresses. The RTSP (de)muxer needs its own network initialization, since it isn't a protocol and url_open hasn't been called yet. Originally committed as revision 22226 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavformat/gopher.c | 2 +- libavformat/http.c | 8 ++------ libavformat/rtmpproto.c | 4 ++-- libavformat/rtpproto.c | 6 +++--- libavformat/rtsp.c | 33 +++++++++++++++++++++------------ libavformat/rtspenc.c | 1 + 6 files changed, 30 insertions(+), 24 deletions(-) diff --git a/libavformat/gopher.c b/libavformat/gopher.c index abd1cc394a..396b358cb1 100644 --- a/libavformat/gopher.c +++ b/libavformat/gopher.c @@ -95,7 +95,7 @@ static int gopher_open(URLContext *h, const char *uri, int flags) if (port < 0) port = 70; - snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port); + ff_url_join(buf, sizeof(buf), "tcp", NULL, hostname, port, NULL); s->hd = NULL; err = url_open(&s->hd, buf, URL_RDWR); diff --git a/libavformat/http.c b/libavformat/http.c index 0c07592039..da200213ce 100644 --- a/libavformat/http.c +++ b/libavformat/http.c @@ -71,11 +71,7 @@ static int http_open_cnx(URLContext *h) /* needed in any case to build the host string */ url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, path1, sizeof(path1), s->location); - if (port > 0) { - snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port); - } else { - av_strlcpy(hoststr, hostname, sizeof(hoststr)); - } + ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL); if (use_proxy) { url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, @@ -90,7 +86,7 @@ static int http_open_cnx(URLContext *h) if (port < 0) port = 80; - snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port); + ff_url_join(buf, sizeof(buf), "tcp", NULL, hostname, port, NULL); err = url_open(&hd, buf, URL_RDWR); if (err < 0) goto fail; diff --git a/libavformat/rtmpproto.c b/libavformat/rtmpproto.c index b5f419cb7e..7fd0da8144 100644 --- a/libavformat/rtmpproto.c +++ b/libavformat/rtmpproto.c @@ -113,7 +113,7 @@ static void gen_connect(URLContext *s, RTMPContext *rt, const char *proto, ff_rtmp_packet_create(&pkt, RTMP_SYSTEM_CHANNEL, RTMP_PT_INVOKE, 0, 4096); p = pkt.data; - snprintf(tcurl, sizeof(tcurl), "%s://%s:%d/%s", proto, host, port, rt->app); + ff_url_join(tcurl, sizeof(tcurl), proto, NULL, host, port, "/%s", rt->app); ff_amf_write_string(&p, "connect"); ff_amf_write_number(&p, 1.0); ff_amf_write_object_start(&p); @@ -817,7 +817,7 @@ static int rtmp_open(URLContext *s, const char *uri, int flags) if (port < 0) port = RTMP_DEFAULT_PORT; - snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port); + ff_url_join(buf, sizeof(buf), "tcp", NULL, hostname, port, NULL); if (url_open(&rt->stream, buf, URL_RDWR) < 0) { av_log(LOG_CONTEXT, AV_LOG_ERROR, "Cannot open connection %s\n", buf); diff --git a/libavformat/rtpproto.c b/libavformat/rtpproto.c index 4af97c1822..4de905e0b3 100644 --- a/libavformat/rtpproto.c +++ b/libavformat/rtpproto.c @@ -67,10 +67,10 @@ int rtp_set_remote_url(URLContext *h, const char *uri) url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, path, sizeof(path), uri); - snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port, path); + ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path); udp_set_remote_url(s->rtp_hd, buf); - snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port + 1, path); + ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port + 1, "%s", path); udp_set_remote_url(s->rtcp_hd, buf); return 0; } @@ -101,7 +101,7 @@ static void build_udp_url(char *buf, int buf_size, int local_port, int ttl, int max_packet_size) { - snprintf(buf, buf_size, "udp://%s:%d", hostname, port); + ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL); if (local_port >= 0) url_add_option(buf, buf_size, "localport=%d", local_port); if (ttl >= 0) diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c index bc0b3b3f42..c7c6fc3422 100644 --- a/libavformat/rtsp.c +++ b/libavformat/rtsp.c @@ -1090,8 +1090,8 @@ static int make_setup_request(AVFormatContext *s, const char *host, int port, /* first try in specified port range */ if (RTSP_RTP_PORT_MIN != 0) { while (j <= RTSP_RTP_PORT_MAX) { - snprintf(buf, sizeof(buf), "rtp://%s?localport=%d", - host, j); + ff_url_join(buf, sizeof(buf), "rtp", NULL, host, -1, + "?localport=%d", j); /* we will use two ports per rtp stream (rtp and rtcp) */ j += 2; if (url_open(&rtsp_st->rtp_handle, buf, URL_RDWR) == 0) @@ -1201,8 +1201,8 @@ static int make_setup_request(AVFormatContext *s, const char *host, int port, char url[1024]; /* XXX: also use address if specified */ - snprintf(url, sizeof(url), "rtp://%s:%d", - host, reply->transports[0].server_port_min); + ff_url_join(url, sizeof(url), "rtp", NULL, host, + reply->transports[0].server_port_min, NULL); if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) && rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) { err = AVERROR_INVALIDDATA; @@ -1230,8 +1230,8 @@ static int make_setup_request(AVFormatContext *s, const char *host, int port, port = rtsp_st->sdp_port; ttl = rtsp_st->sdp_ttl; } - snprintf(url, sizeof(url), "rtp://%s:%d?ttl=%d", - inet_ntoa(in), port, ttl); + ff_url_join(url, sizeof(url), "rtp", NULL, inet_ntoa(in), + port, "?ttl=%d", ttl); if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) { err = AVERROR_INVALIDDATA; goto fail; @@ -1388,6 +1388,9 @@ int ff_rtsp_connect(AVFormatContext *s) RTSPMessageHeader reply1, *reply = &reply1; int lower_transport_mask = 0; char real_challenge[64]; + + if (!ff_network_init()) + return AVERROR(EIO); redirect: /* extract hostname and port */ url_split(NULL, 0, auth, sizeof(auth), @@ -1447,7 +1450,7 @@ redirect: } /* open the tcp connexion */ - snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port); + ff_url_join(tcpname, sizeof(tcpname), "tcp", NULL, host, port, NULL); if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0) { err = AVERROR(EIO); goto fail; @@ -1531,6 +1534,7 @@ redirect: s->filename); goto redirect; } + ff_network_close(); return err; } #endif @@ -1886,6 +1890,7 @@ static int rtsp_read_close(AVFormatContext *s) ff_rtsp_close_streams(s); url_close(rt->rtsp_hd); + ff_network_close(); return 0; } @@ -1933,6 +1938,9 @@ static int sdp_read_header(AVFormatContext *s, AVFormatParameters *ap) char *content; char url[1024]; + if (!ff_network_init()) + return AVERROR(EIO); + /* read the whole sdp file */ /* XXX: better loading */ content = av_malloc(SDP_MAX_SIZE); @@ -1950,11 +1958,10 @@ static int sdp_read_header(AVFormatContext *s, AVFormatParameters *ap) for (i = 0; i < rt->nb_rtsp_streams; i++) { rtsp_st = rt->rtsp_streams[i]; - snprintf(url, sizeof(url), "rtp://%s:%d?localport=%d&ttl=%d", - inet_ntoa(rtsp_st->sdp_ip), - rtsp_st->sdp_port, - rtsp_st->sdp_port, - rtsp_st->sdp_ttl); + ff_url_join(url, sizeof(url), "rtp", NULL, + inet_ntoa(rtsp_st->sdp_ip), rtsp_st->sdp_port, + "?localport=%d&ttl=%d", rtsp_st->sdp_port, + rtsp_st->sdp_ttl); if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) { err = AVERROR_INVALIDDATA; goto fail; @@ -1965,12 +1972,14 @@ static int sdp_read_header(AVFormatContext *s, AVFormatParameters *ap) return 0; fail: ff_rtsp_close_streams(s); + ff_network_close(); return err; } static int sdp_read_close(AVFormatContext *s) { ff_rtsp_close_streams(s); + ff_network_close(); return 0; } diff --git a/libavformat/rtspenc.c b/libavformat/rtspenc.c index c655e70a98..5f893916f1 100644 --- a/libavformat/rtspenc.c +++ b/libavformat/rtspenc.c @@ -116,6 +116,7 @@ static int rtsp_write_close(AVFormatContext *s) ff_rtsp_close_streams(s); url_close(rt->rtsp_hd); + ff_network_close(); return 0; }