Asterisk - The Open Source Telephony Project  21.4.1
Macros | Functions | Variables
res_ari_model.c File Reference

Implementation Swagger validators. More...

#include "asterisk.h"
#include "ari/ari_model_validators.h"
#include "asterisk/logger.h"
#include "asterisk/module.h"
#include "asterisk/utils.h"
#include <regex.h>

Go to the source code of this file.

Macros

#define ISO8601_PATTERN   "^" REGEX_YMD "(T" REGEX_HMS REGEX_TZ ")?$"
 
#define REGEX_HMS   "[0-2][0-9]:[0-5][0-9](:[0-6][0-9](.[0-9]+)?)?"
 
#define REGEX_TZ   "(Z|[-+][0-2][0-9](:?[0-5][0-9])?)"
 
#define REGEX_YMD   "[0-9]{4}-[01][0-9]-[0-3][0-9]"
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
int ast_ari_validate_boolean (struct ast_json *json)
 Validator for native Swagger boolean. More...
 
int ast_ari_validate_byte (struct ast_json *json)
 Validator for native Swagger byte. More...
 
int ast_ari_validate_date (struct ast_json *json)
 Validator for native Swagger date. More...
 
int ast_ari_validate_double (struct ast_json *json)
 Validator for native Swagger double. More...
 
int ast_ari_validate_float (struct ast_json *json)
 Validator for native Swagger float. More...
 
int ast_ari_validate_int (struct ast_json *json)
 Validator for native Swagger int. More...
 
int ast_ari_validate_list (struct ast_json *json, int(*fn)(struct ast_json *))
 Validator for a Swagger List[]/JSON array. More...
 
int ast_ari_validate_long (struct ast_json *json)
 Validator for native Swagger long. More...
 
int ast_ari_validate_object (struct ast_json *json)
 Validator for native Swagger object. More...
 
int ast_ari_validate_string (struct ast_json *json)
 Validator for native Swagger string. More...
 
int ast_ari_validate_void (struct ast_json *json)
 Validator for native Swagger void. More...
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int check_range (intmax_t minval, intmax_t maxval, struct ast_json *json)
 
static int check_type (struct ast_json *json, enum ast_json_type expected)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER | AST_MODFLAG_GLOBAL_SYMBOLS , .description = "ARI Model validators" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "da6642af068ee5e6490c5b1d2cc1d238" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_APP_DEPEND, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static regex_t date_regex
 

Detailed Description

Implementation Swagger validators.

Author
David M. Lee, II dlee@.nosp@m.digi.nosp@m.um.co.nosp@m.m

Definition in file res_ari_model.c.

Function Documentation

int ast_ari_validate_boolean ( struct ast_json json)

Validator for native Swagger boolean.

Parameters
jsonJSON object to validate.
Return values
True(non-zero) if valid.
False(zero) if invalid.

Definition at line 107 of file res_ari_model.c.

References ast_json_typename(), and ast_json_typeof().

Referenced by ast_ari_validate_bridge_attended_transfer(), ast_ari_validate_bridge_blind_transfer(), and ast_ari_validate_channel_hangup_request().

108 {
109  enum ast_json_type actual = ast_json_typeof(json);
110  switch (actual) {
111  case AST_JSON_TRUE:
112  case AST_JSON_FALSE:
113  return 1;
114  default:
115  ast_log(LOG_ERROR, "Expected type boolean, was %s\n",
116  ast_json_typename(actual));
117  return 0;
118  }
119 }
ast_json_type
Valid types of a JSON element.
Definition: json.h:162
enum ast_json_type ast_json_typeof(const struct ast_json *value)
Get the type of value.
Definition: json.c:78
const char * ast_json_typename(enum ast_json_type type)
Get the string name for the given type.
Definition: json.c:95
int ast_ari_validate_byte ( struct ast_json json)

Validator for native Swagger byte.

Parameters
jsonJSON object to validate.
Return values
True(non-zero) if valid.
False(zero) if invalid.

Definition at line 101 of file res_ari_model.c.

102 {
103  /* Java bytes are signed, which accounts for great fun for all */
104  return check_range(-128, 255, json);
105 }
int ast_ari_validate_date ( struct ast_json json)

Validator for native Swagger date.

