Fork me on GitHub
utils.h
Go to the documentation of this file.
1 
12 #ifndef _JANUS_UTILS_H
13 #define _JANUS_UTILS_H
14 
15 #include <stdint.h>
16 #include <glib.h>
17 #include <netinet/in.h>
18 #include <jansson.h>
19 
20 /* Use JANUS_JSON_BOOL instead of the non-existing JSON_BOOLEAN */
21 #define JANUS_JSON_BOOL JSON_TRUE
22 #define JANUS_JSON_PARAM_REQUIRED 1
23 #define JANUS_JSON_PARAM_POSITIVE 2
24 #define JANUS_JSON_PARAM_NONEMPTY 4
25 
27  const gchar *name;
28  json_type jtype;
29  unsigned int flags;
30 };
31 
35 gint64 janus_get_monotonic_time(void);
36 
40 gint64 janus_get_real_time(void);
41 
48 char *janus_string_replace(char *message, const char *old_string, const char *new_string) G_GNUC_WARN_UNUSED_RESULT;
49 
53 gboolean janus_is_true(const char *value);
54 
59 gboolean janus_strcmp_const_time(const void *str1, const void *str2);
60 
64 guint32 janus_random_uint32(void);
65 
68 guint64 janus_random_uint64(void);
69 
77 guint64 *janus_uint64_dup(guint64 num);
78 
81 
83 typedef uint32_t janus_flags;
84 
88 
92 void janus_flags_set(janus_flags *flags, uint32_t flag);
93 
97 void janus_flags_clear(janus_flags *flags, uint32_t flag);
98 
103 gboolean janus_flags_is_set(janus_flags *flags, uint32_t flag);
105 
111 int janus_mkdir(const char *dir, mode_t mode);
112 
117 int janus_get_codec_pt(const char *sdp, const char *codec);
118 
123 const char *janus_get_codec_from_pt(const char *sdp, int pt);
124 
129 gboolean janus_is_ip_valid(const char *ip, int *family);
130 
135 char *janus_address_to_ip(struct sockaddr *address);
136 
140 uint16_t janus_address_to_port(struct sockaddr *address);
141 
145 int janus_pidfile_create(const char *file);
146 
149 int janus_pidfile_remove(void);
150 
156 void janus_get_json_type_name(int jtype, unsigned int flags, char *type_name);
157 
163 gboolean janus_json_is_valid(json_t *val, json_type jtype, unsigned int flags);
164 
175 #define JANUS_VALIDATE_JSON_OBJECT_FORMAT(missing_format, invalid_format, obj, params, error_code, error_cause, log_error, missing_code, invalid_code) \
176  do { \
177  error_code = 0; \
178  unsigned int i; \
179  for(i = 0; i < sizeof(params) / sizeof(struct janus_json_parameter); i++) { \
180  json_t *val = json_object_get(obj, params[i].name); \
181  if(!val) { \
182  if((params[i].flags & JANUS_JSON_PARAM_REQUIRED) != 0) { \
183  error_code = (missing_code); \
184  if(log_error) \
185  JANUS_LOG(LOG_ERR, missing_format "\n", params[i].name); \
186  if(error_cause != NULL) \
187  g_snprintf(error_cause, sizeof(error_cause), missing_format, params[i].name); \
188  break; \
189  } \
190  continue; \
191  } \
192  if(!janus_json_is_valid(val, params[i].jtype, params[i].flags)) { \
193  error_code = (invalid_code); \
194  char type_name[20]; \
195  janus_get_json_type_name(params[i].jtype, params[i].flags, type_name); \
196  if(log_error) \
197  JANUS_LOG(LOG_ERR, invalid_format "\n", params[i].name, type_name); \
198  if(error_cause != NULL) \
199  g_snprintf(error_cause, sizeof(error_cause), invalid_format, params[i].name, type_name); \
200  break; \
201  } \
202  } \
203  } while(0)
204 
213 #define JANUS_VALIDATE_JSON_OBJECT(obj, params, error_code, error_cause, log_error, missing_code, invalid_code) \
214  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)
215 
225 #define JANUS_CHECK_SECRET(secret, obj, member, error_code, error_cause, missing_code, invalid_code, unauthorized_code) \
226  do { \
227  if (secret) { \
228  static struct janus_json_parameter secret_parameters[] = { \
229  {member, JSON_STRING, JANUS_JSON_PARAM_REQUIRED} \
230  }; \
231  JANUS_VALIDATE_JSON_OBJECT(obj, secret_parameters, error_code, error_cause, TRUE, missing_code, invalid_code); \
232  if(error_code == 0 && !janus_strcmp_const_time((secret), json_string_value(json_object_get(obj, member)))) { \
233  error_code = (unauthorized_code); \
234  JANUS_LOG(LOG_ERR, "Unauthorized (wrong %s)\n", member); \
235  if(error_cause != NULL) \
236  g_snprintf(error_cause, sizeof(error_cause), "Unauthorized (wrong %s)", member); \
237  } \
238  } \
239  } while(0)
240 
241 #endif
unsigned int flags
Definition: utils.h:29
uint16_t janus_address_to_port(struct sockaddr *address)
Get the port from a sockaddr address.
Definition: utils.c:405
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...
Definition: utils.c:230
guint64 janus_random_uint64(void)
Helper to generate random 64-bit unsigned integers (useful for Janus IDs)
Definition: utils.c:76
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...
Definition: utils.c:314
int janus_pidfile_create(const char *file)
Create and lock a PID file.
Definition: utils.c:429
void janus_get_json_type_name(int jtype, unsigned int flags, char *type_name)
Creates a string describing the JSON type and constraint.
Definition: utils.c:484
int janus_mkdir(const char *dir, mode_t mode)
Helper to create a new directory, and recursively create parent directories if needed.
Definition: utils.c:203
void janus_flags_set(janus_flags *flags, uint32_t flag)
Janus flags set method.
Definition: utils.c:103
void janus_flags_reset(janus_flags *flags)
Janus flags reset method.
Definition: utils.c:98
const gchar * name
Definition: utils.h:27
gint64 janus_get_monotonic_time(void)
Helper to retrieve the system monotonic time, as Glib&#39;s g_get_monotonic_time may not be available (on...
Definition: utils.c:30
json_type jtype
Definition: utils.h:28
gint64 janus_get_real_time(void)
Helper to retrieve the system real time, as Glib&#39;s g_get_real_time may not be available (only since 2...
Definition: utils.c:36
Definition: utils.h:26
guint32 janus_random_uint32(void)
Helper to generate random 32-bit unsigned integers (useful for SSRCs, etc.)
Definition: utils.c:72
gboolean janus_flags_is_set(janus_flags *flags, uint32_t flag)
Janus flags check method.
Definition: utils.c:115
void janus_flags_clear(janus_flags *flags, uint32_t flag)
Janus flags clear method.
Definition: utils.c:109
uint32_t janus_flags
Janus flags container.
Definition: utils.h:83
char * janus_string_replace(char *message, const char *old_string, const char *new_string) G_GNUC_WARN_UNUSED_RESULT
Helper to replace strings.
Definition: utils.c:124
int janus_pidfile_remove(void)
Unlock and remove a previously created PID file.
Definition: utils.c:468
guint64 * janus_uint64_dup(guint64 num)
Helper to generate an allocated copy of a guint64 number.
Definition: utils.c:92
gboolean janus_is_ip_valid(const char *ip, int *family)
Check if the given IP address is valid: family is set to the address family if the IP is valid...
Definition: utils.c:361
gboolean janus_is_true(const char *value)
Helper to parse yes/no|true/false configuration values.
Definition: utils.c:42
gboolean janus_strcmp_const_time(const void *str1, const void *str2)
Helper to compare strings in constant time.
Definition: utils.c:46
char * janus_address_to_ip(struct sockaddr *address)
Convert a sockaddr address to an IP string.
Definition: utils.c:381
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:524