1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
|
#include "control_ng_flags_parser.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ctype.h>
#include "log.h"
#include "log_funcs.h"
/**
* Data structures.
*/
static const char *transports[] = {
[0x00] = "RTP/AVP",
[0x01] = "RTP/SAVP",
[0x02] = "RTP/AVPF",
[0x03] = "RTP/SAVPF",
[0x04] = "UDP/TLS/RTP/SAVP",
[0x06] = "UDP/TLS/RTP/SAVPF",
};
/**
* Helpers.
*/
/* parsing of key and val from string */
static bool get_key_val(str * key, str * val, str *in_out)
{
if (!str_token_sep(key, in_out, ' '))
return false;
// key=value ?
str k;
*val = STR_NULL;
if (!str_token_sep(&k, key, '='))
return true;
*val = *key;
*key = k;
return true;
}
/* handle either "foo-bar" or "foo=bar" from flags */
static bool str_key_val_prefix(const str * p, const char * q,
const str * v, str * out)
{
if(str_eq(p, q)) {
if(!v->s || !v->len)
return 0;
*out = *v;
return true;
}
*out = *p;
if (str_shift_cmp(out, q))
return false;
if(out->len < 2)
return false;
if(*out->s != '-')
return false;
out->s++;
out->len--;
return true;
}
static inline bool skip_char(str *s, char c) {
if (s->len == 0 || s->s[0] != c)
return false;
str_shift(s, 1);
return true;
}
static inline void skip_chars(str *s, char c) {
while (skip_char(s, c));
}
static int rtpp_is_dict_list(rtpp_pos *a) {
str list = a->cur;
if (!skip_char(&list, '['))
return 0;
// check contents
if (list.len == 0)
list = a->remainder; // could be just leading spaces?
if (list.len == 0)
return 0; // unexpected end of string
if (list.s[0] == '[')
return 1; // contains sub-list, must be a list
// inspect first element for 'key='
str key, val;
if (!get_key_val(&key, &val, &list))
return 0; // nothing to read
if (val.len)
return 2; // is a dict
return 1; // is a list
}
static bool rtpp_is_list(rtpp_pos *a) {
return rtpp_is_dict_list(a) == 1;
}
static bool rtpp_is_dict(rtpp_pos *a) {
return rtpp_is_dict_list(a) == 2;
}
static str *rtpp_get_str(rtpp_pos *a, str *b) {
if (rtpp_is_dict_list(a) != 0)
return NULL;
if (a->cur.len == 0)
return NULL;
*b = a->cur;
return b;
}
static long long rtpp_get_int_str(rtpp_pos *a, long long def) {
str s;
if (!rtpp_get_str(a, &s))
return def;
return str_to_i(&s, def);
}
static bool rtpp_dict_list_end_rewind(rtpp_pos *pos) {
// check for dict/list end, which is only valid if it doesn't also start one
if (pos->cur.len == 0 || pos->cur.s[0] == '[' || pos->cur.s[pos->cur.len - 1] != ']')
return false;
pos->cur.len--;
// remove any extra closing bracket, and return them to the remainder for
// the upper level function to parse
while (pos->cur.len && pos->cur.s[pos->cur.len - 1] == ']') {
pos->cur.len--;
pos->remainder.s--;
pos->remainder.len++;
// we might be on a space or something - go to the actual bracket, which must
// be there somewhere
while (pos->remainder.s[0] != ']') {
pos->remainder.s--;
pos->remainder.len++;
}
}
return true;
}
static bool rtpp_dict_list_closing(rtpp_pos *pos) {
if (pos->cur.s[0] != ']')
return false;
str_shift(&pos->cur, 1);
// anything left in the string, return it to the remainder
pos->remainder.len += pos->remainder.s - pos->cur.s;
pos->remainder.s = pos->cur.s;
return true;
}
static void rtpp_list_iter(const ng_parser_t *parser, rtpp_pos *pos,
void (*str_callback)(str *key, unsigned int, helper_arg),
void (*item_callback)(const ng_parser_t *, parser_arg, helper_arg), helper_arg arg)
{
// list opener
if (!skip_char(&pos->cur, '['))
return;
unsigned int idx = 0;
while (true) {
skip_chars(&pos->cur, ' ');
if (!pos->cur.len)
goto next; // empty token?
// list closing?
if (rtpp_dict_list_closing(pos))
break;
// does it start another list or dict?
if (pos->cur.s[0] == '[') {
if (item_callback)
item_callback(parser, pos, arg);
goto next;
}
// guess it's a string token
// does it end the list?
bool end = rtpp_dict_list_end_rewind(pos);
if (pos->cur.len == 0)
break; // nothing left
if (str_callback)
str_callback(&pos->cur, idx++, arg);
if (end)
break;
goto next;
next:
// find next token in remainder, put in `cur`
if (!str_token_sep(&pos->cur, &pos->remainder, ' '))
break;
}
}
static bool rtpp_dict_iter(const ng_parser_t *parser, rtpp_pos *pos,
void (*callback)(const ng_parser_t *, str *, parser_arg, helper_arg),
helper_arg arg)
{
// list opener
if (!skip_char(&pos->cur, '['))
return false;
while (true) {
skip_chars(&pos->cur, ' ');
if (!pos->cur.len)
goto next; // empty token?
// dict closing?
if (rtpp_dict_list_closing(pos))
break;
str key;
if (!str_token(&key, &pos->cur, '=')) {
ilog(LOG_ERR, "Entry in dictionary without equals sign ('" STR_FORMAT "'), aborting",
STR_FMT(&pos->cur));
break;
}
// guess it's a string token
// does it end the dict?
bool end = rtpp_dict_list_end_rewind(pos);
if (pos->cur.len == 0)
break; // nothing left
callback(parser, &key, pos, arg);
if (rtpp_is_dict_list(pos))
rtpp_list_iter(parser, pos, NULL, NULL, NULL);
if (end)
break;
goto next;
next:
// find next token in remainder, put in `cur`
if (!str_token_sep(&pos->cur, &pos->remainder, ' '))
break;
}
return true;
}
static bool rtpp_is_int(rtpp_pos *pos) {
return false;
}
const ng_parser_t dummy_parser = {
.is_list = rtpp_is_list,
.is_dict = rtpp_is_dict,
.is_int = rtpp_is_int,
.list_iter = rtpp_list_iter,
.dict_iter = rtpp_dict_iter,
.get_str = rtpp_get_str,
.get_int_str = rtpp_get_int_str,
};
static bool parse_codec_to_dict(str * key, str * val, const char *cmp1, const char *cmp2,
const char * dictstr, sdp_ng_flags *flags)
{
str s;
if(!str_key_val_prefix(key, cmp1, val, &s)) {
if(!cmp2)
return false;
if(!str_key_val_prefix(key, cmp2, val, &s))
return false;
}
call_ng_codec_flags(&dummy_parser, STR_PTR(dictstr), &(rtpp_pos) {.cur = s}, flags);
return true;
}
/* parse codec related flags */
static bool parse_codecs(sdp_ng_flags *flags, str * key, str * val) {
if (parse_codec_to_dict(key, val, "transcode",
"codec-transcode", "transcode", flags) ||
parse_codec_to_dict(key, val, "codec-strip",
NULL, "strip", flags) ||
parse_codec_to_dict(key, val, "codec-offer",
NULL, "offer", flags) ||
parse_codec_to_dict(key, val, "codec-mask",
NULL, "mask", flags) ||
parse_codec_to_dict(key, val, "codec-set",
NULL, "set", flags) ||
parse_codec_to_dict(key, val, "codec-accept",
NULL, "accept", flags) ||
parse_codec_to_dict(key, val, "codec-except",
NULL, "except", flags))
{
return true;
}
return false;
}
/* prase transport, such as for example RTP/AVP */
static void parse_transports(unsigned int transport, sdp_ng_flags *out)
{
const char * val = transports[transport & 0x007];
if (!val)
return;
call_ng_main_flags(&dummy_parser, &STR_CONST("transport-protocol"),
&(rtpp_pos) {.cur = STR(val), .remainder = STR_NULL}, out);
}
static void rtpp_direction_flag(sdp_ng_flags *flags, unsigned int *flagnum, str *val) {
static const str keys[2] = {STR_CONST("from-interface"), STR_CONST("to-interface")};
if (*flagnum >= G_N_ELEMENTS(keys)) {
ilog(LOG_WARN, "Too many 'direction=...' flags encountered");
return;
}
str key = keys[(*flagnum)++];
call_ng_main_flags(&dummy_parser, &key, &(rtpp_pos) {.cur = *val}, flags);
}
/**
* Parse flags from bencode string into given bencode dictionary.
*
* Params:
* @param rtpp_flags - raw str rtpp_flags
* @param dict - root dict to store encoded flags
*/
void parse_rtpp_flags(const str * rtpp_flags, sdp_ng_flags *out)
{
str remainder, key, val;
unsigned int direction_flag = 0;
unsigned int transport = 0;
if (!rtpp_flags->s)
return;
remainder = *rtpp_flags;
while (remainder.len)
{
/* skip spaces */
skip_chars(&remainder, ' ');
/* set key and val */
if (!get_key_val(&key, &val, &remainder))
break;
/* codecs have own specific parsing as well */
if (parse_codecs(out, &key, &val))
goto next;
/* parse other generic flags */
switch (key.len)
{
case 3:
/* transport */
if (!val.s && str_eq(&key, "RTP"))
transport = (transport | 0x100) & ~0x001;
else if (!val.s && str_eq(&key, "AVP"))
transport = (transport | 0x100) & ~0x002;
/* other non-defined flags */
else
goto generic;
goto next;
break;
case 4:
/* transport */
if (!val.s && str_eq(&key, "SRTP"))
transport |= 0x101;
else if (!val.s && str_eq(&key, "AVPF"))
transport |= 0x102;
else if (!val.s && str_eq(&key, "DTLS"))
transport |= 0x104;
/* other non-defined flags */
else
goto generic;
goto next;
break;
case 7:
/* transport */
if (!val.s && str_eq(&key, "RTP/AVP"))
transport = 0x100;
/* other non-defined flags */
else
goto generic;
goto next;
break;
case 8:
/* transport */
if (!val.s && str_eq(&key, "RTP/AVPF"))
transport = 0x102;
else if (!val.s && str_eq(&key, "RTP/SAVP"))
transport = 0x101;
/* from-tag can be overriden, but originally has to be provided */
else if (val.s && str_eq(&key, "from-tag")) {
out->directional = 1; /* explicitly add directional for this case */
goto generic;
}
/* direction */
else if (str_eq(&key, "internal") || str_eq(&key, "external"))
rtpp_direction_flag(out, &direction_flag, &key);
/* other non-defined flags */
else
goto generic;
goto next;
break;
case 9:
/* transport */
if (!val.s && str_eq(&key, "RTP/SAVPF"))
transport = 0x103;
/* direction */
else if (str_eq(&key, "direction") && rtpp_is_dict_list(&(rtpp_pos) {.cur=val}) == 0)
rtpp_direction_flag(out, &direction_flag, &val);
else
goto generic;
goto next;
break;
case 16:
/* transport */
if (!val.s && str_eq(&key, "UDP/TLS/RTP/SAVP"))
transport = 0x104;
else
goto generic;
goto next;
break;
case 17:
/* transport */
if (!val.s && str_eq(&key, "UDP/TLS/RTP/SAVPF"))
transport = 0x106;
else
goto generic;
goto next;
break;
}
generic:
/* generic one key flags */
if (!val.len)
call_ng_flags_flags(&key, 0, out);
/* generic flags with value, but no particular processing */
else {
rtpp_pos pos = { .cur = val, .remainder = remainder };
call_ng_main_flags(&dummy_parser, &key, &pos, out);
if (rtpp_is_dict_list(&pos))
rtpp_list_iter(&dummy_parser, &pos, NULL, NULL, NULL);
remainder = pos.remainder;
}
next:;
}
/* define transport */
if (transport)
parse_transports(transport, out);
}
|