Parameters
jsonJSON object to validate.
Return values
True(non-zero) if valid.
False(zero) if invalid.

Definition at line 148 of file res_ari_model.c.

References ast_json_string_get().

Referenced by ast_ari_validate_application_move_failed(), ast_ari_validate_application_replaced(), ast_ari_validate_bridge(), ast_ari_validate_bridge_attended_transfer(), ast_ari_validate_bridge_blind_transfer(), ast_ari_validate_bridge_created(), ast_ari_validate_bridge_destroyed(), ast_ari_validate_bridge_merged(), ast_ari_validate_bridge_video_source_changed(), ast_ari_validate_channel(), ast_ari_validate_channel_caller_id(), ast_ari_validate_channel_connected_line(), ast_ari_validate_channel_created(), ast_ari_validate_channel_destroyed(), ast_ari_validate_channel_dialplan(), ast_ari_validate_channel_dtmf_received(), ast_ari_validate_channel_entered_bridge(), ast_ari_validate_channel_hangup_request(), ast_ari_validate_channel_hold(), ast_ari_validate_channel_left_bridge(), ast_ari_validate_channel_state_change(), ast_ari_validate_channel_talking_finished(), ast_ari_validate_channel_talking_started(), ast_ari_validate_channel_unhold(), ast_ari_validate_channel_userevent(), ast_ari_validate_channel_varset(), ast_ari_validate_contact_status_change(), ast_ari_validate_device_state_changed(), ast_ari_validate_dial(), ast_ari_validate_endpoint_state_change(), ast_ari_validate_event(), ast_ari_validate_peer_status_change(), ast_ari_validate_playback_continuing(), ast_ari_validate_playback_finished(), ast_ari_validate_playback_started(), ast_ari_validate_recording_failed(), ast_ari_validate_recording_finished(), ast_ari_validate_recording_started(), ast_ari_validate_stasis_end(), ast_ari_validate_stasis_start(), ast_ari_validate_status_info(), and ast_ari_validate_text_message_received().

149 {
150  /* Dates are ISO-8601 strings */
151  const char *str;
152  if (!check_type(json, AST_JSON_STRING)) {
153  return 0;
154  }
155  str = ast_json_string_get(json);
156  ast_assert(str != NULL);
157  if (regexec(&date_regex, str, 0, NULL, 0) != 0) {
158  ast_log(LOG_ERROR, "Date field is malformed: '%s'\n", str);
159  return 0;
160  }
161  return 1;
162 }
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:283
int ast_ari_validate_double ( struct ast_json json)

Validator for native Swagger double.

Parameters
jsonJSON object to validate.
Return values
True(non-zero) if valid.
False(zero) if invalid.

Definition at line 138 of file res_ari_model.c.

Referenced by ast_ari_validate_config_info(), and ast_ari_validate_rtpstat().

139 {
140  return check_type(json, AST_JSON_REAL);
141 }
int ast_ari_validate_float ( struct ast_json json)

Validator for native Swagger float.

Parameters
jsonJSON object to validate.
Return values
True(non-zero) if valid.
False(zero) if invalid.

Definition at line 133 of file res_ari_model.c.

134 {
135  return check_type(json, AST_JSON_REAL);
136 }
int ast_ari_validate_int ( struct ast_json json)

Validator for native Swagger int.

Parameters
jsonJSON object to validate.
Return values
True(non-zero) if valid.
False(zero) if invalid.

Definition at line 121 of file res_ari_model.c.

Referenced by ast_ari_validate_channel_caller_id(), ast_ari_validate_channel_destroyed(), ast_ari_validate_channel_dtmf_received(), ast_ari_validate_channel_hangup_request(), ast_ari_validate_channel_talking_finished(), ast_ari_validate_config_info(), ast_ari_validate_live_recording(), ast_ari_validate_mailbox(), ast_ari_validate_module(), and ast_ari_validate_rtpstat().

122 {
123  /* Swagger int's are 32-bit */
124  return check_range(-2147483648LL, 2147483647LL, json);
125 }
int ast_ari_validate_list ( struct ast_json json,
int(*)(struct ast_json *)  fn 
)

Validator for a Swagger List[]/JSON array.

Parameters
jsonJSON object to validate.
fnValidator to call on every element in the array.
Return values
True(non-zero) if valid.
False(zero) if invalid.

