Commit 786bb080 authored by mstorsjo's avatar mstorsjo

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.


git-svn-id: file:///var/local/repositories/ffmpeg/trunk@22226 9553f0bf-9b14-0410-a0b8-cfaf0461ba5b
parent 798adbfe
...@@ -95,7 +95,7 @@ static int gopher_open(URLContext *h, const char *uri, int flags) ...@@ -95,7 +95,7 @@ static int gopher_open(URLContext *h, const char *uri, int flags)
if (port < 0) if (port < 0)
port = 70; 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; s->hd = NULL;
err = url_open(&s->hd, buf, URL_RDWR); err = url_open(&s->hd, buf, URL_RDWR);
......
...@@ -71,11 +71,7 @@ static int http_open_cnx(URLContext *h) ...@@ -71,11 +71,7 @@ static int http_open_cnx(URLContext *h)
/* needed in any case to build the host string */ /* needed in any case to build the host string */
url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
path1, sizeof(path1), s->location); path1, sizeof(path1), s->location);
if (port > 0) { ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
} else {
av_strlcpy(hoststr, hostname, sizeof(hoststr));
}
if (use_proxy) { if (use_proxy) {
url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
...@@ -90,7 +86,7 @@ static int http_open_cnx(URLContext *h) ...@@ -90,7 +86,7 @@ static int http_open_cnx(URLContext *h)
if (port < 0) if (port < 0)
port = 80; 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); err = url_open(&hd, buf, URL_RDWR);
if (err < 0) if (err < 0)
goto fail; goto fail;
......
...@@ -113,7 +113,7 @@ static void gen_connect(URLContext *s, RTMPContext *rt, const char *proto, ...@@ -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); ff_rtmp_packet_create(&pkt, RTMP_SYSTEM_CHANNEL, RTMP_PT_INVOKE, 0, 4096);
p = pkt.data; 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_string(&p, "connect");
ff_amf_write_number(&p, 1.0); ff_amf_write_number(&p, 1.0);
ff_amf_write_object_start(&p); ff_amf_write_object_start(&p);
...@@ -817,7 +817,7 @@ static int rtmp_open(URLContext *s, const char *uri, int flags) ...@@ -817,7 +817,7 @@ static int rtmp_open(URLContext *s, const char *uri, int flags)
if (port < 0) if (port < 0)
port = RTMP_DEFAULT_PORT; 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) { if (url_open(&rt->stream, buf, URL_RDWR) < 0) {
av_log(LOG_CONTEXT, AV_LOG_ERROR, "Cannot open connection %s\n", buf); av_log(LOG_CONTEXT, AV_LOG_ERROR, "Cannot open connection %s\n", buf);
......
...@@ -67,10 +67,10 @@ int rtp_set_remote_url(URLContext *h, const char *uri) ...@@ -67,10 +67,10 @@ int rtp_set_remote_url(URLContext *h, const char *uri)
url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
path, sizeof(path), uri); 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); 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); udp_set_remote_url(s->rtcp_hd, buf);
return 0; return 0;
} }
...@@ -101,7 +101,7 @@ static void build_udp_url(char *buf, int buf_size, ...@@ -101,7 +101,7 @@ static void build_udp_url(char *buf, int buf_size,
int local_port, int ttl, int local_port, int ttl,
int max_packet_size) 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) if (local_port >= 0)
url_add_option(buf, buf_size, "localport=%d", local_port); url_add_option(buf, buf_size, "localport=%d", local_port);
if (ttl >= 0) if (ttl >= 0)
......
...@@ -1090,8 +1090,8 @@ static int make_setup_request(AVFormatContext *s, const char *host, int port, ...@@ -1090,8 +1090,8 @@ static int make_setup_request(AVFormatContext *s, const char *host, int port,
/* first try in specified port range */ /* first try in specified port range */
if (RTSP_RTP_PORT_MIN != 0) { if (RTSP_RTP_PORT_MIN != 0) {
while (j <= RTSP_RTP_PORT_MAX) { while (j <= RTSP_RTP_PORT_MAX) {
snprintf(buf, sizeof(buf), "rtp://%s?localport=%d", ff_url_join(buf, sizeof(buf), "rtp", NULL, host, -1,
host, j); "?localport=%d", j);
/* we will use two ports per rtp stream (rtp and rtcp) */ /* we will use two ports per rtp stream (rtp and rtcp) */
j += 2; j += 2;
if (url_open(&rtsp_st->rtp_handle, buf, URL_RDWR) == 0) 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, ...@@ -1201,8 +1201,8 @@ static int make_setup_request(AVFormatContext *s, const char *host, int port,
char url[1024]; char url[1024];
/* XXX: also use address if specified */ /* XXX: also use address if specified */
snprintf(url, sizeof(url), "rtp://%s:%d", ff_url_join(url, sizeof(url), "rtp", NULL, host,
host, reply->transports[0].server_port_min); reply->transports[0].server_port_min, NULL);
if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) && if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) &&
rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) { rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
err = AVERROR_INVALIDDATA; err = AVERROR_INVALIDDATA;
...@@ -1230,8 +1230,8 @@ static int make_setup_request(AVFormatContext *s, const char *host, int port, ...@@ -1230,8 +1230,8 @@ static int make_setup_request(AVFormatContext *s, const char *host, int port,
port = rtsp_st->sdp_port; port = rtsp_st->sdp_port;
ttl = rtsp_st->sdp_ttl; ttl = rtsp_st->sdp_ttl;
} }
snprintf(url, sizeof(url), "rtp://%s:%d?ttl=%d", ff_url_join(url, sizeof(url), "rtp", NULL, inet_ntoa(in),
inet_ntoa(in), port, ttl); port, "?ttl=%d", ttl);
if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) { if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
err = AVERROR_INVALIDDATA; err = AVERROR_INVALIDDATA;
goto fail; goto fail;
...@@ -1388,6 +1388,9 @@ int ff_rtsp_connect(AVFormatContext *s) ...@@ -1388,6 +1388,9 @@ int ff_rtsp_connect(AVFormatContext *s)
RTSPMessageHeader reply1, *reply = &reply1; RTSPMessageHeader reply1, *reply = &reply1;
int lower_transport_mask = 0; int lower_transport_mask = 0;
char real_challenge[64]; char real_challenge[64];
if (!ff_network_init())
return AVERROR(EIO);
redirect: redirect:
/* extract hostname and port */ /* extract hostname and port */
url_split(NULL, 0, auth, sizeof(auth), url_split(NULL, 0, auth, sizeof(auth),
...@@ -1447,7 +1450,7 @@ redirect: ...@@ -1447,7 +1450,7 @@ redirect:
} }
/* open the tcp connexion */ /* 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) { if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0) {
err = AVERROR(EIO); err = AVERROR(EIO);
goto fail; goto fail;
...@@ -1531,6 +1534,7 @@ redirect: ...@@ -1531,6 +1534,7 @@ redirect:
s->filename); s->filename);
goto redirect; goto redirect;
} }
ff_network_close();
return err; return err;
} }
#endif #endif
...@@ -1886,6 +1890,7 @@ static int rtsp_read_close(AVFormatContext *s) ...@@ -1886,6 +1890,7 @@ static int rtsp_read_close(AVFormatContext *s)
ff_rtsp_close_streams(s); ff_rtsp_close_streams(s);
url_close(rt->rtsp_hd); url_close(rt->rtsp_hd);
ff_network_close();
return 0; return 0;
} }
...@@ -1933,6 +1938,9 @@ static int sdp_read_header(AVFormatContext *s, AVFormatParameters *ap) ...@@ -1933,6 +1938,9 @@ static int sdp_read_header(AVFormatContext *s, AVFormatParameters *ap)
char *content; char *content;
char url[1024]; char url[1024];
if (!ff_network_init())
return AVERROR(EIO);
/* read the whole sdp file */ /* read the whole sdp file */
/* XXX: better loading */ /* XXX: better loading */
content = av_malloc(SDP_MAX_SIZE); content = av_malloc(SDP_MAX_SIZE);
...@@ -1950,11 +1958,10 @@ static int sdp_read_header(AVFormatContext *s, AVFormatParameters *ap) ...@@ -1950,11 +1958,10 @@ static int sdp_read_header(AVFormatContext *s, AVFormatParameters *ap)
for (i = 0; i < rt->nb_rtsp_streams; i++) { for (i = 0; i < rt->nb_rtsp_streams; i++) {
rtsp_st = rt->rtsp_streams[i]; rtsp_st = rt->rtsp_streams[i];
snprintf(url, sizeof(url), "rtp://%s:%d?localport=%d&ttl=%d", ff_url_join(url, sizeof(url), "rtp", NULL,
inet_ntoa(rtsp_st->sdp_ip), inet_ntoa(rtsp_st->sdp_ip), rtsp_st->sdp_port,
rtsp_st->sdp_port, "?localport=%d&ttl=%d", rtsp_st->sdp_port,
rtsp_st->sdp_port, rtsp_st->sdp_ttl);
rtsp_st->sdp_ttl);
if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) { if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
err = AVERROR_INVALIDDATA; err = AVERROR_INVALIDDATA;
goto fail; goto fail;
...@@ -1965,12 +1972,14 @@ static int sdp_read_header(AVFormatContext *s, AVFormatParameters *ap) ...@@ -1965,12 +1972,14 @@ static int sdp_read_header(AVFormatContext *s, AVFormatParameters *ap)
return 0; return 0;
fail: fail:
ff_rtsp_close_streams(s); ff_rtsp_close_streams(s);
ff_network_close();
return err; return err;
} }
static int sdp_read_close(AVFormatContext *s) static int sdp_read_close(AVFormatContext *s)
{ {
ff_rtsp_close_streams(s); ff_rtsp_close_streams(s);
ff_network_close();
return 0; return 0;
} }
......
...@@ -116,6 +116,7 @@ static int rtsp_write_close(AVFormatContext *s) ...@@ -116,6 +116,7 @@ static int rtsp_write_close(AVFormatContext *s)
ff_rtsp_close_streams(s); ff_rtsp_close_streams(s);
url_close(rt->rtsp_hd); url_close(rt->rtsp_hd);
ff_network_close();
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment