Debian Codegraph @

Xpjproject/pjsip/src/pjsip-ua/sip_xfer.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pjsip-ua/sip_xfer.h>
20 #include <pjsip-simple/evsub_msg.h>
21 #include <pjsip/sip_dialog.h>
22 #include <pjsip/sip_errno.h>
23 #include <pjsip/sip_endpoint.h>
24 #include <pjsip/sip_module.h>
25 #include <pjsip/sip_transport.h>
26 #include <pj/assert.h>
27 #include <pj/pool.h>
28 #include <pj/string.h>
29
30 /* Subscription expiration */
31 #ifndef PJSIP_XFER_EXPIRES
32 # define PJSIP_XFER_EXPIRES 600
33 #endif
34
35
36 /*
37 * Refer module (mod-refer)
38 */
39 static struct pjsip_module mod_xfer =
40 {
41 NULL, NULL, /* prev, next. */
42 { "mod-refer", 9 }, /* Name. */
43 -1, /* Id */
44 PJSIP_MOD_PRIORITY_DIALOG_USAGE, /* Priority */
45 NULL, /* load() */
46 NULL, /* start() */
47 NULL, /* stop() */
48 NULL, /* unload() */
49 NULL, /* on_rx_request() */
50 NULL, /* on_rx_response() */
51 NULL, /* on_tx_request. */
52 NULL, /* on_tx_response() */
53 NULL, /* on_tsx_state() */
54 };
55
56
57 /* Declare PJSIP_REFER_METHOD, so that if somebody declares this in
58 * sip_msg.h we can catch the error here.
59 */
60 enum
61 {
62 PJSIP_REFER_METHOD = PJSIP_OTHER_METHOD
63 };
64
65 PJ_DEF_DATA(const pjsip_method) pjsip_refer_method = {
66 (pjsip_method_e) PJSIP_REFER_METHOD,
67 { "REFER", 5}
68 };
69
70 PJ_DEF(const pjsip_method*) pjsip_get_refer_method()
71 {
72 return &pjsip_refer_method;
73 }
74
75 /*
76 * String constants
77 */
78 static const pj_str_t STR_REFER = { "refer", 5 };
79 static const pj_str_t STR_MESSAGE = { "message", 7 };
80 static const pj_str_t STR_SIPFRAG = { "sipfrag", 7 };
81
82
83 /*
84 * Transfer struct.
85 */
86 struct pjsip_xfer
87 {
88 pjsip_evsub *sub; /**< Event subscribtion record. */
89 pjsip_dialog *dlg; /**< The dialog. */
90 pjsip_evsub_user user_cb; /**< The user callback. */
91 pj_str_t refer_to_uri; /**< The full Refer-To URI. */
92 int last_st_code; /**< st_code sent in last NOTIFY */
93 pj_str_t last_st_text; /**< st_text sent in last NOTIFY */
94 };
95
96
97 typedef struct pjsip_xfer pjsip_xfer;
98
99
100
101 /*
102 * Forward decl for evsub callback.
103 */
104 static void xfer_on_evsub_state( pjsip_evsub *sub, pjsip_event *event);
105 static void xfer_on_evsub_tsx_state( pjsip_evsub *sub, pjsip_transaction *tsx,
106 pjsip_event *event);
107 static void xfer_on_evsub_rx_refresh( pjsip_evsub *sub,
108 pjsip_rx_data *rdata,
109 int *p_st_code,
110 pj_str_t **p_st_text,
111 pjsip_hdr *res_hdr,
112 pjsip_msg_body **p_body);
113 static void xfer_on_evsub_rx_notify( pjsip_evsub *sub,
114 pjsip_rx_data *rdata,
115 int *p_st_code,
116 pj_str_t **p_st_text,
117 pjsip_hdr *res_hdr,
118 pjsip_msg_body **p_body);
119 static void xfer_on_evsub_client_refresh(pjsip_evsub *sub);
120 static void xfer_on_evsub_server_timeout(pjsip_evsub *sub);
121
122
123 /*
124 * Event subscription callback for xference.
125 */
126 static pjsip_evsub_user xfer_user =
127 {
128 &xfer_on_evsub_state,
129 &xfer_on_evsub_tsx_state,
130 &xfer_on_evsub_rx_refresh,
131 &xfer_on_evsub_rx_notify,
132 &xfer_on_evsub_client_refresh,
133 &xfer_on_evsub_server_timeout,
134 };
135
136
137
138
139 /*
140 * Initialize the REFER subsystem.
141 */
142 PJ_DEF(pj_status_t) pjsip_xfer_init_module(pjsip_endpoint *endpt)
143 {
144 const pj_str_t accept = { "message/sipfrag;version=2.0", 27 };
145 pj_status_t status;
146
147 PJ_ASSERT_RETURN(endpt != NULL, PJ_EINVAL);
148 PJ_ASSERT_RETURN(mod_xfer.id == -1, PJ_EINVALIDOP);
149
150 status = pjsip_endpt_register_module(endpt, &mod_xfer);
151 if (status != PJ_SUCCESS)
152 return status;
153
154 status = pjsip_endpt_add_capability( endpt, &mod_xfer, PJSIP_H_ALLOW,
155 NULL, 1,
156 &pjsip_get_refer_method()->name);
157 if (status != PJ_SUCCESS)
158 return status;
159
160 status = pjsip_evsub_register_pkg(&mod_xfer, &STR_REFER,
161 PJSIP_XFER_EXPIRES, 1, &accept);
162 if (status != PJ_SUCCESS)
163 return status;
164
165 return PJ_SUCCESS;
166 }
167
168
169 /*
170 * Create transferer (sender of REFER request).
171 *
172 */
173 PJ_DEF(pj_status_t) pjsip_xfer_create_uac( pjsip_dialog *dlg,
174 const pjsip_evsub_user *user_cb,
175 pjsip_evsub **p_evsub )
176 {
177 pj_status_t status;
178 pjsip_xfer *xfer;
179 pjsip_evsub *sub;
180
181 PJ_ASSERT_RETURN(dlg && p_evsub, PJ_EINVAL);
182
183 pjsip_dlg_inc_lock(dlg);
184
185 /* Create event subscription */
186 status = pjsip_evsub_create_uac( dlg, &xfer_user, &STR_REFER,
187 PJSIP_EVSUB_NO_EVENT_ID, &sub);
188 if (status != PJ_SUCCESS)
189 goto on_return;
190
191 /* Create xfer session */
192 xfer = PJ_POOL_ZALLOC_T(dlg->pool, pjsip_xfer);
193 xfer->dlg = dlg;
194 xfer->sub = sub;
195 if (user_cb)
196 pj_memcpy(&xfer->user_cb, user_cb, sizeof(pjsip_evsub_user));
197
198 /* Attach to evsub */
199 pjsip_evsub_set_mod_data(sub, mod_xfer.id, xfer);
200
201 *p_evsub = sub;
202
203 on_return:
204 pjsip_dlg_dec_lock(dlg);
205 return status;
206
207 }
208
209
210
211
212 /*
213 * Create transferee (receiver of REFER request).
214 *
215 */
216 PJ_DEF(pj_status_t) pjsip_xfer_create_uas( pjsip_dialog *dlg,
217 const pjsip_evsub_user *user_cb,
218 pjsip_rx_data *rdata,
219 pjsip_evsub **p_evsub )
220 {
221 pjsip_evsub *sub;
222 pjsip_xfer *xfer;
223 const pj_str_t STR_EVENT = {"Event", 5 };
224 pjsip_event_hdr *event_hdr;
225 pj_status_t status;
226
227 /* Check arguments */
228 PJ_ASSERT_RETURN(dlg && rdata && p_evsub, PJ_EINVAL);
229
230 /* Must be request message */
231 PJ_ASSERT_RETURN(rdata->msg_info.msg->type == PJSIP_REQUEST_MSG,
232 PJSIP_ENOTREQUESTMSG);
233
234 /* Check that request is REFER */
235 PJ_ASSERT_RETURN(pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
236 pjsip_get_refer_method())==0,
237 PJSIP_ENOTREFER);
238
239 /* Lock dialog */
240 pjsip_dlg_inc_lock(dlg);
241
242 /* The evsub framework expects an Event header in the request,
243 * while a REFER request conveniently doesn't have one (pun intended!).
244 * So create a dummy Event header.
245 */
246 if (pjsip_msg_find_hdr_by_name(rdata->msg_info.msg,
247 &STR_EVENT, NULL)==NULL)
248 {
249 event_hdr = pjsip_event_hdr_create(rdata->tp_info.pool);
250 event_hdr->event_type = STR_REFER;
251 pjsip_msg_add_hdr(rdata->msg_info.msg, (pjsip_hdr*)event_hdr);
252 }
253
254 /* Create server subscription */
255 status = pjsip_evsub_create_uas( dlg, &xfer_user, rdata,
256 PJSIP_EVSUB_NO_EVENT_ID, &sub);
257 if (status != PJ_SUCCESS)
258 goto on_return;
259
260 /* Create server xfer subscription */
261 xfer = PJ_POOL_ZALLOC_T(dlg->pool, pjsip_xfer);
262 xfer->dlg = dlg;
263 xfer->sub = sub;
264 if (user_cb)
265 pj_memcpy(&xfer->user_cb, user_cb, sizeof(pjsip_evsub_user));
266
267 /* Attach to evsub */
268 pjsip_evsub_set_mod_data(sub, mod_xfer.id, xfer);
269
270 /* Done: */
271 *p_evsub = sub;
272
273 on_return:
274 pjsip_dlg_dec_lock(dlg);
275 return status;
276 }
277
278
279
280 /*
281 * Call this function to create request to initiate REFER subscription.
282 *
283 */
284 PJ_DEF(pj_status_t) pjsip_xfer_initiate( pjsip_evsub *sub,
285 const pj_str_t *refer_to_uri,
286 pjsip_tx_data **p_tdata)
287 {
288 pjsip_xfer *xfer;
289 const pj_str_t refer_to = { "Refer-To", 8};
290 pjsip_tx_data *tdata;
291 pjsip_generic_string_hdr *hdr;
292 pj_status_t status;
293
294 /* sub and p_tdata argument must be valid. */
295 PJ_ASSERT_RETURN(sub && p_tdata, PJ_EINVAL);
296
297
298 /* Get the xfer object. */
299 xfer = (pjsip_xfer*) pjsip_evsub_get_mod_data(sub, mod_xfer.id);
300 PJ_ASSERT_RETURN(xfer != NULL, PJSIP_ENOREFERSESSION);
301
302 /* refer_to_uri argument MAY be NULL for subsequent REFER requests,
303 * but it MUST be specified in the first REFER.
304 */
305 PJ_ASSERT_RETURN((refer_to_uri || xfer->refer_to_uri.slen), PJ_EINVAL);
306
307 /* Lock dialog. */
308 pjsip_dlg_inc_lock(xfer->dlg);
309
310 /* Create basic REFER request */
311 status = pjsip_evsub_initiate(sub, pjsip_get_refer_method(),
312 PJSIP_EXPIRES_NOT_SPECIFIED, &tdata);
313 if (status != PJ_SUCCESS)
314 goto on_return;
315
316 /* Save Refer-To URI. */
317 if (refer_to_uri == NULL) {
318 refer_to_uri = &xfer->refer_to_uri;
319 } else {
320 pj_strdup(xfer->dlg->pool, &xfer->refer_to_uri, refer_to_uri);
321 }
322
323 /* Create and add Refer-To header. */
324 hdr = pjsip_generic_string_hdr_create(tdata->pool, &refer_to,
325 refer_to_uri);
326 if (!hdr) {
327 pjsip_tx_data_dec_ref(tdata);
328 status = PJ_ENOMEM;
329 goto on_return;
330 }
331
332 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)hdr);
333
334
335 /* Done. */
336 *p_tdata = tdata;
337
338 status = PJ_SUCCESS;
339
340 on_return:
341 pjsip_dlg_dec_lock(xfer->dlg);
342 return status;
343 }
344
345
346 /*
347 * Accept the incoming REFER request by sending 2xx response.
348 *
349 */
350 PJ_DEF(pj_status_t) pjsip_xfer_accept( pjsip_evsub *sub,
351 pjsip_rx_data *rdata,
352 int st_code,
353 const pjsip_hdr *hdr_list )
354 {
355 /*
356 * Don't need to add custom headers, so just call basic
357 * evsub response.
358 */
359 return pjsip_evsub_accept( sub, rdata, st_code, hdr_list );
360 }
361
362
363 /*
364 * For notifier, create NOTIFY request to subscriber, and set the state
365 * of the subscription.
366 */
367 PJ_DEF(pj_status_t) pjsip_xfer_notify( pjsip_evsub *sub,
368 pjsip_evsub_state state,
369 int xfer_st_code,
370 const pj_str_t *xfer_st_text,
371 pjsip_tx_data **p_tdata)
372 {
373 pjsip_tx_data *tdata;
374 pjsip_xfer *xfer;
375 pjsip_param *param;
376 const pj_str_t reason = { "noresource", 10 };
377 char *body;
378 int bodylen;
379 pjsip_msg_body *msg_body;
380 pj_status_t status;
381
382
383 /* Check arguments. */
384 PJ_ASSERT_RETURN(sub, PJ_EINVAL);
385
386 /* Get the xfer object. */
387 xfer = (pjsip_xfer*) pjsip_evsub_get_mod_data(sub, mod_xfer.id);
388 PJ_ASSERT_RETURN(xfer != NULL, PJSIP_ENOREFERSESSION);
389
390
391 /* Lock object. */
392 pjsip_dlg_inc_lock(xfer->dlg);
393
394 /* Create the NOTIFY request.
395 * Note that reason is only used when state is TERMINATED, and
396 * the defined termination reason for REFER is "noresource".
397 */
398 status = pjsip_evsub_notify( sub, state, NULL, &reason, &tdata);
399 if (status != PJ_SUCCESS)
400 goto on_return;
401
402
403 /* Check status text */
404 if (xfer_st_text==NULL || xfer_st_text->slen==0)
405 xfer_st_text = pjsip_get_status_text(xfer_st_code);
406
407 /* Save st_code and st_text, for current_notify() */
408 xfer->last_st_code = xfer_st_code;
409 pj_strdup(xfer->dlg->pool, &xfer->last_st_text, xfer_st_text);
410
411 /* Create sipfrag content. */
412 body = (char*) pj_pool_alloc(tdata->pool, 128);
413 bodylen = pj_ansi_snprintf(body, 128, "SIP/2.0 %u %.*s\r\n",
414 xfer_st_code,
415 (int)xfer_st_text->slen,
416 xfer_st_text->ptr);
417 PJ_ASSERT_ON_FAIL(bodylen > 0 && bodylen < 128,
418 {status=PJ_EBUG; pjsip_tx_data_dec_ref(tdata);
419 goto on_return; });
420
421
422 /* Create SIP message body. */
423 msg_body = PJ_POOL_ZALLOC_T(tdata->pool, pjsip_msg_body);
424 pjsip_media_type_init(&msg_body->content_type, (pj_str_t*)&STR_MESSAGE,
425 (pj_str_t*)&STR_SIPFRAG);
426 msg_body->data = body;
427 msg_body->len = bodylen;
428 msg_body->print_body = &pjsip_print_text_body;
429 msg_body->clone_data = &pjsip_clone_text_data;
430
431 param = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);
432 param->name = pj_str("version");
433 param->value = pj_str("2.0");
434 pj_list_push_back(&msg_body->content_type.param, param);
435
436 /* Attach sipfrag body. */
437 tdata->msg->body = msg_body;
438
439
440 /* Done. */
441 *p_tdata = tdata;
442
443
444 on_return:
445 pjsip_dlg_dec_lock(xfer->dlg);
446 return status;
447
448 }
449
450
451 /*
452 * Send current state and the last sipfrag body.
453 */
454 PJ_DEF(pj_status_t) pjsip_xfer_current_notify( pjsip_evsub *sub,
455 pjsip_tx_data **p_tdata )
456 {
457 pjsip_xfer *xfer;
458 pj_status_t status;
459
460
461 /* Check arguments. */
462 PJ_ASSERT_RETURN(sub, PJ_EINVAL);
463
464 /* Get the xfer object. */
465 xfer = (pjsip_xfer*) pjsip_evsub_get_mod_data(sub, mod_xfer.id);
466 PJ_ASSERT_RETURN(xfer != NULL, PJSIP_ENOREFERSESSION);
467
468 pjsip_dlg_inc_lock(xfer->dlg);
469
470 status = pjsip_xfer_notify(sub, pjsip_evsub_get_state(sub),
471 xfer->last_st_code, &xfer->last_st_text,
472 p_tdata);
473
474 pjsip_dlg_dec_lock(xfer->dlg);
475
476 return status;
477 }
478
479
480 /*
481 * Send request message.
482 */
483 PJ_DEF(pj_status_t) pjsip_xfer_send_request( pjsip_evsub *sub,
484 pjsip_tx_data *tdata)
485 {
486 return pjsip_evsub_send_request(sub, tdata);
487 }
488
489
490 /*
491 * This callback is called by event subscription when subscription
492 * state has changed.
493 */
494 static void xfer_on_evsub_state( pjsip_evsub *sub, pjsip_event *event)
495 {
496 pjsip_xfer *xfer;
497
498 xfer = (pjsip_xfer*) pjsip_evsub_get_mod_data(sub, mod_xfer.id);
499 PJ_ASSERT_ON_FAIL(xfer!=NULL, {return;});
500
501 if (xfer->user_cb.on_evsub_state)
502 (*xfer->user_cb.on_evsub_state)(sub, event);
503
504 }
505
506 /*
507 * Called when transaction state has changed.
508 */
509 static void xfer_on_evsub_tsx_state( pjsip_evsub *sub, pjsip_transaction *tsx,
510 pjsip_event *event)
511 {
512 pjsip_xfer *xfer;
513
514 xfer = (pjsip_xfer*) pjsip_evsub_get_mod_data(sub, mod_xfer.id);
515 PJ_ASSERT_ON_FAIL(xfer!=NULL, {return;});
516
517 if (xfer->user_cb.on_tsx_state)
518 (*xfer->user_cb.on_tsx_state)(sub, tsx, event);
519 }
520
521 /*
522 * Called when REFER is received to refresh subscription.
523 */
524 static void xfer_on_evsub_rx_refresh( pjsip_evsub *sub,
525 pjsip_rx_data *rdata,
526 int *p_st_code,
527 pj_str_t **p_st_text,
528 pjsip_hdr *res_hdr,
529 pjsip_msg_body **p_body)
530 {
531 pjsip_xfer *xfer;
532
533 xfer = (pjsip_xfer*) pjsip_evsub_get_mod_data(sub, mod_xfer.id);
534 PJ_ASSERT_ON_FAIL(xfer!=NULL, {return;});
535
536 if (xfer->user_cb.on_rx_refresh) {
537 (*xfer->user_cb.on_rx_refresh)(sub, rdata, p_st_code, p_st_text,
538 res_hdr, p_body);
539
540 } else {
541 /* Implementors MUST send NOTIFY if it implements on_rx_refresh
542 * (implementor == "us" from evsub point of view.
543 */
544 pjsip_tx_data *tdata;
545 pj_status_t status;
546
547 if (pjsip_evsub_get_state(sub)==PJSIP_EVSUB_STATE_TERMINATED) {
548 status = pjsip_xfer_notify( sub, PJSIP_EVSUB_STATE_TERMINATED,
549 xfer->last_st_code,
550 &xfer->last_st_text,
551 &tdata);
552 } else {
553 status = pjsip_xfer_current_notify(sub, &tdata);
554 }
555
556 if (status == PJ_SUCCESS)
557 pjsip_xfer_send_request(sub, tdata);
558 }
559 }
560
561
562 /*
563 * Called when NOTIFY is received.
564 */
565 static void xfer_on_evsub_rx_notify( pjsip_evsub *sub,
566 pjsip_rx_data *rdata,
567 int *p_st_code,
568 pj_str_t **p_st_text,
569 pjsip_hdr *res_hdr,
570 pjsip_msg_body **p_body)
571 {
572 pjsip_xfer *xfer;
573
574 xfer = (pjsip_xfer*) pjsip_evsub_get_mod_data(sub, mod_xfer.id);
575 PJ_ASSERT_ON_FAIL(xfer!=NULL, {return;});
576
577 if (xfer->user_cb.on_rx_notify)
578 (*xfer->user_cb.on_rx_notify)(sub, rdata, p_st_code, p_st_text,
579 res_hdr, p_body);
580 }
581
582 /*
583 * Called when it's time to send SUBSCRIBE.
584 */
585 static void xfer_on_evsub_client_refresh(pjsip_evsub *sub)
586 {
587 pjsip_xfer *xfer;
588
589 xfer = (pjsip_xfer*) pjsip_evsub_get_mod_data(sub, mod_xfer.id);
590 PJ_ASSERT_ON_FAIL(xfer!=NULL, {return;});
591
592 if (xfer->user_cb.on_client_refresh) {
593 (*xfer->user_cb.on_client_refresh)(sub);
594 } else {
595 pj_status_t status;
596 pjsip_tx_data *tdata;
597
598 status = pjsip_evsub_initiate(sub, NULL, PJSIP_XFER_EXPIRES, &tdata);
599 if (status == PJ_SUCCESS)
600 pjsip_xfer_send_request(sub, tdata);
601 }
602 }
603
604
605 /*
606 * Called when no refresh is received after the interval.
607 */
608 static void xfer_on_evsub_server_timeout(pjsip_evsub *sub)
609 {
610 pjsip_xfer *xfer;
611
612 xfer = (pjsip_xfer*) pjsip_evsub_get_mod_data(sub, mod_xfer.id);
613 PJ_ASSERT_ON_FAIL(xfer!=NULL, {return;});
614
615 if (xfer->user_cb.on_server_timeout) {
616 (*xfer->user_cb.on_server_timeout)(sub);
617 } else {
618 pj_status_t status;
619 pjsip_tx_data *tdata;
620
621 status = pjsip_xfer_notify(sub, PJSIP_EVSUB_STATE_TERMINATED,
622 xfer->last_st_code,
623 &xfer->last_st_text, &tdata);
624 if (status == PJ_SUCCESS)
625 pjsip_xfer_send_request(sub, tdata);
626 }
627 }
628