Definition at line 164 of file res_ari_model.c.

References ast_json_array_get(), and ast_json_array_size().

Referenced by ast_ari_applications_list_cb(), ast_ari_asterisk_get_object_cb(), ast_ari_asterisk_list_log_channels_cb(), ast_ari_asterisk_list_modules_cb(), ast_ari_asterisk_update_object_cb(), ast_ari_bridges_list_cb(), ast_ari_channels_list_cb(), ast_ari_device_states_list_cb(), ast_ari_endpoints_list_by_tech_cb(), ast_ari_endpoints_list_cb(), ast_ari_mailboxes_list_cb(), ast_ari_recordings_list_stored_cb(), ast_ari_sounds_list_cb(), ast_ari_validate_application(), ast_ari_validate_application_move_failed(), ast_ari_validate_bridge(), ast_ari_validate_endpoint(), ast_ari_validate_missing_params(), ast_ari_validate_sound(), and ast_ari_validate_stasis_start().

165 {
166  int res = 1;
167  size_t i;
168 
169  if (!check_type(json, AST_JSON_ARRAY)) {
170  return 0;
171  }
172 
173  for (i = 0; i < ast_json_array_size(json); ++i) {
174  int member_res;
175  member_res = fn(ast_json_array_get(json, i));
176  if (!member_res) {
177  ast_log(LOG_ERROR,
178  "Array member %zu failed validation\n", i);
179  res = 0;
180  }
181  }
182 
183  return res;
184 }
size_t ast_json_array_size(const struct ast_json *array)
Get the size of a JSON array.
Definition: json.c:366
struct ast_json * ast_json_array_get(const struct ast_json *array, size_t index)
Get an element from an array.
Definition: json.c:370
int ast_ari_validate_long ( struct ast_json json)

Validator for native Swagger long.

Parameters
jsonJSON object to validate.
Return values
True(non-zero) if valid.
False(zero) if invalid.

Definition at line 127 of file res_ari_model.c.

Referenced by ast_ari_validate_dialplan_cep().

128 {
129  /* All integral values are valid longs. No need for range check. */
130  return check_type(json, AST_JSON_INTEGER);
131 }
int ast_ari_validate_object ( struct ast_json json)

Validator for native Swagger object.

Parameters
jsonJSON object to validate.
Return values
True(non-zero) if valid.
False(zero) if invalid.

Definition at line 96 of file res_ari_model.c.

Referenced by ast_ari_validate_application(), ast_ari_validate_channel(), ast_ari_validate_channel_userevent(), and ast_ari_validate_text_message().

97 {
98  return check_type(json, AST_JSON_OBJECT);
99 }
int ast_ari_validate_string ( struct ast_json json)

Validator for native Swagger string.

Parameters
jsonJSON object to validate.
Return values
True(non-zero) if valid.
False(zero) if invalid.

Definition at line 143 of file res_ari_model.c.

