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 <jansson.h>
18 
19 /* Use JANUS_JSON_BOOL instead of the non-existing JSON_BOOLEAN */
20 #define JANUS_JSON_BOOL JSON_TRUE
21 #define JANUS_JSON_PARAM_REQUIRED 1
22 #define JANUS_JSON_PARAM_POSITIVE 2
23 #define JANUS_JSON_PARAM_NONEMPTY 4
24 
26  const gchar *name;
27  json_type jtype;
28  unsigned int flags;
29 };
30 
34 gint64 janus_get_monotonic_time(void);
35 
39 gint64 janus_get_real_time(void);
40 
47 char *janus_string_replace(char *message, const char *old_string, const char *new_string) G_GNUC_WARN_UNUSED_RESULT;
48 
52 gboolean janus_is_true(const char *value);
53 
58 gboolean janus_strcmp_const_time(const void *str1, const void *str2);
59 
63 guint32 janus_random_uint32(void);
64 
67 guint64 janus_random_uint64(void);
68 
76 guint64 *janus_uint64_dup(guint64 num);
77 
80 
82 typedef uint32_t janus_flags;
83 
87 
91 void janus_flags_set(janus_flags *flags, uint32_t flag);
92 
96 void janus_flags_clear(janus_flags *flags, uint32_t flag);
97 
102 gboolean janus_flags_is_set(janus_flags *flags, uint32_t flag);
104 
110 int janus_mkdir(const char *dir, mode_t mode);
111 
116 int janus_get_codec_pt(const char *sdp, const char *codec);
117 
122 const char *janus_get_codec_from_pt(const char *sdp, int pt);
123 
127 int janus_pidfile_create(const char *file);
128 
131 int janus_pidfile_remove(void);
132 
138 void janus_get_json_type_name(int jtype, unsigned int flags, char *type_name);
139 
145 gboolean janus_json_is_valid(json_t *val, json_type jtype, unsigned int flags);
146 
157 #define JANUS_VALIDATE_JSON_OBJECT_FORMAT(missing_format, invalid_format, obj, params, error_code, error_cause, log_error, missing_code, invalid_code) \
158  do { \
159  error_code = 0; \
160  unsigned int i; \
161  for(i = 0; i < sizeof(params) / sizeof(struct janus_json_parameter); i++) { \
162  json_t *val = json_object_get(obj, params[i].name); \
163  if(!val) { \
164  if((params[i].flags & JANUS_JSON_PARAM_REQUIRED) != 0) { \
165  error_code = (missing_code); \
166  if(log_error) \
167  JANUS_LOG(LOG_ERR, missing_format "\n", params[i].name); \
168  if(error_cause != NULL) \
169  g_snprintf(error_cause, sizeof(error_cause), missing_format, params[i].name); \
170  break; \
171  } \
172  continue; \
173  } \
174  if(!janus_json_is_valid(val, params[i].jtype, params[i].flags)) { \
175  error_code = (invalid_code); \
176  char type_name[20]; \
177  janus_get_json_type_name(params[i].jtype, params[i].flags, type_name); \
178  if(log_error) \
179  JANUS_LOG(LOG_ERR, invalid_format "\n", params[i].name, type_name); \
180  if(error_cause != NULL) \
181  g_snprintf(error_cause, sizeof(error_cause), invalid_format, params[i].name, type_name); \
182  break; \
183  } \
184  } \
185  } while(0)
186 
195 #define JANUS_VALIDATE_JSON_OBJECT(obj, params, error_code, error_cause, log_error, missing_code, invalid_code) \
196  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)
197 
207 #define JANUS_CHECK_SECRET(secret, obj, member, error_code, error_cause, missing_code, invalid_code, unauthorized_code) \
208  do { \
209  if (secret) { \
210  static struct janus_json_parameter secret_parameters[] = { \
211  {member, JSON_STRING, JANUS_JSON_PARAM_REQUIRED} \
212  }; \
213  JANUS_VALIDATE_JSON_OBJECT(obj, secret_parameters, error_code, error_cause, TRUE, missing_code, invalid_code); \
214  if(error_code == 0 && !janus_strcmp_const_time((secret), json_string_value(json_object_get(obj, member)))) { \
215  error_code = (unauthorized_code); \
216  JANUS_LOG(LOG_ERR, "Unauthorized (wrong %s)\n", member); \
217  if(error_cause != NULL) \
218  g_snprintf(error_cause, sizeof(error_cause), "Unauthorized (wrong %s)", member); \
219  } \
220  } \
221  } while(0)
222 
223 #endif
unsigned int flags
Definition: utils.h:28
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:228
guint64 janus_random_uint64(void)
Helper to generate random 64-bit unsigned integers (useful for Janus IDs)
Definition: utils.c:74
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:317
int janus_pidfile_create(const char *file)
Create and lock a PID file.
Definition: utils.c:372
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:427
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:201
void janus_flags_set(janus_flags *flags, uint32_t flag)
Janus flags set method.
Definition: utils.c:101
void janus_flags_reset(janus_flags *flags)
Janus flags reset method.
Definition: utils.c:96
const gchar * name
Definition: utils.h:26
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:28
json_type jtype
Definition: utils.h:27
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:34
Definition: utils.h:25
guint32 janus_random_uint32(void)
Helper to generate random 32-bit unsigned integers (useful for SSRCs, etc.)
Definition: utils.c:70
gboolean janus_flags_is_set(janus_flags *flags, uint32_t flag)
Janus flags check method.
Definition: utils.c:113
void janus_flags_clear(janus_flags *flags, uint32_t flag)
Janus flags clear method.
Definition: utils.c:107
uint32_t janus_flags
Janus flags container.
Definition: utils.h:82
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:122
int janus_pidfile_remove(void)
Unlock and remove a previously created PID file.
Definition: utils.c:411
guint64 * janus_uint64_dup(guint64 num)
Helper to generate an allocated copy of a guint64 number.
Definition: utils.c:90
gboolean janus_is_true(const char *value)
Helper to parse yes/no|true/false configuration values.
Definition: utils.c:40
gboolean janus_strcmp_const_time(const void *str1, const void *str2)
Helper to compare strings in constant time.
Definition: utils.c:44
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