12
12
#include <re_sa.h>
13
13
#include <re_list.h>
14
14
#include <re_tcp.h>
15
+ #include <re_srtp.h>
16
+ #include <re_tls.h>
15
17
#include <re_sys.h>
16
18
#include <re_odict.h>
17
19
#include <re_dns.h>
@@ -38,6 +40,7 @@ static void conn_destructor(void *data)
38
40
mem_deref (conn -> dnsq6 );
39
41
mem_deref (conn -> dnsq4 );
40
42
mem_deref (conn -> dnsc );
43
+ mem_deref (conn -> sc );
41
44
mem_deref (conn -> tc );
42
45
mem_deref (conn -> mb );
43
46
mem_deref (conn -> dechunk );
@@ -358,6 +361,7 @@ static void conn_close(struct rtmp_conn *conn, int err)
358
361
{
359
362
rtmp_close_h * closeh ;
360
363
364
+ conn -> sc = mem_deref (conn -> sc );
361
365
conn -> tc = mem_deref (conn -> tc );
362
366
conn -> dnsq6 = mem_deref (conn -> dnsq6 );
363
367
conn -> dnsq4 = mem_deref (conn -> dnsq4 );
@@ -688,13 +692,25 @@ static int req_connect(struct rtmp_conn *conn)
688
692
conn -> last_ack = 0 ;
689
693
conn -> total_bytes = 0 ;
690
694
conn -> mb = mem_deref (conn -> mb );
695
+ conn -> sc = mem_deref (conn -> sc );
691
696
conn -> tc = mem_deref (conn -> tc );
692
697
693
698
rtmp_dechunker_set_chunksize (conn -> dechunk ,
694
699
RTMP_DEFAULT_CHUNKSIZE );
695
700
696
701
err = tcp_connect (& conn -> tc , addr , tcp_estab_handler ,
697
702
tcp_recv_handler , tcp_close_handler , conn );
703
+
704
+ #ifdef USE_TLS
705
+ if (conn -> tls && !err ) {
706
+ err = tls_start_tcp (& conn -> sc , conn -> tls ,
707
+ conn -> tc , 0 );
708
+ if (!err )
709
+ err = tls_set_verify_server (conn -> sc ,
710
+ conn -> host );
711
+ }
712
+ #endif
713
+
698
714
if (!err )
699
715
break ;
700
716
}
@@ -764,6 +780,7 @@ static void query_handler(int err, const struct dnshdr *hdr, struct list *ansl,
764
780
* @param connp Pointer to allocated RTMP connection object
765
781
* @param dnsc DNS Client for resolving FQDN uris
766
782
* @param uri RTMP uri to connect to
783
+ * @param tls TLS Context (optional)
767
784
* @param estabh Established handler
768
785
* @param cmdh Incoming command handler
769
786
* @param closeh Close handler
@@ -777,35 +794,56 @@ static void query_handler(int err, const struct dnshdr *hdr, struct list *ansl,
777
794
* rtmp://[::1]/vod/mp4:sample.mp4
778
795
*/
779
796
int rtmp_connect (struct rtmp_conn * * connp , struct dnsc * dnsc , const char * uri ,
797
+ struct tls * tls ,
780
798
rtmp_estab_h * estabh , rtmp_command_h * cmdh ,
781
799
rtmp_close_h * closeh , void * arg )
782
800
{
783
801
struct rtmp_conn * conn ;
802
+ struct pl pl_scheme ;
784
803
struct pl pl_hostport ;
785
804
struct pl pl_host ;
786
805
struct pl pl_port ;
787
806
struct pl pl_app ;
788
807
struct pl pl_stream ;
808
+ uint16_t defport ;
789
809
int err ;
790
810
791
811
if (!connp || !uri )
792
812
return EINVAL ;
793
813
794
- if (re_regex (uri , strlen (uri ), "rtmp ://[^/]+/[^/]+/[^]+" ,
795
- & pl_hostport , & pl_app , & pl_stream ))
814
+ if (re_regex (uri , strlen (uri ), "[a-z]+ ://[^/]+/[^/]+/[^]+" ,
815
+ & pl_scheme , & pl_hostport , & pl_app , & pl_stream ))
796
816
return EINVAL ;
797
817
818
+ if (!pl_strcasecmp (& pl_scheme , "rtmp" )) {
819
+ tls = NULL ;
820
+ defport = RTMP_PORT ;
821
+ }
822
+ #ifdef USE_TLS
823
+ else if (!pl_strcasecmp (& pl_scheme , "rtmps" )) {
824
+
825
+ if (!tls )
826
+ return EINVAL ;
827
+
828
+ defport = 443 ;
829
+ }
830
+ #endif
831
+ else
832
+ return ENOTSUP ;
833
+
798
834
if (uri_decode_hostport (& pl_hostport , & pl_host , & pl_port ))
799
835
return EINVAL ;
800
836
801
837
conn = rtmp_conn_alloc (true, estabh , cmdh , closeh , arg );
802
838
if (!conn )
803
839
return ENOMEM ;
804
840
805
- conn -> port = pl_isset (& pl_port ) ? pl_u32 (& pl_port ) : RTMP_PORT ;
841
+ conn -> port = pl_isset (& pl_port ) ? pl_u32 (& pl_port ) : defport ;
842
+ conn -> tls = tls ;
806
843
807
844
err = pl_strdup (& conn -> app , & pl_app );
808
845
err |= pl_strdup (& conn -> stream , & pl_stream );
846
+ err |= pl_strdup (& conn -> host , & pl_host );
809
847
err |= str_dup (& conn -> uri , uri );
810
848
if (err )
811
849
goto out ;
@@ -828,10 +866,6 @@ int rtmp_connect(struct rtmp_conn **connp, struct dnsc *dnsc, const char *uri,
828
866
goto out ;
829
867
}
830
868
831
- err = pl_strdup (& conn -> host , & pl_host );
832
- if (err )
833
- goto out ;
834
-
835
869
conn -> dnsc = mem_ref (dnsc );
836
870
837
871
err = dnsc_query (& conn -> dnsq4 , dnsc , conn -> host , DNS_TYPE_A ,
@@ -866,13 +900,15 @@ int rtmp_connect(struct rtmp_conn **connp, struct dnsc *dnsc, const char *uri,
866
900
*
867
901
* @param connp Pointer to allocated RTMP connection object
868
902
* @param ts TCP socket with pending connection
903
+ * @param tls TLS Context (optional)
869
904
* @param cmdh Incoming command handler
870
905
* @param closeh Close handler
871
906
* @param arg Handler argument
872
907
*
873
908
* @return 0 if success, otherwise errorcode
874
909
*/
875
910
int rtmp_accept (struct rtmp_conn * * connp , struct tcp_sock * ts ,
911
+ struct tls * tls ,
876
912
rtmp_command_h * cmdh , rtmp_close_h * closeh , void * arg )
877
913
{
878
914
struct rtmp_conn * conn ;
@@ -890,6 +926,14 @@ int rtmp_accept(struct rtmp_conn **connp, struct tcp_sock *ts,
890
926
if (err )
891
927
goto out ;
892
928
929
+ #ifdef USE_TLS
930
+ if (tls ) {
931
+ err = tls_start_tcp (& conn -> sc , tls , conn -> tc , 0 );
932
+ if (err )
933
+ goto out ;
934
+ }
935
+ #endif
936
+
893
937
out :
894
938
if (err )
895
939
mem_deref (conn );
0 commit comments