Skip to content

Commit 05a2359

Browse files
alfredhrichaas
authored andcommitted
rtmp: handle both IPv4 and IPv6 uris (creytiv#166)
1 parent 41e771a commit 05a2359

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

include/re_uri.h

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ typedef int (uri_apply_h)(const struct pl *name, const struct pl *val,
2323
struct re_printf;
2424
int uri_encode(struct re_printf *pf, const struct uri *uri);
2525
int uri_decode(struct uri *uri, const struct pl *pl);
26+
int uri_decode_hostport(const struct pl *hostport, struct pl *host,
27+
struct pl *port);
2628
int uri_param_get(const struct pl *pl, const struct pl *pname,
2729
struct pl *pvalue);
2830
int uri_params_apply(const struct pl *pl, uri_apply_h *ah, void *arg);

src/rtmp/conn.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <re_sys.h>
1616
#include <re_odict.h>
1717
#include <re_dns.h>
18+
#include <re_uri.h>
1819
#include <re_rtmp.h>
1920
#include "rtmp.h"
2021

@@ -773,12 +774,14 @@ static void query_handler(int err, const struct dnshdr *hdr, struct list *ansl,
773774
* Example URIs:
774775
*
775776
* rtmp://a.rtmp.youtube.com/live2/my-stream
777+
* rtmp://[::1]/vod/mp4:sample.mp4
776778
*/
777779
int rtmp_connect(struct rtmp_conn **connp, struct dnsc *dnsc, const char *uri,
778780
rtmp_estab_h *estabh, rtmp_command_h *cmdh,
779781
rtmp_close_h *closeh, void *arg)
780782
{
781783
struct rtmp_conn *conn;
784+
struct pl pl_hostport;
782785
struct pl pl_host;
783786
struct pl pl_port;
784787
struct pl pl_app;
@@ -788,8 +791,11 @@ int rtmp_connect(struct rtmp_conn **connp, struct dnsc *dnsc, const char *uri,
788791
if (!connp || !uri)
789792
return EINVAL;
790793

791-
if (re_regex(uri, strlen(uri), "rtmp://[^:/]+[:]*[0-9]*/[^/]+/[^]+",
792-
&pl_host, NULL, &pl_port, &pl_app, &pl_stream))
794+
if (re_regex(uri, strlen(uri), "rtmp://[^/]+/[^/]+/[^]+",
795+
&pl_hostport, &pl_app, &pl_stream))
796+
return EINVAL;
797+
798+
if (uri_decode_hostport(&pl_hostport, &pl_host, &pl_port))
793799
return EINVAL;
794800

795801
conn = rtmp_conn_alloc(true, estabh, cmdh, closeh, arg);

src/uri/uri.c

+11-4
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,18 @@ int uri_encode(struct re_printf *pf, const struct uri *uri)
7474
/**
7575
* Decode host-port portion of a URI (if present)
7676
*
77+
* @param hostport Host and port input string
78+
* @param host Decoded host portion
79+
* @param port Decoded port portion
80+
*
7781
* @return 0 if success, otherwise errorcode
7882
*/
79-
static int decode_hostport(const struct pl *hostport, struct pl *host,
80-
struct pl *port)
83+
int uri_decode_hostport(const struct pl *hostport, struct pl *host,
84+
struct pl *port)
8185
{
86+
if (!hostport || !host || !port)
87+
return EINVAL;
88+
8289
/* Try IPv6 first */
8390
if (!re_regex(hostport->p, hostport->l, "\\[[0-9a-f:]+\\][:]*[0-9]*",
8491
host, NULL, port))
@@ -114,15 +121,15 @@ int uri_decode(struct uri *uri, const struct pl *pl)
114121
&uri->scheme, &uri->user, NULL, &uri->password,
115122
&hostport, &uri->params, &uri->headers)) {
116123

117-
if (0 == decode_hostport(&hostport, &uri->host, &port))
124+
if (0 == uri_decode_hostport(&hostport, &uri->host, &port))
118125
goto out;
119126
}
120127

121128
memset(uri, 0, sizeof(*uri));
122129
err = re_regex(pl->p, pl->l, "[^:]+:[^;? ]+[^?]*[^]*",
123130
&uri->scheme, &hostport, &uri->params, &uri->headers);
124131
if (0 == err) {
125-
err = decode_hostport(&hostport, &uri->host, &port);
132+
err = uri_decode_hostport(&hostport, &uri->host, &port);
126133
if (0 == err)
127134
goto out;
128135
}

0 commit comments

Comments
 (0)