1 /*****************************************************************************
2 * srt_common.c: SRT (Secure Reliable Transport) access module
3 *****************************************************************************
5 * Copyright (C) 2019, Haivision Systems Inc.
7 * Author: Aaron Boxer <aaron.boxer@collabora.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include "srt_common.h"
26 const char * const srt_key_length_names
[] = { N_( "16 bytes" ), N_(
27 "24 bytes" ), N_( "32 bytes" ), };
29 typedef struct parsed_param
{
35 find(char *str
, char find
)
37 str
= strchr( str
, find
);
38 return str
!= NULL
? str
+ 1 : NULL
;
42 * Parse a query string into an array of key/value structs.
44 * The query string should be a null terminated string of parameters separated
45 * by a delimiter. Each parameter are checked for the equal sign character.
46 * If it appears in the parameter, it will be used as a null terminator
47 * and the part that comes after it will be the value of the parameter.
50 * param: query: the query string to parse. The string will be modified.
51 * param: delimiter: the character that separates the key/value pairs
53 * param: params: an array of parsed_param structs to hold the result.
54 * param: max_params: maximum number of parameters to parse.
56 * Return: the number of parsed items. -1 if there was an error.
58 static int srt_url_parse_query(char *query
, const char* delimiter
,
59 parsed_param_t
*params
, int max_params
)
64 if (!query
|| *query
== '\0')
66 if (!params
|| max_params
== 0)
69 token
= strtok( query
, delimiter
);
70 while (token
!= NULL
&& i
< max_params
) {
71 params
[i
].key
= token
;
73 if ((params
[i
].val
= strchr( params
[i
].key
, '=' )) != NULL
) {
74 size_t val_len
= strlen( params
[i
].val
);
76 /* make key into a zero-delimited string */
77 *(params
[i
].val
) = '\0';
79 /* make sure val is not empty */
83 /* make sure key is not empty */
88 token
= strtok( NULL
, delimiter
);
93 bool srt_parse_url(char* url
, srt_params_t
* params
)
96 struct parsed_param local_params
[32];
101 if (!url
|| !url
[0] || !params
)
104 /* initialize params */
105 params
->latency
= -1;
106 params
->passphrase
= NULL
;
107 params
->key_length
= -1;
108 params
->payload_size
= -1;
109 params
->bandwidth_overhead_limit
= -1;
111 /* Parse URL parameters */
112 query
= find( url
, '?' );
114 num_params
= srt_url_parse_query( query
, "&", local_params
,
115 sizeof(local_params
) / sizeof(struct parsed_param
) );
116 if (num_params
> 0) {
118 for (i
= 0; i
< num_params
; ++i
) {
119 char* val
= local_params
[i
].val
;
123 if (strcmp( local_params
[i
].key
, SRT_PARAM_LATENCY
) == 0) {
124 int temp
= atoi( val
);
126 params
->latency
= temp
;
127 } else if (strcmp( local_params
[i
].key
, SRT_PARAM_PASSPHRASE
)
129 params
->passphrase
= val
;
130 } else if (strcmp( local_params
[i
].key
, SRT_PARAM_PAYLOAD_SIZE
)
132 int temp
= atoi( val
);
134 params
->payload_size
= temp
;
135 } else if (strcmp( local_params
[i
].key
, SRT_PARAM_KEY_LENGTH
)
137 int temp
= atoi( val
);
138 if (temp
== srt_key_lengths
[0] || temp
== srt_key_lengths
[1]
139 || temp
== srt_key_lengths
[2]) {
140 params
->key_length
= temp
;
142 } else if (strcmp( local_params
[i
].key
,
143 SRT_PARAM_BANDWIDTH_OVERHEAD_LIMIT
) == 0) {
144 int temp
= atoi( val
);
146 params
->bandwidth_overhead_limit
= temp
;
156 int srt_set_socket_option(vlc_object_t
*this, const char *srt_param
,
157 SRTSOCKET u
, SRT_SOCKOPT opt
, const void *optval
, int optlen
)
161 stat
= srt_setsockopt( u
, 0, opt
, optval
, optlen
);
163 msg_Err( this, "Failed to set socket option %s (reason: %s)", srt_param
,
164 srt_getlasterror_str() );