Asterisk - The Open Source Telephony Project  GIT-master-a24979a
Macros | Functions | Variables
res_ari_endpoints.c File Reference

Endpoint resources. More...

#include "asterisk.h"
#include "asterisk/app.h"
#include "asterisk/module.h"
#include "asterisk/stasis_app.h"
#include "ari/resource_endpoints.h"
Include dependency graph for res_ari_endpoints.c:

Go to the source code of this file.

Macros

#define MAX_VALS   128
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static void ast_ari_endpoints_get_cb (struct ast_tcptls_session_instance *ser, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
 Parameter parsing callback for /endpoints/{tech}/{resource}. More...
 
static void ast_ari_endpoints_list_by_tech_cb (struct ast_tcptls_session_instance *ser, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
 Parameter parsing callback for /endpoints/{tech}. More...
 
static void ast_ari_endpoints_list_cb (struct ast_tcptls_session_instance *ser, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
 Parameter parsing callback for /endpoints. More...
 
static void ast_ari_endpoints_send_message_cb (struct ast_tcptls_session_instance *ser, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
 Parameter parsing callback for /endpoints/sendMessage. More...
 
int ast_ari_endpoints_send_message_parse_body (struct ast_json *body, struct ast_ari_endpoints_send_message_args *args)
 Body parsing function for /endpoints/sendMessage. More...
 
static void ast_ari_endpoints_send_message_to_endpoint_cb (struct ast_tcptls_session_instance *ser, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
 Parameter parsing callback for /endpoints/{tech}/{resource}/sendMessage. More...
 
int ast_ari_endpoints_send_message_to_endpoint_parse_body (struct ast_json *body, struct ast_ari_endpoints_send_message_to_endpoint_args *args)
 Body parsing function for /endpoints/{tech}/{resource}/sendMessage. More...
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "RESTful API module - Endpoint resources" , .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 = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_ari,res_ari_model,res_stasis", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct stasis_rest_handlers endpoints
 REST handler for /api-docs/endpoints.json. More...
 
static struct stasis_rest_handlers endpoints_sendMessage
 REST handler for /api-docs/endpoints.json. More...
 
static struct stasis_rest_handlers endpoints_tech
 REST handler for /api-docs/endpoints.json. More...
 
static struct stasis_rest_handlers endpoints_tech_resource
 REST handler for /api-docs/endpoints.json. More...
 
static struct stasis_rest_handlers endpoints_tech_resource_sendMessage
 REST handler for /api-docs/endpoints.json. More...
 

Detailed Description

Endpoint resources.

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

Definition in file res_ari_endpoints.c.

Macro Definition Documentation

◆ MAX_VALS

#define MAX_VALS   128

Definition at line 51 of file res_ari_endpoints.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 483 of file res_ari_endpoints.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 483 of file res_ari_endpoints.c.

◆ ast_ari_endpoints_get_cb()

static void ast_ari_endpoints_get_cb ( struct ast_tcptls_session_instance ser,
struct ast_variable get_params,
struct ast_variable path_vars,
struct ast_variable headers,
struct ast_json body,
struct ast_ari_response response 
)
static

Parameter parsing callback for /endpoints/{tech}/{resource}.

Parameters
serTCP/TLS session object
get_paramsGET parameters in the HTTP request.
path_varsPath variables extracted from the request.
headersHTTP headers.
body
[out]responseResponse to the HTTP request.

Definition at line 263 of file res_ari_endpoints.c.

267 {
268  struct ast_ari_endpoints_get_args args = {};
269  struct ast_variable *i;
270 #if defined(AST_DEVMODE)
271  int is_valid;
272  int code;
273 #endif /* AST_DEVMODE */
274 
275  for (i = path_vars; i; i = i->next) {
276  if (strcmp(i->name, "tech") == 0) {
277  args.tech = (i->value);
278  } else
279  if (strcmp(i->name, "resource") == 0) {
280  args.resource = (i->value);
281  } else
282  {}
283  }
284  ast_ari_endpoints_get(headers, &args, response);
285 #if defined(AST_DEVMODE)
286  code = response->response_code;
287 
288  switch (code) {
289  case 0: /* Implementation is still a stub, or the code wasn't set */
290  is_valid = response->message == NULL;
291  break;
292  case 500: /* Internal Server Error */
293  case 501: /* Not Implemented */
294  case 400: /* Invalid parameters for sending a message. */
295  case 404: /* Endpoints not found */
296  is_valid = 1;
297  break;
298  default:
299  if (200 <= code && code <= 299) {
300  is_valid = ast_ari_validate_endpoint(
301  response->message);
302  } else {
303  ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/{tech}/{resource}\n", code);
304  is_valid = 0;
305  }
306  }
307 
308  if (!is_valid) {
309  ast_log(LOG_ERROR, "Response validation failed for /endpoints/{tech}/{resource}\n");
310  ast_ari_response_error(response, 500,
311  "Internal Server Error", "Response validation failed");
312  }
313 #endif /* AST_DEVMODE */
314 
315 fin: __attribute__((unused))
316  return;
317 }
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:259
int ast_ari_validate_endpoint(struct ast_json *json)
Validator for Endpoint.
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
#define NULL
Definition: resample.c:96
void ast_ari_endpoints_get(struct ast_variable *headers, struct ast_ari_endpoints_get_args *args, struct ast_ari_response *response)
Details for an endpoint.
struct ast_json * message
Definition: ari.h:94
int response_code
Definition: ari.h:99
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
const char * args

References args, ast_ari_endpoints_get(), ast_ari_response_error(), ast_ari_validate_endpoint(), ast_log, LOG_ERROR, ast_ari_response::message, ast_variable::name, ast_variable::next, NULL, ast_ari_response::response_code, and ast_variable::value.

◆ ast_ari_endpoints_list_by_tech_cb()

static void ast_ari_endpoints_list_by_tech_cb ( struct ast_tcptls_session_instance ser,
struct ast_variable get_params,
struct ast_variable path_vars,
struct ast_variable headers,
struct ast_json body,
struct ast_ari_response response 
)
static

Parameter parsing callback for /endpoints/{tech}.

Parameters
serTCP/TLS session object
get_paramsGET parameters in the HTTP request.
path_varsPath variables extracted from the request.
headersHTTP headers.
body
[out]responseResponse to the HTTP request.

Definition at line 203 of file res_ari_endpoints.c.

207 {
209  struct ast_variable *i;
210 #if defined(AST_DEVMODE)
211  int is_valid;
212  int code;
213 #endif /* AST_DEVMODE */
214 
215  for (i = path_vars; i; i = i->next) {
216  if (strcmp(i->name, "tech") == 0) {
217  args.tech = (i->value);
218  } else
219  {}
220  }
221  ast_ari_endpoints_list_by_tech(headers, &args, response);
222 #if defined(AST_DEVMODE)
223  code = response->response_code;
224 
225  switch (code) {
226  case 0: /* Implementation is still a stub, or the code wasn't set */
227  is_valid = response->message == NULL;
228  break;
229  case 500: /* Internal Server Error */
230  case 501: /* Not Implemented */
231  case 404: /* Endpoints not found */
232  is_valid = 1;
233  break;
234  default:
235  if (200 <= code && code <= 299) {
236  is_valid = ast_ari_validate_list(response->message,
238  } else {
239  ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/{tech}\n", code);
240  is_valid = 0;
241  }
242  }
243 
244  if (!is_valid) {
245  ast_log(LOG_ERROR, "Response validation failed for /endpoints/{tech}\n");
246  ast_ari_response_error(response, 500,
247  "Internal Server Error", "Response validation failed");
248  }
249 #endif /* AST_DEVMODE */
250 
251 fin: __attribute__((unused))
252  return;
253 }
ari_validator ast_ari_validate_endpoint_fn(void)
Function pointer to ast_ari_validate_endpoint().
int ast_ari_validate_list(struct ast_json *json, int(*fn)(struct ast_json *))
Validator for a Swagger List[]/JSON array.
void ast_ari_endpoints_list_by_tech(struct ast_variable *headers, struct ast_ari_endpoints_list_by_tech_args *args, struct ast_ari_response *response)
List available endoints for a given endpoint technology.

References args, ast_ari_endpoints_list_by_tech(), ast_ari_response_error(), ast_ari_validate_endpoint_fn(), ast_ari_validate_list(), ast_log, LOG_ERROR, ast_ari_response::message, ast_variable::name, ast_variable::next, NULL, ast_ari_response::response_code, and ast_variable::value.

◆ ast_ari_endpoints_list_cb()

static void ast_ari_endpoints_list_cb ( struct ast_tcptls_session_instance ser,
struct ast_variable get_params,
struct ast_variable path_vars,
struct ast_variable headers,
struct ast_json body,
struct ast_ari_response response 
)
static

Parameter parsing callback for /endpoints.

Parameters
serTCP/TLS session object
get_paramsGET parameters in the HTTP request.
path_varsPath variables extracted from the request.
headersHTTP headers.
body
[out]responseResponse to the HTTP request.

Definition at line 62 of file res_ari_endpoints.c.

66 {
68 #if defined(AST_DEVMODE)
69  int is_valid;
70  int code;
71 #endif /* AST_DEVMODE */
72 
73  ast_ari_endpoints_list(headers, &args, response);
74 #if defined(AST_DEVMODE)
75  code = response->response_code;
76 
77  switch (code) {
78  case 0: /* Implementation is still a stub, or the code wasn't set */
79  is_valid = response->message == NULL;
80  break;
81  case 500: /* Internal Server Error */
82  case 501: /* Not Implemented */
83  is_valid = 1;
84  break;
85  default:
86  if (200 <= code && code <= 299) {
87  is_valid = ast_ari_validate_list(response->message,
89  } else {
90  ast_log(LOG_ERROR, "Invalid error response %d for /endpoints\n", code);
91  is_valid = 0;
92  }
93  }
94 
95  if (!is_valid) {
96  ast_log(LOG_ERROR, "Response validation failed for /endpoints\n");
97  ast_ari_response_error(response, 500,
98  "Internal Server Error", "Response validation failed");
99  }
100 #endif /* AST_DEVMODE */
101 
102 fin: __attribute__((unused))
103  return;
104 }
void ast_ari_endpoints_list(struct ast_variable *headers, struct ast_ari_endpoints_list_args *args, struct ast_ari_response *response)
List all endpoints.

References args, ast_ari_endpoints_list(), ast_ari_response_error(), ast_ari_validate_endpoint_fn(), ast_ari_validate_list(), ast_log, LOG_ERROR, ast_ari_response::message, NULL, and ast_ari_response::response_code.

◆ ast_ari_endpoints_send_message_cb()

static void ast_ari_endpoints_send_message_cb ( struct ast_tcptls_session_instance ser,
struct ast_variable get_params,
struct ast_variable path_vars,
struct ast_variable headers,
struct ast_json body,
struct ast_ari_response response 
)
static

Parameter parsing callback for /endpoints/sendMessage.

Parameters
serTCP/TLS session object
get_paramsGET parameters in the HTTP request.
path_varsPath variables extracted from the request.
headersHTTP headers.
body
[out]responseResponse to the HTTP request.

Definition at line 135 of file res_ari_endpoints.c.

139 {
141  struct ast_variable *i;
142 #if defined(AST_DEVMODE)
143  int is_valid;
144  int code;
145 #endif /* AST_DEVMODE */
146 
147  for (i = get_params; i; i = i->next) {
148  if (strcmp(i->name, "to") == 0) {
149  args.to = (i->value);
150  } else
151  if (strcmp(i->name, "from") == 0) {
152  args.from = (i->value);
153  } else
154  if (strcmp(i->name, "body") == 0) {
155  args.body = (i->value);
156  } else
157  {}
158  }
159  args.variables = body;
160  ast_ari_endpoints_send_message(headers, &args, response);
161 #if defined(AST_DEVMODE)
162  code = response->response_code;
163 
164  switch (code) {
165  case 0: /* Implementation is still a stub, or the code wasn't set */
166  is_valid = response->message == NULL;
167  break;
168  case 500: /* Internal Server Error */
169  case 501: /* Not Implemented */
170  case 400: /* Invalid parameters for sending a message. */
171  case 404: /* Endpoint not found */
172  is_valid = 1;
173  break;
174  default:
175  if (200 <= code && code <= 299) {
176  is_valid = ast_ari_validate_void(
177  response->message);
178  } else {
179  ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/sendMessage\n", code);
180  is_valid = 0;
181  }
182  }
183 
184  if (!is_valid) {
185  ast_log(LOG_ERROR, "Response validation failed for /endpoints/sendMessage\n");
186  ast_ari_response_error(response, 500,
187  "Internal Server Error", "Response validation failed");
188  }
189 #endif /* AST_DEVMODE */
190 
191 fin: __attribute__((unused))
192  return;
193 }
int ast_ari_validate_void(struct ast_json *json)
Validator for native Swagger void.
Definition: res_ari_model.c:91
void ast_ari_endpoints_send_message(struct ast_variable *headers, struct ast_ari_endpoints_send_message_args *args, struct ast_ari_response *response)
Send a message to some technology URI or endpoint.

References args, ast_ari_endpoints_send_message(), ast_ari_response_error(), ast_ari_validate_void(), ast_log, LOG_ERROR, ast_ari_response::message, ast_variable::name, ast_variable::next, NULL, ast_ari_response::response_code, and ast_variable::value.

◆ ast_ari_endpoints_send_message_parse_body()

int ast_ari_endpoints_send_message_parse_body ( struct ast_json body,
struct ast_ari_endpoints_send_message_args args 
)

Body parsing function for /endpoints/sendMessage.

Parameters
bodyThe JSON body from which to parse parameters.
[out]argsThe args structure to parse into.
Return values
zeroon success
non-zeroon failure

Definition at line 105 of file res_ari_endpoints.c.

108 {
109  struct ast_json *field;
110  /* Parse query parameters out of it */
111  field = ast_json_object_get(body, "to");
112  if (field) {
113  args->to = ast_json_string_get(field);
114  }
115  field = ast_json_object_get(body, "from");
116  if (field) {
117  args->from = ast_json_string_get(field);
118  }
119  field = ast_json_object_get(body, "body");
120  if (field) {
121  args->body = ast_json_string_get(field);
122  }
123  return 0;
124 }
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:397
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:273
Abstract JSON element (object, array, string, int, ...).

References args, ast_json_object_get(), and ast_json_string_get().

Referenced by ast_ari_endpoints_send_message().

◆ ast_ari_endpoints_send_message_to_endpoint_cb()

static void ast_ari_endpoints_send_message_to_endpoint_cb ( struct ast_tcptls_session_instance ser,
struct ast_variable get_params,
struct ast_variable path_vars,
struct ast_variable headers,
struct ast_json body,
struct ast_ari_response response 
)
static

Parameter parsing callback for /endpoints/{tech}/{resource}/sendMessage.

Parameters
serTCP/TLS session object
get_paramsGET parameters in the HTTP request.
path_varsPath variables extracted from the request.
headersHTTP headers.
body
[out]responseResponse to the HTTP request.

Definition at line 344 of file res_ari_endpoints.c.

348 {
350  struct ast_variable *i;
351 #if defined(AST_DEVMODE)
352  int is_valid;
353  int code;
354 #endif /* AST_DEVMODE */
355 
356  for (i = get_params; i; i = i->next) {
357  if (strcmp(i->name, "from") == 0) {
358  args.from = (i->value);
359  } else
360  if (strcmp(i->name, "body") == 0) {
361  args.body = (i->value);
362  } else
363  {}
364  }
365  for (i = path_vars; i; i = i->next) {
366  if (strcmp(i->name, "tech") == 0) {
367  args.tech = (i->value);
368  } else
369  if (strcmp(i->name, "resource") == 0) {
370  args.resource = (i->value);
371  } else
372  {}
373  }
374  args.variables = body;
376 #if defined(AST_DEVMODE)
377  code = response->response_code;
378 
379  switch (code) {
380  case 0: /* Implementation is still a stub, or the code wasn't set */
381  is_valid = response->message == NULL;
382  break;
383  case 500: /* Internal Server Error */
384  case 501: /* Not Implemented */
385  case 400: /* Invalid parameters for sending a message. */
386  case 404: /* Endpoint not found */
387  is_valid = 1;
388  break;
389  default:
390  if (200 <= code && code <= 299) {
391  is_valid = ast_ari_validate_void(
392  response->message);
393  } else {
394  ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/{tech}/{resource}/sendMessage\n", code);
395  is_valid = 0;
396  }
397  }
398 
399  if (!is_valid) {
400  ast_log(LOG_ERROR, "Response validation failed for /endpoints/{tech}/{resource}/sendMessage\n");
401  ast_ari_response_error(response, 500,
402  "Internal Server Error", "Response validation failed");
403  }
404 #endif /* AST_DEVMODE */
405 
406 fin: __attribute__((unused))
407  return;
408 }
void ast_ari_endpoints_send_message_to_endpoint(struct ast_variable *headers, struct ast_ari_endpoints_send_message_to_endpoint_args *args, struct ast_ari_response *response)
Send a message to some endpoint in a technology.

References args, ast_ari_endpoints_send_message_to_endpoint(), ast_ari_response_error(), ast_ari_validate_void(), ast_log, LOG_ERROR, ast_ari_response::message, ast_variable::name, ast_variable::next, NULL, ast_ari_response::response_code, and ast_variable::value.

◆ ast_ari_endpoints_send_message_to_endpoint_parse_body()

int ast_ari_endpoints_send_message_to_endpoint_parse_body ( struct ast_json body,
struct ast_ari_endpoints_send_message_to_endpoint_args args 
)

Body parsing function for /endpoints/{tech}/{resource}/sendMessage.

Parameters
bodyThe JSON body from which to parse parameters.
[out]argsThe args structure to parse into.
Return values
zeroon success
non-zeroon failure

Definition at line 318 of file res_ari_endpoints.c.

321 {
322  struct ast_json *field;
323  /* Parse query parameters out of it */
324  field = ast_json_object_get(body, "from");
325  if (field) {
326  args->from = ast_json_string_get(field);
327  }
328  field = ast_json_object_get(body, "body");
329  if (field) {
330  args->body = ast_json_string_get(field);
331  }
332  return 0;
333 }

References args, ast_json_object_get(), and ast_json_string_get().

Referenced by ast_ari_endpoints_send_message_to_endpoint().

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 483 of file res_ari_endpoints.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 464 of file res_ari_endpoints.c.

465 {
466  int res = 0;
467 
468 
470  if (res) {
471  unload_module();
473  }
474 
476 }
int ast_ari_add_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:179
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static struct stasis_rest_handlers endpoints
REST handler for /api-docs/endpoints.json.
static int unload_module(void)

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 458 of file res_ari_endpoints.c.

459 {
461  return 0;
462 }
int ast_ari_remove_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:202

References ast_ari_remove_handler(), and endpoints.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "RESTful API module - Endpoint resources" , .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 = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_ari,res_ari_model,res_stasis", }
static

Definition at line 464 of file res_ari_endpoints.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 483 of file res_ari_endpoints.c.

◆ endpoints

struct stasis_rest_handlers endpoints
static

REST handler for /api-docs/endpoints.json.

Definition at line 344 of file res_ari_endpoints.c.

Referenced by unload_module().

◆ endpoints_sendMessage

struct stasis_rest_handlers endpoints_sendMessage
static

REST handler for /api-docs/endpoints.json.

Definition at line 344 of file res_ari_endpoints.c.

◆ endpoints_tech

struct stasis_rest_handlers endpoints_tech
static

REST handler for /api-docs/endpoints.json.

Definition at line 344 of file res_ari_endpoints.c.

◆ endpoints_tech_resource

struct stasis_rest_handlers endpoints_tech_resource
static

REST handler for /api-docs/endpoints.json.

Definition at line 344 of file res_ari_endpoints.c.

◆ endpoints_tech_resource_sendMessage

struct stasis_rest_handlers endpoints_tech_resource_sendMessage
static

REST handler for /api-docs/endpoints.json.

Definition at line 344 of file res_ari_endpoints.c.