Asterisk - The Open Source Telephony Project  18.5.0
res_pjsip_xpidf_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 *xpidf_allocate_body(void *data)
39 {
40  struct ast_sip_exten_state_data *state_data = data;
41  char *local = ast_strdupa(state_data->local);
42  pjxpidf_pres *pres;
43  pj_str_t name;
44 
45  pres = pjxpidf_create(state_data->pool, pj_cstr(&name, ast_strip_quoted(local, "<", ">")));
46  return pres;
47 }
48 
49 static int xpidf_generate_body_content(void *body, void *data)
50 {
51  pjxpidf_pres *pres = body;
52  struct ast_sip_exten_state_data *state_data = data;
53  static pj_str_t STR_ADDR_PARAM = { ";user=ip", 8 };
54  char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL;
55  pj_xml_attr *attr;
56  enum ast_sip_pidf_state local_state;
57  pj_str_t uri;
58  char sanitized[PJSIP_MAX_URL_SIZE];
59  pj_xml_node *atom;
60  pj_xml_node *address;
61  pj_xml_node *status;
62  pj_xml_node *msnsubstatus;
63 
64  ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring,
65  &pidfstate, &pidfnote, &local_state, 0);
66 
67  ast_sip_presence_xml_find_node_attr(state_data->pool, pres, "atom", "id",
68  &atom, &attr);
69  pj_strdup2(state_data->pool, &attr->value, state_data->exten);
70 
71  ast_sip_presence_xml_find_node_attr(state_data->pool, atom, "address",
72  "uri", &address, &attr);
73 
74  ast_sip_sanitize_xml(state_data->remote, sanitized, sizeof(sanitized));
75 
76  uri.ptr = (char*) pj_pool_alloc(state_data->pool,
77  strlen(sanitized) + STR_ADDR_PARAM.slen);
78  pj_strcpy2( &uri, sanitized);
79  pj_strcat( &uri, &STR_ADDR_PARAM);
80  pj_strdup(state_data->pool, &attr->value, &uri);
81 
82  ast_sip_presence_xml_create_attr(state_data->pool, address, "priority", "0.80000");
83 
84  ast_sip_presence_xml_find_node_attr(state_data->pool, address,
85  "status", "status", &status, &attr);
86  pj_strdup2(state_data->pool, &attr->value,
87  (local_state == NOTIFY_OPEN) ? "open" :
88  (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
89 
90  ast_sip_presence_xml_find_node_attr(state_data->pool, address,
91  "msnsubstatus", "substatus", &msnsubstatus, &attr);
92  pj_strdup2(state_data->pool, &attr->value,
93  (local_state == NOTIFY_OPEN) ? "online" :
94  (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
95 
96  return 0;
97 }
98 
99 #define MAX_STRING_GROWTHS 5
100 
101 static void xpidf_to_string(void *body, struct ast_str **str)
102 {
103  pjxpidf_pres *pres = body;
104  int growths = 0;
105  int size;
106 
107  do {
108  size = pjxpidf_print(pres, ast_str_buffer(*str), ast_str_size(*str) - 1);
109  if (size <= AST_PJSIP_XML_PROLOG_LEN) {
110  ast_str_make_space(str, ast_str_size(*str) * 2);
111  ++growths;
112  }
113  } while (size <= AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
114  if (size <= AST_PJSIP_XML_PROLOG_LEN) {
115  ast_log(LOG_WARNING, "XPIDF body text too large\n");
116  return;
117  }
118 
119  *(ast_str_buffer(*str) + size) = '\0';
120  ast_str_update(*str);
121 }
122 
124  .type = "application",
125  .subtype = "xpidf+xml",
126  .body_type = AST_SIP_EXTEN_STATE_DATA,
127  .allocate_body = xpidf_allocate_body,
128  .generate_body_content = xpidf_generate_body_content,
129  .to_string = xpidf_to_string,
130  /* No need for a destroy_body callback since we use a pool */
131 };
132 
134  .type = "application",
135  .subtype = "cpim-pidf+xml",
136  .body_type = AST_SIP_EXTEN_STATE_DATA,
137  .allocate_body = xpidf_allocate_body,
138  .generate_body_content = xpidf_generate_body_content,
139  .to_string = xpidf_to_string,
140  /* No need for a destroy_body callback since we use a pool */
141 };
142 
143 static void unregister_all(void)
144 {
145  ast_sip_pubsub_unregister_body_generator(&cpim_pidf_body_generator);
146  ast_sip_pubsub_unregister_body_generator(&xpidf_body_generator);
147 }
148 
149 static int load_module(void)
150 {
151  if (ast_sip_pubsub_register_body_generator(&xpidf_body_generator)) {
152  goto fail;
153  }
154 
155  if (ast_sip_pubsub_register_body_generator(&cpim_pidf_body_generator)) {
156  goto fail;
157  }
158 
160 
161 fail:
162  unregister_all();
164 }
165 
166 static int unload_module(void)
167 {
168  unregister_all();
169  return 0;
170 }
171 
172 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State PIDF Provider",
173  .support_level = AST_MODULE_SUPPORT_CORE,
174  .load = load_module,
175  .unload = unload_module,
176  .load_pri = AST_MODPRI_CHANNEL_DEPEND,
177  .requires = "res_pjsip,res_pjsip_pubsub",
178 );
Asterisk main include file. File version handling, generic pbx functions.
static int load_module(void)
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
char * address
Definition: f2c.h:59
static int xpidf_generate_body_content(void *body, void *data)
#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 struct ast_sip_pubsub_body_generator xpidf_body_generator
pj_xml_attr * ast_sip_presence_xml_create_attr(pj_pool_t *pool, pj_xml_node *node, const char *name, const char *value)
Create XML attribute.
Definition: presence_xml.c:140
static int unload_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
#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
static void xpidf_to_string(void *body, struct ast_str **str)
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
static void unregister_all(void)
#define AST_PJSIP_XML_PROLOG_LEN
Length of the XML prolog when printing presence or other XML in PJSIP.
static void * xpidf_allocate_body(void *data)
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
static const char name[]
Definition: cdr_mysql.c:74
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
static struct ast_sip_pubsub_body_generator cpim_pidf_body_generator
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
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
#define MAX_STRING_GROWTHS
ast_sip_pidf_state
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
void ast_sip_presence_xml_find_node_attr(pj_pool_t *pool, pj_xml_node *parent, const char *node_name, const char *attr_name, pj_xml_node **node, pj_xml_attr **attr)
Find an attribute within a given node.
Definition: presence_xml.c:172
jack_status_t status
Definition: app_jack.c:146
void ast_sip_pubsub_unregister_body_generator(struct ast_sip_pubsub_body_generator *generator)
Unregister a body generator with the pubsub core.