TURN REST API client.
More...
#include <stdint.h>
#include <glib.h>
#include <jansson.h>
Go to the source code of this file.
|
#define | JANUS_JSON_BOOL JSON_TRUE |
|
#define | JANUS_JSON_PARAM_REQUIRED 1 |
|
#define | JANUS_JSON_PARAM_POSITIVE 2 |
|
#define | JANUS_JSON_PARAM_NONEMPTY 4 |
|
#define | JANUS_VALIDATE_JSON_OBJECT_FORMAT(missing_format, invalid_format, obj, params, error_code, error_cause, log_error, missing_code, invalid_code) |
| Validates the JSON object against the description of its parameters. More...
|
|
#define | JANUS_VALIDATE_JSON_OBJECT(obj, params, error_code, error_cause, log_error, missing_code, invalid_code) JANUS_VALIDATE_JSON_OBJECT_FORMAT("Missing mandatory element (%s)", "Invalid element type (%s should be %s)", obj, params, error_code, error_cause, log_error, missing_code, invalid_code) |
| Validates the JSON object against the description of its parameters. More...
|
|
#define | JANUS_CHECK_SECRET(secret, obj, member, error_code, error_cause, missing_code, invalid_code, unauthorized_code) |
| If the secret isn't NULL, check the secret after validating the specified member of the JSON object. More...
|
|
|
gint64 | janus_get_monotonic_time (void) |
| Helper to retrieve the system monotonic time, as Glib's g_get_monotonic_time may not be available (only since 2.28) More...
|
|
gint64 | janus_get_real_time (void) |
| Helper to retrieve the system real time, as Glib's g_get_real_time may not be available (only since 2.28) More...
|
|
char * | janus_string_replace (char *message, const char *old_string, const char *new_string) G_GNUC_WARN_UNUSED_RESULT |
| Helper to replace strings. More...
|
|
gboolean | janus_is_true (const char *value) |
| Helper to parse yes/no|true/false configuration values. More...
|
|
gboolean | janus_strcmp_const_time (const void *str1, const void *str2) |
| Helper to compare strings in constant time. More...
|
|
guint32 | janus_random_uint32 (void) |
| Helper to generate random 32-bit unsigned integers (useful for SSRCs, etc.) More...
|
|
guint64 | janus_random_uint64 (void) |
| Helper to generate random 64-bit unsigned integers (useful for Janus IDs) More...
|
|
guint64 * | janus_uint64_dup (guint64 num) |
| Helper to generate an allocated copy of a guint64 number. More...
|
|
int | janus_mkdir (const char *dir, mode_t mode) |
| Helper to create a new directory, and recursively create parent directories if needed. More...
|
|
int | janus_get_codec_pt (const char *sdp, const char *codec) |
| Ugly and dirty helper to quickly get the payload type associated with a codec in an SDP. More...
|
|
const char * | janus_get_codec_from_pt (const char *sdp, int pt) |
| Ugly and dirty helper to quickly get the codec associated with a payload type in an SDP. More...
|
|
int | janus_pidfile_create (const char *file) |
| Create and lock a PID file. More...
|
|
int | janus_pidfile_remove (void) |
| Unlock and remove a previously created PID file. More...
|
|
void | janus_get_json_type_name (int jtype, unsigned int flags, char *type_name) |
| Creates a string describing the JSON type and constraint. More...
|
|
gboolean | janus_json_is_valid (json_t *val, json_type jtype, unsigned int flags) |
| Checks whether the JSON value matches the type and constraint. More...
|
|
TURN REST API client.
Utilities and helpers (headers)
TURN REST API client (headers)
- Author
- Lorenzo Miniero loren.nosp@m.zo@m.nosp@m.eetec.nosp@m.ho.c.nosp@m.om
- Copyright
- GNU General Public License v3
Implementation of the draft-uberti-rtcweb-turn-rest-00
draft, that is a REST API that can be used to access TURN services, more specifically credentials to use. Currently implemented in both rfc5766-turn-server and coturn, and so should be generic enough to be usable here.
- Note
- This implementation depends on
libcurl
and is optional.
Core
- Author
- Lorenzo Miniero loren.nosp@m.zo@m.nosp@m.eetec.nosp@m.ho.c.nosp@m.om
- Copyright
- GNU General Public License v3
Implementations of a few methods that may be of use here and there in the code.
Core
◆ JANUS_CHECK_SECRET
#define JANUS_CHECK_SECRET |
( |
|
secret, |
|
|
|
obj, |
|
|
|
member, |
|
|
|
error_code, |
|
|
|
error_cause, |
|
|
|
missing_code, |
|
|
|
invalid_code, |
|
|
|
unauthorized_code |
|
) |
| |
Value:do { \
if (secret) { \
}; \
JANUS_VALIDATE_JSON_OBJECT(obj, secret_parameters, error_code, error_cause, TRUE, missing_code, invalid_code); \
error_code = (unauthorized_code); \
JANUS_LOG(
LOG_ERR,
"Unauthorized (wrong %s)\n", member); \
if(error_cause != NULL) \
g_snprintf(error_cause, sizeof(error_cause), "Unauthorized (wrong %s)", member); \
} \
} \
} while(0)
#define JANUS_JSON_PARAM_REQUIRED
Definition: utils.h:21
#define LOG_ERR
Non-fatal error.
Definition: debug.h:43
gboolean janus_strcmp_const_time(const void *str1, const void *str2)
Helper to compare strings in constant time.
Definition: utils.c:44
If the secret isn't NULL, check the secret after validating the specified member of the JSON object.
- Parameters
-
| secret | The secret to be checked; no check if the secret is NULL |
| obj | The JSON object to be validated |
| member | The JSON member with the secret, usually "secret" or "pin" |
[out] | error_code | int to return error code |
[out] | error_cause | Array of char or NULL to return the error descriptions; the array has to be a global or stack variable to make sizeof work; the required size is 60 |
| missing_code | The code to be returned in error_code if a parameter is missing |
| invalid_code | The code to be returned in error_code if a parameter is invalid |
| unauthorized_code | The code to be returned in error_code if the secret doesn't match |
◆ JANUS_JSON_BOOL
#define JANUS_JSON_BOOL JSON_TRUE |
◆ JANUS_JSON_PARAM_NONEMPTY
#define JANUS_JSON_PARAM_NONEMPTY 4 |
◆ JANUS_JSON_PARAM_POSITIVE
#define JANUS_JSON_PARAM_POSITIVE 2 |
◆ JANUS_JSON_PARAM_REQUIRED
#define JANUS_JSON_PARAM_REQUIRED 1 |
◆ JANUS_VALIDATE_JSON_OBJECT
#define JANUS_VALIDATE_JSON_OBJECT |
( |
|
obj, |
|
|
|
params, |
|
|
|
error_code, |
|
|
|
error_cause, |
|
|
|
log_error, |
|
|
|
missing_code, |
|
|
|
invalid_code |
|
) |
| JANUS_VALIDATE_JSON_OBJECT_FORMAT("Missing mandatory element (%s)", "Invalid element type (%s should be %s)", obj, params, error_code, error_cause, log_error, missing_code, invalid_code) |
Validates the JSON object against the description of its parameters.
- Parameters
-
| obj | The JSON object to be validated |
| params | Array of struct janus_json_parameter to describe the parameters; the array has to be a global or stack variable to make sizeof work |
[out] | error_code | int to return error code |
[out] | error_cause | Array of char or NULL to return the error descriptions; the array has to be a global or stack variable to make sizeof work; the required size is the length of the longest parameter name plus 54 for the format string and type description |
| log_error | If TRUE, log any error with JANUS_LOG(LOG_ERR) |
| missing_code | The code to be returned in error_code if a parameter is missing |
| invalid_code | The code to be returned in error_code if a parameter is invalid |
◆ JANUS_VALIDATE_JSON_OBJECT_FORMAT
#define JANUS_VALIDATE_JSON_OBJECT_FORMAT |
( |
|
missing_format, |
|
|
|
invalid_format, |
|
|
|
obj, |
|
|
|
params, |
|
|
|
error_code, |
|
|
|
error_cause, |
|
|
|
log_error, |
|
|
|
missing_code, |
|
|
|
invalid_code |
|
) |
| |
Value:do { \
error_code = 0; \
unsigned int i; \
json_t *val = json_object_get(obj, params[i].
name); \
if(!val) { \
error_code = (missing_code); \
if(log_error) \
JANUS_LOG(
LOG_ERR, missing_format
"\n", params[i].
name); \
if(error_cause != NULL) \
g_snprintf(error_cause,
sizeof(error_cause), missing_format, params[i].
name); \
break; \
} \
continue; \
} \
error_code = (invalid_code); \
char type_name[20]; \
janus_get_json_type_name(params[i].jtype, params[i].flags, type_name); \
if(log_error) \
JANUS_LOG(
LOG_ERR, invalid_format
"\n", params[i].
name, type_name); \
if(error_cause != NULL) \
g_snprintf(error_cause,
sizeof(error_cause), invalid_format, params[i].
name, type_name); \
break; \
} \
} \
} while(0)
unsigned int flags
Definition: utils.h:28
#define JANUS_JSON_PARAM_REQUIRED
Definition: utils.h:21
const gchar * name
Definition: utils.h:26
json_type jtype
Definition: utils.h:27
#define LOG_ERR
Non-fatal error.
Definition: debug.h:43
gboolean janus_json_is_valid(json_t *val, json_type jtype, unsigned int flags)
Checks whether the JSON value matches the type and constraint.
Definition: utils.c:467
Validates the JSON object against the description of its parameters.
- Parameters
-
| missing_format | printf format to indicate a missing required parameter; needs one s for the parameter name |
| invalid_format | printf format to indicate an invalid parameter; needs two s for parameter name and type description from janus_get_json_type_name |
| obj | The JSON object to be validated |
| params | Array of struct janus_json_parameter to describe the parameters; the array has to be a global or stack variable to make sizeof work |
[out] | error_code | int to return error code |
[out] | error_cause | Array of char or NULL to return the error descriptions; the array has to be a global or stack variable to make sizeof work; the required size is the length of the format string plus the length of the longest parameter name plus 19 for the type description |
| log_error | If TRUE, log any error with JANUS_LOG(LOG_ERR) |
| missing_code | The code to be returned in error_code if a parameter is missing |
| invalid_code | The code to be returned in error_code if a parameter is invalid |
◆ janus_flags
◆ janus_flags_clear()
void janus_flags_clear |
( |
janus_flags * |
flags, |
|
|
uint32_t |
flag |
|
) |
| |
Janus flags clear method.
- Parameters
-
[in] | flags | The janus_flags instance to update |
[in] | flag | The flag to clear |
◆ janus_flags_is_set()
gboolean janus_flags_is_set |
( |
janus_flags * |
flags, |
|
|
uint32_t |
flag |
|
) |
| |
Janus flags check method.
- Parameters
-
[in] | flags | The janus_flags instance to check |
[in] | flag | The flag to check |
- Returns
- true if the flag is set, false otherwise
◆ janus_flags_reset()
Janus flags reset method.
- Parameters
-
[in] | flags | The janus_flags instance to reset |
◆ janus_flags_set()
void janus_flags_set |
( |
janus_flags * |
flags, |
|
|
uint32_t |
flag |
|
) |
| |
Janus flags set method.
- Parameters
-
[in] | flags | The janus_flags instance to update |
[in] | flag | The flag to set |
◆ janus_get_codec_from_pt()
const char* janus_get_codec_from_pt |
( |
const char * |
sdp, |
|
|
int |
pt |
|
) |
| |
Ugly and dirty helper to quickly get the codec associated with a payload type in an SDP.
- Parameters
-
sdp | The SDP to parse |
pt | The payload type to look for |
- Returns
- The codec name, if found, NULL otherwise
◆ janus_get_codec_pt()
int janus_get_codec_pt |
( |
const char * |
sdp, |
|
|
const char * |
codec |
|
) |
| |
Ugly and dirty helper to quickly get the payload type associated with a codec in an SDP.
- Parameters
-
sdp | The SDP to parse |
codec | The codec to look for |
- Returns
- The payload type, if found, -1 otherwise
◆ janus_get_json_type_name()
void janus_get_json_type_name |
( |
int |
jtype, |
|
|
unsigned int |
flags, |
|
|
char * |
type_name |
|
) |
| |
Creates a string describing the JSON type and constraint.
- Parameters
-
| jtype | The JSON type, e.g., JSON_STRING |
| flags | Indicates constraints for the described type |
[out] | type_name | The type description, e.g., "a positive integer"; required size is 19 characters |
- Returns
- 0 if successful, a negative integer otherwise
◆ janus_get_monotonic_time()
gint64 janus_get_monotonic_time |
( |
void |
| ) |
|
Helper to retrieve the system monotonic time, as Glib's g_get_monotonic_time may not be available (only since 2.28)
- Returns
- The system monotonic time
◆ janus_get_real_time()
gint64 janus_get_real_time |
( |
void |
| ) |
|
Helper to retrieve the system real time, as Glib's g_get_real_time may not be available (only since 2.28)
- Returns
- The system real time
◆ janus_is_true()
gboolean janus_is_true |
( |
const char * |
value | ) |
|
Helper to parse yes/no|true/false configuration values.
- Parameters
-
value | The configuration value to parse |
- Returns
- true if the value contains a "yes", "YES", "true", TRUE", "1", false otherwise
◆ janus_json_is_valid()
gboolean janus_json_is_valid |
( |
json_t * |
val, |
|
|
json_type |
jtype, |
|
|
unsigned int |
flags |
|
) |
| |
Checks whether the JSON value matches the type and constraint.
- Parameters
-
val | The JSON value to be checked |
jtype | The JSON type, e.g., JSON_STRING |
flags | Indicates constraints for the described type |
- Returns
- TRUE if the value is valid
◆ janus_mkdir()
int janus_mkdir |
( |
const char * |
dir, |
|
|
mode_t |
mode |
|
) |
| |
Helper to create a new directory, and recursively create parent directories if needed.
- Parameters
-
dir | Path to the new folder to create |
mode | File permissions for the new directory file |
- Returns
- An integer like the regular mkdir does
- Note
- A failure may indicate that creating any of the subdirectories failed: some may still have been created
◆ janus_pidfile_create()
int janus_pidfile_create |
( |
const char * |
file | ) |
|
Create and lock a PID file.
- Parameters
-
file | Path to the PID file to use |
- Returns
- 0 if successful, a negative integer otherwise
◆ janus_pidfile_remove()
int janus_pidfile_remove |
( |
void |
| ) |
|
Unlock and remove a previously created PID file.
- Returns
- 0 if successful, a negative integer otherwise
◆ janus_random_uint32()
guint32 janus_random_uint32 |
( |
void |
| ) |
|
Helper to generate random 32-bit unsigned integers (useful for SSRCs, etc.)
- Note
- Currently just wraps g_random_int()
- Returns
- A random 32-bit unsigned integer
◆ janus_random_uint64()
guint64 janus_random_uint64 |
( |
void |
| ) |
|
Helper to generate random 64-bit unsigned integers (useful for Janus IDs)
- Returns
- A random 64-bit unsigned integer
◆ janus_strcmp_const_time()
gboolean janus_strcmp_const_time |
( |
const void * |
str1, |
|
|
const void * |
str2 |
|
) |
| |
Helper to compare strings in constant time.
- Parameters
-
str1 | The first string to compare |
str2 | The second string to compare |
- Returns
- true if the strings are the same, false otherwise
◆ janus_string_replace()
char* janus_string_replace |
( |
char * |
message, |
|
|
const char * |
old_string, |
|
|
const char * |
new_string |
|
) |
| |
Helper to replace strings.
- Parameters
-
message | The string that contains the text to replace, which may be freed if it is too short |
old_string | The old text to replace |
new_string | The new text |
- Returns
- A pointer to the updated text string (re-allocated or just updated)
◆ janus_uint64_dup()
guint64* janus_uint64_dup |
( |
guint64 |
num | ) |
|
Helper to generate an allocated copy of a guint64 number.
- Note
- While apparently silly, this is needed in order to make sure guint64 values used as keys in GHashTable operations are not lost: using temporary guint64 numbers in a g_hash_table_insert, for instance, will cause the key to contain garbage as soon as the temporary variable is lost, and all opererations on the key to fail
- Parameters
-
num | The guint64 number to duplicate |
- Returns
- A pointer to a guint64 number, if successful, NULL otherwise