Asterisk - The Open Source Telephony Project  18.5.0
res_pjsip_pidf_body_generator.c
Go to the documentation of this file.
1 /*
2  * asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2014, Digium, Inc.
5  *
6  * Mark Michelson <[email protected]>
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 /*** MODULEINFO
20  <depend>pjproject</depend>
21  <depend>res_pjsip</depend>
22  <depend>res_pjsip_pubsub</depend>
23  <support_level>core</support_level>
24  ***/
25 
26 #include "asterisk.h"
27 
28 #include <pjsip.h>
29 #include <pjsip_simple.h>
30 #include <pjlib.h>
31 
32 #include "asterisk/module.h"
33 #include "asterisk/res_pjsip.h"
37 
38 static void *pidf_allocate_body(void *data)
39 {
40  struct ast_sip_exten_state_data *state_data = data;
41  char *local = ast_strdupa(state_data->local);
42  pjpidf_pres *pres;
43  pj_str_t entity;
44 
45  pres = pjpidf_create(state_data->pool, pj_cstr(&entity, ast_strip_quoted(local, "<", ">")));
46 
47  return pres;
48 }
49 
50 static int pidf_generate_body_content(void *body, void *data)
51 {
52  pjpidf_tuple *tuple;
53  pj_str_t note, id, contact, priority;
54  char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL;
55  enum ast_sip_pidf_state local_state;
56  char sanitized[PJSIP_MAX_URL_SIZE];
57  pjpidf_pres *pres = body;
58  struct ast_sip_exten_state_data *state_data = data;
59 
60  ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring,
61  &pidfstate, &pidfnote, &local_state, 0);
62 
63  if (!pjpidf_pres_add_note(state_data->pool, pres, pj_cstr(&note, pidfnote))) {
64  ast_log(LOG_WARNING, "Unable to add note to PIDF presence\n");
65  return -1;
66  }
67 
68  if (!(tuple = pjpidf_pres_add_tuple(state_data->pool, pres,
69  pj_cstr(&id, state_data->exten)))) {
70  ast_log(LOG_WARNING, "Unable to create PIDF tuple\n");
71  return -1;
72  }
73 
74  ast_sip_sanitize_xml(state_data->remote, sanitized, sizeof(sanitized));
75  pjpidf_tuple_set_contact(state_data->pool, tuple, pj_cstr(&contact, sanitized));
76  pjpidf_tuple_set_contact_prio(state_data->pool, tuple, pj_cstr(&priority, "1"));
77  pjpidf_status_set_basic_open(pjpidf_tuple_get_status(tuple),
78  local_state == NOTIFY_OPEN || local_state == NOTIFY_INUSE);
79 
80  return 0;
81 }
82 
83 #define MAX_STRING_GROWTHS 5
84 
85 static void pidf_to_string(void *body, struct ast_str **str)
86 {
87  pjpidf_pres *pres = body;
88  int growths = 0;
89  int size;
90 
91  do {
92  size = pjpidf_print(pres, ast_str_buffer(*str), ast_str_size(*str) - 1);
93  if (size <= AST_PJSIP_XML_PROLOG_LEN) {
94  ast_str_make_space(str, ast_str_size(*str) * 2);
95  ++growths;
96  }
97  } while (size <= AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
98  if (size <= AST_PJSIP_XML_PROLOG_LEN) {
99  ast_log(LOG_WARNING, "PIDF body text too large\n");
100  return;
101  }
102 
103  *(ast_str_buffer(*str) + size) = '\0';
104  ast_str_update(*str);
105 }
106 
108  .type = "application",
109  .subtype = "pidf+xml",
110  .body_type = AST_SIP_EXTEN_STATE_DATA,
111  .allocate_body = pidf_allocate_body,
112  .generate_body_content = pidf_generate_body_content,
113  .to_string = pidf_to_string,
114  /* No need for a destroy_body callback since we use a pool */
115 };
116 
117 static int load_module(void)
118 {
119  if (ast_sip_pubsub_register_body_generator(&pidf_body_generator)) {
121  }
123 }
124 
125 static int unload_module(void)
126 {
127  ast_sip_pubsub_unregister_body_generator(&pidf_body_generator);
128  return 0;
129 }
130 
131 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State PIDF Provider",
132  .support_level = AST_MODULE_SUPPORT_CORE,
133  .load = load_module,
134  .unload = unload_module,
135  .load_pri = AST_MODPRI_CHANNEL_DEPEND,
136  .requires = "res_pjsip,res_pjsip_pubsub",
137 );
Asterisk main include file. File version handling, generic pbx functions.
static void * pidf_allocate_body(void *data)
Pubsub body generator.
const char * type
Content type In "plain/text", "plain" is the type.
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
Definition: strings.h:699
#define LOG_WARNING
Definition: logger.h:274
#define ast_str_make_space(buf, new_len)
Definition: strings.h:780
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
static int entity
Definition: isdn_lib.c:259
#define MAX_STRING_GROWTHS
static int load_module(void)
enum ast_extension_states exten_state
const char * str
Definition: app_jack.c:147
int ast_sip_pubsub_register_body_generator(struct ast_sip_pubsub_body_generator *generator)
Register a body generator with the pubsub core.
#define NULL
Definition: resample.c:96
static int priority
static struct ast_sip_pubsub_body_generator pidf_body_generator
#define AST_SIP_EXTEN_STATE_DATA
char * ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes)
Strip leading/trailing whitespace and quotes from a string.
Definition: main/utils.c:1639
#define ast_log
Definition: astobj2.c:42
void ast_sip_presence_exten_state_to_str(int state, char **statestring, char **pidfstate, char **pidfnote, enum ast_sip_pidf_state *local_state, unsigned int notify_early_inuse_ringing)
Convert extension state to relevant PIDF strings.
Definition: presence_xml.c:84
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
#define AST_PJSIP_XML_PROLOG_LEN
Length of the XML prolog when printing presence or other XML in PJSIP.
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
static int pidf_generate_body_content(void *body, void *data)
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
void ast_sip_sanitize_xml(const char *input, char *output, size_t len)
Replace offensive XML characters with XML entities.
Definition: presence_xml.c:29
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
structure used for presence XML bodies
static void pidf_to_string(void *body, struct ast_str **str)
void ast_str_update(struct ast_str *buf)
Update the length of the buffer, after using ast_str merely as a buffer.
Definition: strings.h:663
enum queue_result id
Definition: app_queue.c:1507
ast_sip_pidf_state
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
static int unload_module(void)
Asterisk module definitions.
void ast_sip_pubsub_unregister_body_generator(struct ast_sip_pubsub_body_generator *generator)
Unregister a body generator with the pubsub core.