Asterisk - The Open Source Telephony Project GIT-master-67613d1
json.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2012 - 2013, Digium, Inc.
5 *
6 * David M. Lee, II <dlee@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*! \file
20 *
21 * \brief JSON abstraction layer.
22 *
23 * This is a very thin wrapper around the Jansson API. For more details on it,
24 * see its docs at http://www.digip.org/jansson/doc/2.11/apiref.html.
25 *
26 * \author David M. Lee, II <dlee@digium.com>
27 */
28
29/*** MODULEINFO
30 <depend>jansson</depend>
31 <support_level>core</support_level>
32 ***/
33
34#include "asterisk.h"
35
36#include "asterisk/json.h"
37#include "asterisk/localtime.h"
38#include "asterisk/module.h"
39#include "asterisk/utils.h"
40#include "asterisk/astobj2.h"
41#include "asterisk/channel.h"
42#include "asterisk/callerid.h"
43
44#include <jansson.h>
45#include <time.h>
46
47void *ast_json_malloc(size_t size)
48{
49 return ast_malloc(size);
50}
51
52void ast_json_free(void *p)
53{
54 ast_free(p);
55}
56
57void ast_json_set_alloc_funcs(void *(*malloc_fn)(size_t), void (*free_fn)(void*))
58{
59 json_set_alloc_funcs(malloc_fn, free_fn);
60}
61
63{
64 json_set_alloc_funcs(ast_json_malloc, ast_json_free);
65}
66
67struct ast_json *ast_json_ref(struct ast_json *json)
68{
69 json_incref((json_t *)json);
70 return json;
71}
72
73void ast_json_unref(struct ast_json *json)
74{
75 json_decref((json_t *) json);
76}
77
78enum ast_json_type ast_json_typeof(const struct ast_json *json)
79{
80 int r = json_typeof((json_t*)json);
81 switch(r) {
82 case JSON_OBJECT: return AST_JSON_OBJECT;
83 case JSON_ARRAY: return AST_JSON_ARRAY;
84 case JSON_STRING: return AST_JSON_STRING;
85 case JSON_INTEGER: return AST_JSON_INTEGER;
86 case JSON_REAL: return AST_JSON_REAL;
87 case JSON_TRUE: return AST_JSON_TRUE;
88 case JSON_FALSE: return AST_JSON_FALSE;
89 case JSON_NULL: return AST_JSON_NULL;
90 }
91 ast_assert(0); /* Unexpect return from json_typeof */
92 return r;
93}
94
96{
97 switch (type) {
98 case AST_JSON_OBJECT: return "object";
99 case AST_JSON_ARRAY: return "array";
100 case AST_JSON_STRING: return "string";
101 case AST_JSON_INTEGER: return "integer";
102 case AST_JSON_REAL: return "real";
103 case AST_JSON_TRUE: return "boolean";
104 case AST_JSON_FALSE: return "boolean";
105 case AST_JSON_NULL: return "null";
106 }
107 ast_assert(0);
108 return "?";
109}
110
111/* Ported from libjansson utf.c:utf8_check_first() */
112static size_t json_utf8_check_first(char byte)
113{
114 unsigned char ch = (unsigned char) byte;
115
116 if (ch < 0x80) {
117 return 1;
118 }
119
120 if (0x80 <= ch && ch <= 0xBF) {
121 /* second, third or fourth byte of a multi-byte
122 sequence, i.e. a "continuation byte" */
123 return 0;
124 } else if (ch == 0xC0 || ch == 0xC1) {
125 /* overlong encoding of an ASCII byte */
126 return 0;
127 } else if (0xC2 <= ch && ch <= 0xDF) {
128 /* 2-byte sequence */
129 return 2;
130 } else if (0xE0 <= ch && ch <= 0xEF) {
131 /* 3-byte sequence */
132 return 3;
133 } else if (0xF0 <= ch && ch <= 0xF4) {
134 /* 4-byte sequence */
135 return 4;
136 } else { /* ch >= 0xF5 */
137 /* Restricted (start of 4-, 5- or 6-byte sequence) or invalid
138 UTF-8 */
139 return 0;
140 }
141}
142
143/* Ported from libjansson utf.c:utf8_check_full() */
144static size_t json_utf8_check_full(const char *str, size_t len)
145{
146 size_t pos;
148 unsigned char ch = (unsigned char) str[0];
149
150 if (len == 2) {
151 value = ch & 0x1F;
152 } else if (len == 3) {
153 value = ch & 0xF;
154 } else if (len == 4) {
155 value = ch & 0x7;
156 } else {
157 return 0;
158 }
159
160 for (pos = 1; pos < len; ++pos) {
161 ch = (unsigned char) str[pos];
162 if (ch < 0x80 || ch > 0xBF) {
163 /* not a continuation byte */
164 return 0;
165 }
166
167 value = (value << 6) + (ch & 0x3F);
168 }
169
170 if (value > 0x10FFFF) {
171 /* not in Unicode range */
172 return 0;
173 } else if (0xD800 <= value && value <= 0xDFFF) {
174 /* invalid code point (UTF-16 surrogate halves) */
175 return 0;
176 } else if ((len == 2 && value < 0x80)
177 || (len == 3 && value < 0x800)
178 || (len == 4 && value < 0x10000)) {
179 /* overlong encoding */
180 return 0;
181 }
182
183 return 1;
184}
185
186int ast_json_utf8_check_len(const char *str, size_t len)
187{
188 size_t pos;
189 size_t count;
190 int res = 1;
191
192 if (!str) {
193 return 0;
194 }
195
196 /*
197 * Since the json library does not make the check function
198 * public we recreate/copy the function in our interface
199 * module.
200 *
201 * Loop ported from libjansson utf.c:utf8_check_string()
202 */
203 for (pos = 0; pos < len; pos += count) {
204 count = json_utf8_check_first(str[pos]);
205 if (count == 0) {
206 res = 0;
207 break;
208 } else if (count > 1) {
209 if (count > len - pos) {
210 /* UTF-8 needs more than we have left in the string. */
211 res = 0;
212 break;
213 }
214
215 if (!json_utf8_check_full(&str[pos], count)) {
216 res = 0;
217 break;
218 }
219 }
220 }
221
222 if (!res) {
223 ast_debug(1, "String '%.*s' is not UTF-8 for json conversion\n", (int) len, str);
224 }
225 return res;
226}
227
228int ast_json_utf8_check(const char *str)
229{
230 return str ? ast_json_utf8_check_len(str, strlen(str)) : 0;
231}
232
234{
235 return (struct ast_json *)json_true();
236}
237
239{
240 return (struct ast_json *)json_false();
241}
242
244{
245 return (struct ast_json *)json_boolean(value);
246}
247
249{
250 return (struct ast_json *)json_null();
251}
252
253int ast_json_is_array(const struct ast_json *json)
254{
255 return json_is_array((const json_t *)json);
256}
257
258int ast_json_is_object(const struct ast_json *json)
259{
260 return json_is_object((const json_t *)json);
261}
262
263int ast_json_is_true(const struct ast_json *json)
264{
265 return json_is_true((const json_t *)json);
266}
267
268int ast_json_is_false(const struct ast_json *json)
269{
270 return json_is_false((const json_t *)json);
271}
272
273int ast_json_is_null(const struct ast_json *json)
274{
275 return json_is_null((const json_t *)json);
276}
277
279{
280 return (struct ast_json *)json_string(value);
281}
282
283const char *ast_json_string_get(const struct ast_json *string)
284{
285 return json_string_value((json_t *)string);
286}
287
288int ast_json_string_set(struct ast_json *string, const char *value)
289{
290 return json_string_set((json_t *)string, value);
291}
292
293struct ast_json *ast_json_stringf(const char *format, ...)
294{
295 struct ast_json *ret;
296 va_list args;
297 va_start(args, format);
298 ret = ast_json_vstringf(format, args);
299 va_end(args);
300 return ret;
301}
302
303struct ast_json *ast_json_vstringf(const char *format, va_list args)
304{
305 json_t *ret = NULL;
306
307 if (format) {
308 /* json_pack was not introduced until jansson-2.0 so Asterisk could never
309 * be compiled against older versions. The version check can never match
310 * anything older than 2.12. */
311#if defined(HAVE_JANSSON_BUNDLED) || JANSSON_MAJOR_VERSION > 2 || JANSSON_MINOR_VERSION > 11
312 ret = json_vsprintf(format, args);
313#else
314 char *str = NULL;
315 int err = ast_vasprintf(&str, format, args);
316
317 if (err >= 0) {
318 ret = json_string(str);
319 ast_free(str);
320 }
321#endif
322 }
323
324 return (struct ast_json *)ret;
325}
326
328{
329 return (struct ast_json *)json_integer(value);
330}
331
332intmax_t ast_json_integer_get(const struct ast_json *integer)
333{
334 return json_integer_value((json_t *)integer);
335}
336
338{
339 return json_integer_set((json_t *)integer, value);
340}
341
343{
344 return (struct ast_json *)json_real(value);
345}
346
347double ast_json_real_get(const struct ast_json *real)
348{
349 return json_real_value((json_t *)real);
350}
351
353{
354 return json_real_set((json_t *)real, value);
355}
356
357int ast_json_equal(const struct ast_json *lhs, const struct ast_json *rhs)
358{
359 return json_equal((json_t *)lhs, (json_t *)rhs);
360}
361
363{
364 return (struct ast_json *)json_array();
365}
366size_t ast_json_array_size(const struct ast_json *array)
367{
368 return json_array_size((json_t *)array);
369}
370struct ast_json *ast_json_array_get(const struct ast_json *array, size_t index)
371{
372 return (struct ast_json *)json_array_get((json_t *)array, index);
373}
374int ast_json_array_set(struct ast_json *array, size_t index, struct ast_json *value)
375{
376 return json_array_set_new((json_t *)array, index, (json_t *)value);
377}
379{
380 return json_array_append_new((json_t *)array, (json_t *)value);
381}
382int ast_json_array_insert(struct ast_json *array, size_t index, struct ast_json *value)
383{
384 return json_array_insert_new((json_t *)array, index, (json_t *)value);
385}
386int ast_json_array_remove(struct ast_json *array, size_t index)
387{
388 return json_array_remove((json_t *)array, index);
389}
391{
392 return json_array_clear((json_t *)array);
393}
395{
396 return json_array_extend((json_t *)array, (json_t *)tail);
397}
398
400{
401 return (struct ast_json *)json_object();
402}
403size_t ast_json_object_size(struct ast_json *object)
404{
405 return json_object_size((json_t *)object);
406}
407struct ast_json *ast_json_object_get(struct ast_json *object, const char *key)
408{
409 if (!key) {
410 return NULL;
411 }
412 return (struct ast_json *)json_object_get((json_t *)object, key);
413}
414int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
415{
416 return json_object_set_new((json_t *)object, key, (json_t *)value);
417}
418int ast_json_object_del(struct ast_json *object, const char *key)
419{
420 return json_object_del((json_t *)object, key);
421}
423{
424 return json_object_clear((json_t *)object);
425}
426int ast_json_object_update(struct ast_json *object, struct ast_json *other)
427{
428 return json_object_update((json_t *)object, (json_t *)other);
429}
430int ast_json_object_update_existing(struct ast_json *object, struct ast_json *other)
431{
432 return json_object_update_existing((json_t *)object, (json_t *)other);
433}
434int ast_json_object_update_missing(struct ast_json *object, struct ast_json *other)
435{
436 return json_object_update_missing((json_t *)object, (json_t *)other);
437}
438
440{
441 return json_object_iter((json_t *)object);
442}
443struct ast_json_iter *ast_json_object_iter_at(struct ast_json *object, const char *key)
444{
445 return json_object_iter_at((json_t *)object, key);
446}
448{
449 return json_object_iter_next((json_t *)object, iter);
450}
452{
453 return json_object_iter_key(iter);
454}
456{
457 return (struct ast_json *)json_object_iter_value(iter);
458}
459int ast_json_object_iter_set(struct ast_json *object, struct ast_json_iter *iter, struct ast_json *value)
460{
461 return json_object_iter_set_new((json_t *)object, iter, (json_t *)value);
462}
463
464/*!
465 * \brief Default flags for JSON encoding.
466 */
467static size_t dump_flags(enum ast_json_encoding_format format)
468{
469 size_t jansson_dump_flags;
470
471 if (format & AST_JSON_PRETTY) {
472 jansson_dump_flags = JSON_INDENT(2);
473 } else {
474 jansson_dump_flags = JSON_COMPACT;
475 }
476
477 if (format & AST_JSON_SORTED) {
478 jansson_dump_flags |= JSON_SORT_KEYS;
479 }
480
481 return jansson_dump_flags;
482}
483
485{
486 return json_dumps((json_t *)root, dump_flags(format));
487}
488
489static int write_to_ast_str(const char *buffer, size_t size, void *data)
490{
491 struct ast_str **dst = data;
492 size_t str_size = ast_str_size(*dst);
493 size_t remaining = str_size - ast_str_strlen(*dst);
494 int ret;
495
496 /* While ast_str_append will grow the ast_str, it won't report
497 * allocation errors. Fortunately, it's not that hard.
498 */
499
500 /* Remaining needs to be big enough for buffer, plus null char */
501 while (remaining < size + 1) {
502 /* doubling the size of the buffer gives us 'amortized
503 * constant' time.
504 * See http://stackoverflow.com/a/249695/115478 for info.
505 */
506 str_size *= 2;
507 remaining = str_size - ast_str_strlen(*dst);
508 }
509
510 ret = ast_str_make_space(dst, str_size);
511 if (ret == -1) {
512 /* Could not alloc; fail */
513 return -1;
514 }
515
516 ast_str_append_substr(dst, -1, buffer, size);
517 return 0;
518}
519
520int ast_json_dump_str_format(struct ast_json *root, struct ast_str **dst, enum ast_json_encoding_format format)
521{
522 return json_dump_callback((json_t *)root, write_to_ast_str, dst, dump_flags(format));
523}
524
525
526int ast_json_dump_file_format(struct ast_json *root, FILE *output, enum ast_json_encoding_format format)
527{
528 if (!root || !output) {
529 return -1;
530 }
531 return json_dumpf((json_t *)root, output, dump_flags(format));
532}
533int ast_json_dump_new_file_format(struct ast_json *root, const char *path, enum ast_json_encoding_format format)
534{
535 if (!root || !path) {
536 return -1;
537 }
538 return json_dump_file((json_t *)root, path, dump_flags(format));
539}
540
541/*!
542 * \brief Copy Jansson error struct to ours.
543 */
544static void copy_error(struct ast_json_error *error, const json_error_t *jansson_error)
545{
546 if (error && jansson_error) {
547 error->line = jansson_error->line;
548 error->column = jansson_error->column;
549 error->position = jansson_error->position;
550 ast_copy_string(error->text, jansson_error->text, sizeof(error->text));
551 ast_copy_string(error->source, jansson_error->source, sizeof(error->source));
552 }
553
554}
555
556static void parse_error(struct ast_json_error *error, const char *text, const char *source)
557{
558 if (error != NULL) {
559 error->line = 0;
560 error->column = 0;
561 error->position = 0;
562 strncpy(error->text, text, sizeof(error->text));
563 strncpy(error->source, source, sizeof(error->text));
564 }
565}
566
568{
569 json_error_t jansson_error = {};
570 struct ast_json *r = NULL;
571 if (input != NULL) {
572 r = (struct ast_json *)json_loads(input, 0, &jansson_error);
573 copy_error(error, &jansson_error);
574 } else {
575 parse_error(error, "NULL input string", "<null>");
576 }
577 return r;
578}
579
581{
583}
584
585struct ast_json *ast_json_load_buf(const char *buffer, size_t buflen, struct ast_json_error *error)
586{
587 json_error_t jansson_error = {};
588 struct ast_json *r = (struct ast_json *)json_loadb(buffer, buflen, 0, &jansson_error);
589 copy_error(error, &jansson_error);
590 return r;
591}
593{
594 json_error_t jansson_error = {};
595 struct ast_json *r = NULL;
596 if (input != NULL) {
597 r = (struct ast_json *)json_loadf(input, 0, &jansson_error);
598 copy_error(error, &jansson_error);
599 } else {
600 parse_error(error, "NULL input file", "<null>");
601 }
602 return r;
603}
604struct ast_json *ast_json_load_new_file(const char *path, struct ast_json_error *error)
605{
606 json_error_t jansson_error = {};
607 struct ast_json *r = (struct ast_json *)json_load_file(path, 0, &jansson_error);
608 copy_error(error, &jansson_error);
609 return r;
610}
611
612struct ast_json *ast_json_pack(char const *format, ...)
613{
614 struct ast_json *ret;
615 va_list args;
616 va_start(args, format);
617 ret = ast_json_vpack(format, args);
618 va_end(args);
619 return ret;
620}
621struct ast_json *ast_json_vpack(char const *format, va_list ap)
622{
623 json_error_t error;
624 struct ast_json *r = NULL;
625 if (format) {
626 r = (struct ast_json *)json_vpack_ex(&error, 0, format, ap);
627 if (!r && !ast_strlen_zero(error.text)) {
629 "Error building JSON from '%s': %s.\n",
630 format, error.text);
632 }
633 }
634 return r;
635}
636
637struct ast_json *ast_json_copy(const struct ast_json *value)
638{
639 return (struct ast_json *)json_copy((json_t *)value);
640}
642{
643 return (struct ast_json *)json_deep_copy((json_t *)value);
644}
645
646struct ast_json *ast_json_name_number(const char *name, const char *number)
647{
648 return ast_json_pack("{s: s, s: s}",
650 "number", AST_JSON_UTF8_VALIDATE(number));
651}
652
654 const char *context, const char *exten, int priority, const char *app_name, const char *app_data)
655{
656 return ast_json_pack("{s: s?, s: s?, s: o, s: s?, s: s?}",
657 "context", context,
658 "exten", exten,
660 "app_name", app_name,
661 "app_data", app_data
662 );
663}
664
665struct ast_json *ast_json_dialplan_cep(const char *context, const char *exten, int priority)
666{
667 return ast_json_dialplan_cep_app(context, exten, priority, "", "");
668}
669
670struct ast_json *ast_json_timeval(const struct timeval tv, const char *zone)
671{
672 char buf[AST_ISO8601_LEN];
673 struct ast_tm tm = {};
674
675 ast_localtime(&tv, &tm, zone);
676
677 ast_strftime(buf, sizeof(buf),AST_ISO8601_FORMAT, &tm);
678
680}
681
682struct ast_json *ast_json_ipaddr(const struct ast_sockaddr *addr, enum ast_transport transport_type)
683{
684 struct ast_str *string = ast_str_alloca(64);
685
686 if (!string) {
687 return NULL;
688 }
689
690 ast_str_set(&string, 0, (ast_sockaddr_is_ipv4(addr) ||
691 ast_sockaddr_is_ipv4_mapped(addr)) ? "IPV4/" : "IPV6/");
692
693 if (transport_type) {
694 char *transport_string = NULL;
695
696 /* NOTE: None will be applied if multiple transport types are specified in transport_type */
697 switch(transport_type) {
699 transport_string = "UDP";
700 break;
702 transport_string = "TCP";
703 break;
705 transport_string = "TLS";
706 break;
707 case AST_TRANSPORT_WS:
708 transport_string = "WS";
709 break;
711 transport_string = "WSS";
712 break;
713 }
714
715 if (transport_string) {
716 ast_str_append(&string, 0, "%s/", transport_string);
717 }
718 }
719
720 ast_str_append(&string, 0, "%s", ast_sockaddr_stringify_addr(addr));
721 ast_str_append(&string, 0, "/%s", ast_sockaddr_stringify_port(addr));
722
724}
725
727{
728 json_t *version_check;
729
730 /* Setup to use Asterisk custom allocators */
732
733 /* We depend on functionality of jansson-2.11 but don't actually use
734 * any symbols. If we link at runtime to less than 2.11 this json_pack
735 * will return NULL. */
736 version_check = json_pack("{s: o?, s: o*}",
737 "JSON", NULL,
738 "Bourne", NULL);
739 if (!version_check) {
740 ast_log(LOG_ERROR, "There was a problem finding jansson 2.11 runtime libraries.\n"
741 "Please rebuild Asterisk using ./configure --with-jansson-bundled.\n");
742 return -1;
743 }
744
745 json_decref(version_check);
746
747 return 0;
748}
749
750static void json_payload_destructor(void *obj)
751{
752 struct ast_json_payload *payload = obj;
753 ast_json_unref(payload->json);
754}
755
757{
758 struct ast_json_payload *payload;
759
760 if (!(payload = ao2_alloc(sizeof(*payload), json_payload_destructor))) {
761 return NULL;
762 }
763
765 payload->json = json;
766
767 return payload;
768}
769
771{
772 if (!number->valid) {
773 return NULL;
774 }
775 return ast_json_pack("{s: s, s: i, s: i, s: s}",
776 "number", AST_JSON_UTF8_VALIDATE(number->str),
777 "plan", number->plan,
778 "presentation", number->presentation,
779 "presentation_txt", ast_describe_caller_presentation(number->presentation));
780}
781
783{
784 if (!name->valid) {
785 return NULL;
786 }
787 return ast_json_pack("{s: s, s: s, s: i, s: s}",
788 "name", AST_JSON_UTF8_VALIDATE(name->str),
789 "character_set", ast_party_name_charset_describe(name->char_set),
790 "presentation", name->presentation,
791 "presentation_txt", ast_describe_caller_presentation(name->presentation));
792}
793
794static struct ast_json *json_party_subaddress(struct ast_party_subaddress *subaddress)
795{
796 if (!subaddress->valid) {
797 return NULL;
798 }
799 return ast_json_pack("{s: s, s: i, s: b}",
800 "subaddress", AST_JSON_UTF8_VALIDATE(subaddress->str),
801 "type", subaddress->type,
802 "odd", subaddress->odd_even_indicator);
803}
804
806{
807 int pres = ast_party_id_presentation(party);
808
809 /* Combined party presentation */
810 return ast_json_pack("{s: i, s: s, s: o*, s: o*, s: o*}",
811 "presentation", pres,
812 "presentation_txt", ast_describe_caller_presentation(pres),
813 "number", json_party_number(&party->number),
814 "name", json_party_name(&party->name),
815 "subaddress", json_party_subaddress(&party->subaddress));
816}
817
818enum ast_json_to_ast_vars_code ast_json_to_ast_variables(struct ast_json *json_variables, struct ast_variable **variables)
819{
820 struct ast_json_iter *it_json_var;
821 struct ast_variable *tail = NULL;
822
823 *variables = NULL;
824
825 for (it_json_var = ast_json_object_iter(json_variables); it_json_var;
826 it_json_var = ast_json_object_iter_next(json_variables, it_json_var)) {
827 struct ast_variable *new_var;
828 const char *key = ast_json_object_iter_key(it_json_var);
829 const char *value;
830 struct ast_json *json_value;
831
832 if (ast_strlen_zero(key)) {
833 continue;
834 }
835
836 json_value = ast_json_object_iter_value(it_json_var);
837 if (ast_json_typeof(json_value) != AST_JSON_STRING) {
838 /* Error: Only strings allowed */
839 ast_variables_destroy(*variables);
840 *variables = NULL;
842 }
843 value = ast_json_string_get(json_value);
844 /* Should never be NULL. Otherwise, how could it be a string type? */
846 if (!value) {
847 /* To be safe. */
848 continue;
849 }
850 new_var = ast_variable_new(key, value, "");
851 if (!new_var) {
852 /* Error: OOM */
853 ast_variables_destroy(*variables);
854 *variables = NULL;
856 }
857
858 tail = ast_variable_list_append_hint(variables, tail, new_var);
859 }
860
862}
863
864struct ast_json *ast_json_channel_vars(struct varshead *channelvars)
865{
866 struct ast_json *ret;
867 struct ast_var_t *var;
868
870 AST_LIST_TRAVERSE(channelvars, var, entries) {
872 }
873
874 return ret;
875}
876
877struct ast_json *ast_json_object_create_vars(const struct ast_variable *variables, const char *excludes)
878{
879 const struct ast_variable *i;
880 struct ast_json *obj;
881
883 if (!obj) {
884 return NULL;
885 }
886
887 for (i = variables; i; i = i->next) {
888 if (!excludes || !ast_in_delimited_string(i->name, excludes, ',')) {
890 }
891 }
892
893 return obj;
894}
const char * str
Definition: app_jack.c:147
char * text
Definition: app_queue.c:1639
#define var
Definition: ast_expr2f.c:605
static int input(yyscan_t yyscanner)
Definition: ast_expr2f.c:1570
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_vasprintf(ret, fmt, ap)
A wrapper for vasprintf()
Definition: astmm.h:278
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
#define ast_log
Definition: astobj2.c:42
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:409
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
const char * ast_describe_caller_presentation(int data)
Convert caller ID pres value to explanatory string.
Definition: callerid.c:1265
const char * ast_party_name_charset_describe(int data)
Convert ast_party_name.char_set value to explanatory string.
Definition: callerid.c:1448
static int priority
static const char type[]
Definition: chan_ooh323.c:109
General Asterisk PBX channel definitions.
int ast_party_id_presentation(const struct ast_party_id *id)
Determine the overall presentation value for the given party.
Definition: channel.c:1821
int int32_t
Definition: db.h:60
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static const char name[]
Definition: format_mp3.c:68
static int array(struct ast_channel *chan, const char *cmd, char *var, const char *value)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ast_variable_new(name, value, filename)
struct ast_variable * ast_variable_list_append_hint(struct ast_variable **head, struct ast_variable *search_hint, struct ast_variable *new_var)
Appends a variable list to the end of another list.
Definition: main/config.c:646
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1262
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
void ast_log_backtrace(void)
Log a backtrace of the current thread's execution stack to the Asterisk log.
Definition: logger.c:2498
int ast_json_array_remove(struct ast_json *array, size_t index)
Remove an element from an array.
Definition: json.c:386
enum ast_json_type ast_json_typeof(const struct ast_json *json)
Get the type of value.
Definition: json.c:78
int ast_json_object_clear(struct ast_json *object)
Delete all elements from a JSON object.
Definition: json.c:422
static struct ast_json * json_party_name(struct ast_party_name *name)
Definition: json.c:782
int ast_json_is_true(const struct ast_json *json)
Check if value is JSON true.
Definition: json.c:263
struct ast_json * ast_json_false(void)
Get the JSON false value.
Definition: json.c:238
struct ast_json * ast_json_ipaddr(const struct ast_sockaddr *addr, enum ast_transport transport_type)
Construct an IP address as JSON.
Definition: json.c:682
static struct ast_json * json_party_subaddress(struct ast_party_subaddress *subaddress)
Definition: json.c:794
struct ast_json * ast_json_object_iter_value(struct ast_json_iter *iter)
Get the value from an iterator.
Definition: json.c:455
struct ast_json * ast_json_dialplan_cep(const char *context, const char *exten, int priority)
Construct a context/exten/priority as JSON.
Definition: json.c:665
struct ast_json * ast_json_deep_copy(const struct ast_json *value)
Copy a JSON value, and its children.
Definition: json.c:641
int ast_json_array_clear(struct ast_json *array)
Remove all elements from an array.
Definition: json.c:390
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:278
int ast_json_object_update_missing(struct ast_json *object, struct ast_json *other)
Add new fields to object with the fields of other.
Definition: json.c:434
struct ast_json * ast_json_null(void)
Get the JSON null value.
Definition: json.c:248
void ast_json_free(void *p)
Asterisk's custom JSON allocator. Exposed for use by unit tests.
Definition: json.c:52
struct ast_json_iter * ast_json_object_iter_next(struct ast_json *object, struct ast_json_iter *iter)
Get the next iterator.
Definition: json.c:447
int ast_json_array_set(struct ast_json *array, size_t index, struct ast_json *value)
Change an element in an array.
Definition: json.c:374
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:378
void ast_json_unref(struct ast_json *json)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
int ast_json_equal(const struct ast_json *lhs, const struct ast_json *rhs)
Compare two JSON objects.
Definition: json.c:357
size_t ast_json_object_size(struct ast_json *object)
Get size of JSON object.
Definition: json.c:403
struct ast_json * ast_json_ref(struct ast_json *json)
Increase refcount on value.
Definition: json.c:67
static void json_payload_destructor(void *obj)
Definition: json.c:750
struct ast_json * ast_json_object_create(void)
Create a new JSON object.
Definition: json.c:399
int ast_json_is_false(const struct ast_json *json)
Check if value is JSON false.
Definition: json.c:268
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
struct ast_json * ast_json_name_number(const char *name, const char *number)
Common JSON rendering functions for common 'objects'.
Definition: json.c:646
const char * ast_json_typename(enum ast_json_type type)
Get the string name for the given type.
Definition: json.c:95
struct ast_json_payload * ast_json_payload_create(struct ast_json *json)
Create an ao2 object to pass json blobs as data payloads for stasis.
Definition: json.c:756
int ast_json_integer_set(struct ast_json *integer, intmax_t value)
Set the value of a JSON integer.
Definition: json.c:337
int ast_json_is_null(const struct ast_json *json)
Check if value is JSON null.
Definition: json.c:273
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
int ast_json_array_insert(struct ast_json *array, size_t index, struct ast_json *value)
Insert into an array.
Definition: json.c:382
struct ast_json * ast_json_timeval(const struct timeval tv, const char *zone)
Construct a timeval as JSON.
Definition: json.c:670
struct ast_json * ast_json_load_str(const struct ast_str *input, struct ast_json_error *error)
Parse ast_str into a JSON object or array.
Definition: json.c:580
struct ast_json * ast_json_boolean(int value)
Get the JSON boolean corresponding to value.
Definition: json.c:243
static size_t json_utf8_check_full(const char *str, size_t len)
Definition: json.c:144
static int write_to_ast_str(const char *buffer, size_t size, void *data)
Definition: json.c:489
struct ast_json * ast_json_integer_create(intmax_t value)
Create a JSON integer.
Definition: json.c:327
struct ast_json * ast_json_vstringf(const char *format, va_list args)
Create a JSON string, vprintf style.
Definition: json.c:303
struct ast_json * ast_json_stringf(const char *format,...)
Create a JSON string, printf style.
Definition: json.c:293
struct ast_json * ast_json_load_string(const char *input, struct ast_json_error *error)
Parse null terminated string into a JSON object or array.
Definition: json.c:567
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
Definition: json.c:362
struct ast_json * ast_json_party_id(struct ast_party_id *party)
Construct an ast_party_id as JSON.
Definition: json.c:805
void * ast_json_malloc(size_t size)
Asterisk's custom JSON allocator. Exposed for use by unit tests.
Definition: json.c:47
static size_t json_utf8_check_first(char byte)
Definition: json.c:112
void ast_json_reset_alloc_funcs(void)
Change alloc funcs back to the resource module defaults.
Definition: json.c:62
struct ast_json * ast_json_object_create_vars(const struct ast_variable *variables, const char *excludes)
Create a new JSON object using the given variables.
Definition: json.c:877
int ast_json_object_update_existing(struct ast_json *object, struct ast_json *other)
Update existing fields in object with the fields of other.
Definition: json.c:430
int ast_json_object_iter_set(struct ast_json *object, struct ast_json_iter *iter, struct ast_json *value)
Set the value of the field pointed to by an iterator.
Definition: json.c:459
struct ast_json * ast_json_true(void)
Get the JSON true value.
Definition: json.c:233
struct ast_json_iter * ast_json_object_iter_at(struct ast_json *object, const char *key)
Get an iterator pointing to a specified key in object.
Definition: json.c:443
static struct ast_json * json_party_number(struct ast_party_number *number)
Definition: json.c:770
int ast_json_object_del(struct ast_json *object, const char *key)
Delete a field from a JSON object.
Definition: json.c:418
struct ast_json_iter * ast_json_object_iter(struct ast_json *object)
Get an iterator pointing to the first field in a JSON object.
Definition: json.c:439
int ast_json_array_extend(struct ast_json *array, struct ast_json *tail)
Append all elements from tail to array.
Definition: json.c:394
int ast_json_dump_file_format(struct ast_json *root, FILE *output, enum ast_json_encoding_format format)
Encode a JSON value to a FILE.
Definition: json.c:526
static void copy_error(struct ast_json_error *error, const json_error_t *jansson_error)
Copy Jansson error struct to ours.
Definition: json.c:544
int ast_json_utf8_check_len(const char *str, size_t len)
Check the string of the given length for UTF-8 format.
Definition: json.c:186
static void parse_error(struct ast_json_error *error, const char *text, const char *source)
Definition: json.c:556
int ast_json_real_set(struct ast_json *real, double value)
Set the value of a JSON real number.
Definition: json.c:352
struct ast_json * ast_json_load_file(FILE *input, struct ast_json_error *error)
Parse a FILE into JSON object or array.
Definition: json.c:592
struct ast_json * ast_json_dialplan_cep_app(const char *context, const char *exten, int priority, const char *app_name, const char *app_data)
Construct a context/exten/priority/application/application_data as JSON.
Definition: json.c:653
int ast_json_dump_str_format(struct ast_json *root, struct ast_str **dst, enum ast_json_encoding_format format)
Encode a JSON value to an ast_str.
Definition: json.c:520
int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
Set a field in a JSON object.
Definition: json.c:414
int ast_json_dump_new_file_format(struct ast_json *root, const char *path, enum ast_json_encoding_format format)
Encode a JSON value to a file at the given location.
Definition: json.c:533
struct ast_json * ast_json_load_buf(const char *buffer, size_t buflen, struct ast_json_error *error)
Parse buffer with known length into a JSON object or array.
Definition: json.c:585
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:283
const char * ast_json_object_iter_key(struct ast_json_iter *iter)
Get the key from an iterator.
Definition: json.c:451
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:407
void ast_json_set_alloc_funcs(void *(*malloc_fn)(size_t), void(*free_fn)(void *))
Set custom allocators instead of the standard ast_malloc() and ast_free().
Definition: json.c:57
enum ast_json_to_ast_vars_code ast_json_to_ast_variables(struct ast_json *json_variables, struct ast_variable **variables)
Convert a ast_json list of key/value pair tuples into a ast_variable list.
Definition: json.c:818
int ast_json_init(void)
Initialize the JSON library.
Definition: json.c:726
intmax_t ast_json_integer_get(const struct ast_json *integer)
Get the value from a JSON integer.
Definition: json.c:332
int ast_json_is_array(const struct ast_json *json)
Check if value is JSON array.
Definition: json.c:253
struct ast_json * ast_json_channel_vars(struct varshead *channelvars)
Construct a JSON object from a ast_var_t list.
Definition: json.c:864
char * ast_json_dump_string_format(struct ast_json *root, enum ast_json_encoding_format format)
Encode a JSON value to a string.
Definition: json.c:484
struct ast_json * ast_json_copy(const struct ast_json *value)
Copy a JSON value, but not its children.
Definition: json.c:637
struct ast_json * ast_json_real_create(double value)
Create a JSON real number.
Definition: json.c:342
int ast_json_is_object(const struct ast_json *json)
Check if value is JSON object.
Definition: json.c:258
struct ast_json * ast_json_load_new_file(const char *path, struct ast_json_error *error)
Parse file at path into JSON object or array.
Definition: json.c:604
int ast_json_utf8_check(const char *str)
Check the nul terminated string for UTF-8 format.
Definition: json.c:228
struct ast_json * ast_json_vpack(char const *format, va_list ap)
Helper for creating complex JSON values simply.
Definition: json.c:621
int ast_json_object_update(struct ast_json *object, struct ast_json *other)
Update object with all of the fields of other.
Definition: json.c:426
size_t ast_json_array_size(const struct ast_json *array)
Get the size of a JSON array.
Definition: json.c:366
double ast_json_real_get(const struct ast_json *real)
Get the value from a JSON real number.
Definition: json.c:347
int ast_json_string_set(struct ast_json *string, const char *value)
Change the value of a JSON string.
Definition: json.c:288
static size_t dump_flags(enum ast_json_encoding_format format)
Default flags for JSON encoding.
Definition: json.c:467
Asterisk JSON abstraction layer.
ast_json_type
Valid types of a JSON element.
Definition: json.h:163
@ AST_JSON_STRING
Definition: json.h:166
@ AST_JSON_ARRAY
Definition: json.h:165
@ AST_JSON_NULL
Definition: json.h:171
@ AST_JSON_OBJECT
Definition: json.h:164
@ AST_JSON_FALSE
Definition: json.h:170
@ AST_JSON_REAL
Definition: json.h:168
@ AST_JSON_INTEGER
Definition: json.h:167
@ AST_JSON_TRUE
Definition: json.h:169
ast_json_encoding_format
Encoding format type.
Definition: json.h:791
@ AST_JSON_SORTED
Definition: json.h:797
@ AST_JSON_PRETTY
Definition: json.h:795
#define AST_JSON_UTF8_VALIDATE(str)
Check str for UTF-8 and replace with an empty string if fails the check.
Definition: json.h:224
ast_json_to_ast_vars_code
Definition: json.h:1109
@ AST_JSON_TO_AST_VARS_CODE_INVALID_TYPE
Conversion failed because invalid value type supplied.
Definition: json.h:1116
@ AST_JSON_TO_AST_VARS_CODE_SUCCESS
Conversion successful.
Definition: json.h:1111
@ AST_JSON_TO_AST_VARS_CODE_OOM
Conversion failed because of allocation failure. (Out Of Memory)
Definition: json.h:1118
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:491
Custom localtime functions for multiple timezones.
#define AST_ISO8601_LEN
Max length of an null terminated, millisecond resolution, ISO8601 timestamp string.
Definition: localtime.h:105
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
#define AST_ISO8601_FORMAT
ast_strftime for ISO8601 formatting timestamps.
Definition: localtime.h:103
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2524
float real
Definition: lpc10.h:79
INT32 integer
Definition: lpc10.h:80
Asterisk module definitions.
ast_transport
Definition: netsock2.h:59
@ AST_TRANSPORT_WSS
Definition: netsock2.h:64
@ AST_TRANSPORT_WS
Definition: netsock2.h:63
@ AST_TRANSPORT_UDP
Definition: netsock2.h:60
@ AST_TRANSPORT_TLS
Definition: netsock2.h:62
@ AST_TRANSPORT_TCP
Definition: netsock2.h:61
static char * ast_sockaddr_stringify_port(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return a port only.
Definition: netsock2.h:358
static char * ast_sockaddr_stringify_addr(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return an address only.
Definition: netsock2.h:286
int ast_sockaddr_is_ipv4(const struct ast_sockaddr *addr)
Determine if the address is an IPv4 address.
Definition: netsock2.c:497
int ast_sockaddr_is_ipv4_mapped(const struct ast_sockaddr *addr)
Determine if this is an IPv4-mapped IPv6 address.
Definition: netsock2.c:507
const char * app_name(struct ast_app *app)
Definition: pbx_app.c:463
#define NULL
Definition: resample.c:96
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
char * ast_str_append_substr(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc)
Append a non-NULL terminated substring to the end of a dynamic string.
Definition: strings.h:1062
#define ast_str_alloca(init_len)
Definition: strings.h:848
int ast_in_delimited_string(const char *needle, const char *haystack, char delim)
Check if there is an exact match for 'needle' between delimiters in 'haystack'.
Definition: strings.c:433
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
#define ast_str_make_space(buf, new_len)
Definition: strings.h:828
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:730
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
Definition: strings.h:742
JSON parsing error information.
Definition: json.h:887
Iterator for JSON object key/values.
struct ast_json * json
Definition: json.h:1083
Abstract JSON element (object, array, string, int, ...).
Information needed to identify an endpoint in a call.
Definition: channel.h:338
struct ast_party_subaddress subaddress
Subscriber subaddress.
Definition: channel.h:344
struct ast_party_name name
Subscriber name.
Definition: channel.h:340
struct ast_party_number number
Subscriber phone number.
Definition: channel.h:342
Information needed to specify a name in a call.
Definition: channel.h:262
Information needed to specify a number in a call.
Definition: channel.h:289
Information needed to specify a subaddress in a call.
Definition: channel.h:307
unsigned char odd_even_indicator
TRUE if odd number of address signals.
Definition: channel.h:326
unsigned char valid
TRUE if the subaddress information is valid/present.
Definition: channel.h:328
char * str
Malloced subaddress string.
Definition: channel.h:313
int type
Q.931 subaddress type.
Definition: channel.h:320
Socket address structure.
Definition: netsock2.h:97
Support for dynamic strings.
Definition: strings.h:623
struct ast_var_t::@211 entries
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
Number structure.
Definition: app_followme.c:154
int value
Definition: syslog.c:37
const char * args
Time-related functions and macros.
int error(const char *format,...)
Definition: utils/frame.c:999
Utility functions.
#define ast_assert(a)
Definition: utils.h:739