Referenced by ast_ari_validate_application(), ast_ari_validate_application_move_failed(), ast_ari_validate_application_replaced(), ast_ari_validate_asterisk_ping(), ast_ari_validate_bridge(), ast_ari_validate_bridge_attended_transfer(), ast_ari_validate_bridge_blind_transfer(), ast_ari_validate_bridge_created(), ast_ari_validate_bridge_destroyed(), ast_ari_validate_bridge_merged(), ast_ari_validate_bridge_video_source_changed(), ast_ari_validate_build_info(), ast_ari_validate_caller_id(), ast_ari_validate_channel(), ast_ari_validate_channel_caller_id(), ast_ari_validate_channel_connected_line(), ast_ari_validate_channel_created(), ast_ari_validate_channel_destroyed(), ast_ari_validate_channel_dialplan(), ast_ari_validate_channel_dtmf_received(), ast_ari_validate_channel_entered_bridge(), ast_ari_validate_channel_hangup_request(), ast_ari_validate_channel_hold(), ast_ari_validate_channel_left_bridge(), ast_ari_validate_channel_state_change(), ast_ari_validate_channel_talking_finished(), ast_ari_validate_channel_talking_started(), ast_ari_validate_channel_unhold(), ast_ari_validate_channel_userevent(), ast_ari_validate_channel_varset(), ast_ari_validate_config_info(), ast_ari_validate_config_tuple(), ast_ari_validate_contact_info(), ast_ari_validate_contact_status_change(), ast_ari_validate_device_state(), ast_ari_validate_device_state_changed(), ast_ari_validate_dial(), ast_ari_validate_dialplan_cep(), ast_ari_validate_endpoint(), ast_ari_validate_endpoint_state_change(), ast_ari_validate_event(), ast_ari_validate_format_lang_pair(), ast_ari_validate_live_recording(), ast_ari_validate_log_channel(), ast_ari_validate_mailbox(), ast_ari_validate_message(), ast_ari_validate_missing_params(), ast_ari_validate_module(), ast_ari_validate_peer(), ast_ari_validate_peer_status_change(), ast_ari_validate_playback(), ast_ari_validate_playback_continuing(), ast_ari_validate_playback_finished(), ast_ari_validate_playback_started(), ast_ari_validate_recording_failed(), ast_ari_validate_recording_finished(), ast_ari_validate_recording_started(), ast_ari_validate_rtpstat(), ast_ari_validate_set_id(), ast_ari_validate_sound(), ast_ari_validate_stasis_end(), ast_ari_validate_stasis_start(), ast_ari_validate_stored_recording(), ast_ari_validate_system_info(), ast_ari_validate_text_message(), ast_ari_validate_text_message_received(), and ast_ari_validate_variable().

144 {
145  return check_type(json, AST_JSON_STRING);
146 }
int ast_ari_validate_void ( struct ast_json json)

Validator for native Swagger void.

Parameters
jsonJSON object to validate.
Return values
True(non-zero) if valid.
False(zero) if invalid.

Definition at line 91 of file res_ari_model.c.

Referenced by ast_ari_asterisk_add_log_cb(), ast_ari_asterisk_delete_log_cb(), ast_ari_asterisk_delete_object_cb(), ast_ari_asterisk_load_module_cb(), ast_ari_asterisk_reload_module_cb(), ast_ari_asterisk_rotate_log_cb(), ast_ari_asterisk_set_global_var_cb(), ast_ari_asterisk_unload_module_cb(), ast_ari_bridges_add_channel_cb(), ast_ari_bridges_clear_video_source_cb(), ast_ari_bridges_destroy_cb(), ast_ari_bridges_remove_channel_cb(), ast_ari_bridges_set_video_source_cb(), ast_ari_bridges_start_moh_cb(), ast_ari_bridges_stop_moh_cb(), ast_ari_channels_answer_cb(), ast_ari_channels_continue_in_dialplan_cb(), ast_ari_channels_dial_cb(), ast_ari_channels_hangup_cb(), ast_ari_channels_hold_cb(), ast_ari_channels_move_cb(), ast_ari_channels_mute_cb(), ast_ari_channels_redirect_cb(), ast_ari_channels_ring_cb(), ast_ari_channels_ring_stop_cb(), ast_ari_channels_send_dtmf_cb(), ast_ari_channels_set_channel_var_cb(), ast_ari_channels_start_moh_cb(), ast_ari_channels_start_silence_cb(), ast_ari_channels_stop_moh_cb(), ast_ari_channels_stop_silence_cb(), ast_ari_channels_unhold_cb(), ast_ari_channels_unmute_cb(), ast_ari_device_states_delete_cb(), ast_ari_device_states_update_cb(), ast_ari_endpoints_refer_cb(), ast_ari_endpoints_refer_to_endpoint_cb(), ast_ari_endpoints_send_message_cb(), ast_ari_endpoints_send_message_to_endpoint_cb(), ast_ari_events_user_event_cb(), ast_ari_mailboxes_delete_cb(), ast_ari_mailboxes_update_cb(), ast_ari_playbacks_control_cb(), ast_ari_playbacks_stop_cb(), ast_ari_recordings_cancel_cb(), ast_ari_recordings_delete_stored_cb(), ast_ari_recordings_mute_cb(), ast_ari_recordings_pause_cb(), ast_ari_recordings_stop_cb(), ast_ari_recordings_unmute_cb(), and ast_ari_recordings_unpause_cb().

92 {
93  return check_type(json, AST_JSON_NULL);
94 }