1 /*
2 * This file is part of the Sofia-SIP package
3 *
4 * Copyright (C) 2006 Nokia Corporation.
5 *
6 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 */
24
25 /**@CFILE nua_server.c
26 * @brief Server transaction handling
27 *
28 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
29 *
30 * @date Created: Tue Feb 3 16:10:45 EET 2009
31 */
32
33 #include "config.h"
34
35 #include <stddef.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <limits.h>
39
40 #include <assert.h>
41
42 #include <sofia-sip/su_string.h>
43 #include <sofia-sip/su_tagarg.h>
44
45 #include <sofia-sip/sip_protos.h>
46 #include <sofia-sip/sip_status.h>
47 #include <sofia-sip/sip_util.h>
48
49 #define NUA_SAVED_EVENT_T su_msg_t *
50 #define NUA_SAVED_SIGNAL_T su_msg_t *
51
52 #include <sofia-sip/su_wait.h>
53
54 #include "nua_stack.h"
55 #include "nua_dialog.h"
56 #include "nua_server.h"
57 #include "nua_params.h"
58
59 /* ======================================================================== */
60 /*
61 * Process incoming requests
62 */
63
64 nua_server_methods_t const *nua_server_methods[] = {
65 /* These must be in same order as in sip_method_t */
66 &nua_extension_server_methods,
67 &nua_invite_server_methods, /**< INVITE */
68 NULL, /**< ACK */
69 NULL, /**< CANCEL */
70 &nua_bye_server_methods, /**< BYE */
71 &nua_options_server_methods, /**< OPTIONS */
72 &nua_register_server_methods, /**< REGISTER */
73 &nua_info_server_methods, /**< INFO */
74 &nua_prack_server_methods, /**< PRACK */
75 &nua_update_server_methods, /**< UPDATE */
76 &nua_message_server_methods, /**< MESSAGE */
77 &nua_subscribe_server_methods,/**< SUBSCRIBE */
78 &nua_notify_server_methods, /**< NOTIFY */
79 &nua_refer_server_methods, /**< REFER */
80 &nua_publish_server_methods, /**< PUBLISH */
81 NULL
82 };
83
84
nua_stack_process_request(nua_handle_t * nh,nta_leg_t * leg,nta_incoming_t * irq,sip_t const * sip)85 int nua_stack_process_request(nua_handle_t *nh,
86 nta_leg_t *leg,
87 nta_incoming_t *irq,
88 sip_t const *sip)
89 {
90 nua_t *nua = nh->nh_nua;
91 sip_method_t method = sip->sip_request->rq_method;
92 char const *name = sip->sip_request->rq_method_name;
93 nua_server_methods_t const *sm;
94 nua_server_request_t *sr, sr0[1];
95 int status, initial = 1;
96 int create_dialog;
97
98 char const *user_agent = NH_PGET(nh, user_agent);
99 sip_supported_t const *supported = NH_PGET(nh, supported);
100 sip_allow_t const *allow = NH_PGET(nh, allow);
101
102 enter;
103
104 nta_incoming_tag(irq, NULL);
105
106 if (method == sip_method_cancel)
107 return 481;
108
109 /* Hook to outbound */
110 if (method == sip_method_options) {
111 status = nua_registration_process_request(nua->nua_registrations,
112 irq, sip);
113 if (status)
114 return status;
115 }
116
117 if (nta_check_method(irq, sip, allow,
118 SIPTAG_SUPPORTED(supported),
119 SIPTAG_USER_AGENT_STR(user_agent),
120 TAG_END()))
121 return 405;
122
123 switch (sip->sip_request->rq_url->url_type) {
124 case url_sip:
125 case url_sips:
126 case url_im:
127 case url_pres:
128 case url_tel:
129 break;
130 default:
131 nta_incoming_treply(irq, status = SIP_416_UNSUPPORTED_URI,
132 SIPTAG_ALLOW(allow),
133 SIPTAG_SUPPORTED(supported),
134 SIPTAG_USER_AGENT_STR(user_agent),
135 TAG_END());
136 return status;
137 }
138
139 if (nta_check_required(irq, sip, supported,
140 SIPTAG_ALLOW(allow),
141 SIPTAG_USER_AGENT_STR(user_agent),
142 TAG_END()))
143 return 420;
144
145 if (method > sip_method_unknown && method <= sip_method_publish)
146 sm = nua_server_methods[method];
147 else
148 sm = nua_server_methods[0];
149
150 initial = nh == nua->nua_dhandle;
151
152 if (sm == NULL) {
153 SU_DEBUG_1(("nua(%p): strange %s from <" URL_PRINT_FORMAT ">\n",
154 (void *)nh, sip->sip_request->rq_method_name,
155 URL_PRINT_ARGS(sip->sip_from->a_url)));
156 }
157 else if (initial && sm->sm_flags.in_dialog) {
158 /* These must be in-dialog */
159 sm = NULL;
160 }
161 else if (initial && sip->sip_to->a_tag && method != sip_method_subscribe) {
162 /* RFC 3261 section 12.2.2:
163
164 If the UAS wishes to reject the request because it does not wish to
165 recreate the dialog, it MUST respond to the request with a 481
166 (Call/Transaction Does Not Exist) status code and pass that to the
167 server transaction.
168 */ /* we allow this on subscribes because we have disabled the built-in notify server and we need those messages in the application layer */
169
170 if (method == sip_method_info)
171 /* accept out-of-dialog info */; else
172 if (method != sip_method_message || !NH_PGET(nh, win_messenger_enable))
173 sm = NULL;
174 }
175
176 if (!sm) {
177 nta_incoming_treply(irq,
178 status = 481, "Call Does Not Exist",
179 SIPTAG_ALLOW(allow),
180 SIPTAG_SUPPORTED(supported),
181 SIPTAG_USER_AGENT_STR(user_agent),
182 TAG_END());
183 return 481;
184 }
185
186 create_dialog = sm->sm_flags.create_dialog;
187 if (method == sip_method_message && NH_PGET(nh, win_messenger_enable))
188 create_dialog = 1;
189 sr = memset(sr0, 0, (sizeof sr0));
190
191 sr->sr_methods = sm;
192 sr->sr_method = method = sip->sip_request->rq_method;
193 sr->sr_add_contact = sm->sm_flags.add_contact;
194 sr->sr_target_refresh = sm->sm_flags.target_refresh;
195
196 sr->sr_owner = nh;
197 sr->sr_initial = initial;
198
199 sr->sr_irq = irq;
200
201 SR_STATUS1(sr, SIP_100_TRYING);
202
203 sr->sr_request.msg = nta_incoming_getrequest(irq);
204 sr->sr_request.sip = sip;
205 assert(sr->sr_request.msg);
206
207 sr->sr_response.msg = nta_incoming_create_response(irq, 0, NULL);
208 sr->sr_response.sip = sip_object(sr->sr_response.msg);
209
210 if (sr->sr_response.msg == NULL) {
211 SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
212 }
213 else if (sm->sm_init && sm->sm_init(sr)) {
214 if (sr->sr_status < 200) /* Init may have set response status */
215 SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
216 }
217 /* Create handle if request does not fail */
218 else if (initial && sr->sr_status < 300) {
219 if ((nh = nua_stack_incoming_handle(nua, irq, sip, create_dialog)))
220 sr->sr_owner = nh;
221 else
222 SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
223 }
224
225 if (sr->sr_status < 300 && sm->sm_preprocess && sm->sm_preprocess(sr)) {
226 if (sr->sr_status < 200) /* Set response status if preprocess did not */
227 SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
228 }
229
230 if (sr->sr_status < 300) {
231 if (sr->sr_target_refresh)
232 nua_dialog_uas_route(nh, nh->nh_ds, sip, 1); /* Set route and tags */
233 nua_dialog_store_peer_info(nh, nh->nh_ds, sip);
234 }
235
236 if (sr->sr_status == 100 && method != sip_method_unknown &&
237 !sip_is_allowed(NH_PGET(sr->sr_owner, appl_method), method, name)) {
238 if (method == sip_method_refer || method == sip_method_subscribe)
239 SR_STATUS1(sr, SIP_202_ACCEPTED);
240 else
241 SR_STATUS1(sr, SIP_200_OK);
242 }
243
244 /* INVITE server request is not finalized after 2XX response */
245 if (sr->sr_status < (method == sip_method_invite ? 300 : 200)) {
246 sr = su_alloc(nh->nh_home, (sizeof *sr));
247
248 if (sr) {
249 *sr = *sr0;
250
251 if ((sr->sr_next = nh->nh_ds->ds_sr))
252 *(sr->sr_prev = sr->sr_next->sr_prev) = sr,
253 sr->sr_next->sr_prev = &sr->sr_next;
254 else
255 *(sr->sr_prev = &nh->nh_ds->ds_sr) = sr;
256 }
257 else {
258 sr = sr0;
259 SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
260 }
261 }
262
263 if (sr->sr_status <= 100) {
264 SR_STATUS1(sr, SIP_100_TRYING);
265 if ((method == sip_method_invite && nh->nh_prefs->nhp_auto_invite_100) ||
266 sip->sip_timestamp) {
267 nta_incoming_treply(irq, SIP_100_TRYING,
268 SIPTAG_USER_AGENT_STR(user_agent),
269 TAG_END());
270
271 }
272 }
273 else {
274 /* Note that this may change the sr->sr_status */
275 nua_server_respond(sr, NULL);
276 }
277
278 if (nua_server_report(sr) == 0)
279 return 0;
280
281 return 501;
282 }
283
284 #undef nua_base_server_init
285 #undef nua_base_server_preprocess
286
nua_base_server_init(nua_server_request_t * sr)287 int nua_base_server_init(nua_server_request_t *sr)
288 {
289 return 0;
290 }
291
nua_base_server_preprocess(nua_server_request_t * sr)292 int nua_base_server_preprocess(nua_server_request_t *sr)
293 {
294 return 0;
295 }
296
nua_server_request_destroy(nua_server_request_t * sr)297 void nua_server_request_destroy(nua_server_request_t *sr)
298 {
299 nua_server_request_t *sr0 = NULL;
300
301 if (sr == NULL)
302 return;
303
304 if (SR_HAS_SAVED_SIGNAL(sr))
305 nua_destroy_signal(sr->sr_signal);
306
307 if (sr->sr_prev) {
308 /* Allocated from heap */
309 if ((*sr->sr_prev = sr->sr_next))
310 sr->sr_next->sr_prev = sr->sr_prev;
311 sr0 = sr;
312 }
313
314 if (sr->sr_irq) {
315 nta_incoming_t *irq = sr->sr_irq;
316 if (sr->sr_method == sip_method_bye && sr->sr_status < 200) {
317 nta_incoming_treply(sr->sr_irq, SIP_200_OK, TAG_END());
318 }
319 sr->sr_irq = NULL;
320 nta_incoming_destroy(irq);
321 }
322
323 if (sr->sr_request.msg) {
324 msg_t *msg = sr->sr_request.msg;
325 sr->sr_request.msg = NULL;
326 msg_destroy(msg);
327 }
328
329 if (sr->sr_response.msg) {
330 msg_t *msg = sr->sr_response.msg;
331 sr->sr_response.msg = NULL;
332 msg_destroy(msg);
333 }
334
335 if (sr0) su_free(sr->sr_owner->nh_home, sr0);
336 }
337
338 /**@fn void nua_respond(nua_handle_t *nh, int status, char const *phrase, tag_type_t tag, tag_value_t value, ...);
339 *
340 * Respond to a request with given status code and phrase.
341 *
342 * The stack returns a SIP response message with given status code and
343 * phrase to the client. The tagged parameter list can specify extra headers
344 * to include with the response message and other stack parameters. The SIP
345 * session or other protocol state associated with the handle is updated
346 * accordingly (for instance, if an initial INVITE is responded with 200, a
347 * SIP session is established.)
348 *
349 * When responding to an incoming INVITE request, the nua_respond() can be
350 * called without NUTAG_WITH() (or NUTAG_WITH_CURRENT() or
351 * NUTAG_WITH_SAVED()). Otherwise, NUTAG_WITH() will contain an indication
352 * of the request being responded.
353 *
354 * @param nh Pointer to operation handle
355 * @param status SIP response status code (see RFCs of SIP)
356 * @param phrase free text (default response phrase is used if NULL)
357 * @param tag, value, ... List of tagged parameters
358 *
359 * @return
360 * nothing
361 *
362 * @par Responses by Protocol Engine
363 *
364 * When nua protocol engine receives an incoming SIP request, it can either
365 * respond to the request automatically or let application to respond to the
366 * request. The automatic response is returned to the client if the request
367 * fails syntax check, or the method, SIP extension or content negotiation
368 * fails.
369 *
370 * When the @ref nua_handlingevents "request event" is delivered to the
371 * application, the application should examine the @a status parameter. The
372 * @a status parameter is 200 or greater if the request has been already
373 * responded automatically by the stack.
374 *
375 * The application can add methods that it likes to handle by itself with
376 * NUTAG_APPL_METHOD(). The default set of NUTAG_APPL_METHOD() includes
377 * INVITE, PUBLISH, REGISTER and SUBSCRIBE. Note that unless the method is
378 * also included in the set of allowed methods with NUTAG_ALLOW(), the stack
379 * will respond to the incoming methods with <i>405 Not Allowed</i>.
380 *
381 * In order to simplify the simple applications, most requests are responded
382 * automatically. The BYE and CANCEL requests are always responded by the
383 * stack. Likewise, the NOTIFY requests associated with an event
384 * subscription are responded by the stack.
385 *
386 * Note that certain methods are rejected outside a SIP session (created
387 * with INVITE transaction). They include BYE, UPDATE, PRACK and INFO. Also
388 * the auxiliary methods ACK and CANCEL are rejected by the stack if there
389 * is no ongoing INVITE transaction corresponding to them.
390 *
391 * @par Related Tags:
392 * NUTAG_WITH(), NUTAG_WITH_THIS(), NUTAG_WITH_SAVED() \n
393 * NUTAG_EARLY_ANSWER() \n
394 * SOATAG_ADDRESS() \n
395 * SOATAG_AF() \n
396 * SOATAG_HOLD() \n
397 * Tags used with nua_set_hparams() \n
398 * Header tags defined in <sofia-sip/sip_tag.h>.
399 *
400 * @par Events:
401 * #nua_i_state \n
402 * #nua_i_media_error \n
403 * #nua_i_error \n
404 * #nua_i_active \n
405 * #nua_i_terminated \n
406 *
407 * @sa #nua_i_invite, #nua_i_register, #nua_i_subscribe, #nua_i_publish
408 */
409
410 void
nua_stack_respond(nua_t * nua,nua_handle_t * nh,int status,char const * phrase,tagi_t const * tags)411 nua_stack_respond(nua_t *nua, nua_handle_t *nh,
412 int status, char const *phrase, tagi_t const *tags)
413 {
414 nua_server_request_t *sr;
415 tagi_t const *t;
416 msg_t const *request = NULL;
417
418 t = tl_find_last(tags, nutag_with);
419
420 if (t)
421 request = (msg_t const *)t->t_value;
422
423 for (sr = nh->nh_ds->ds_sr; sr; sr = sr->sr_next) {
424 if (request && sr->sr_request.msg == request)
425 break;
426 /* nua_respond() to INVITE can be used without NUTAG_WITH() */
427 if (!t && sr->sr_method == sip_method_invite)
428 break;
429 }
430
431 if (sr == NULL) {
432 nua_stack_event(nua, nh, NULL, nua_i_error,
433 500, "Responding to a Non-Existing Request", NULL);
434 return;
435 }
436 else if (!nua_server_request_is_pending(sr)) {
437 nua_stack_event(nua, nh, NULL, nua_i_error,
438 500, "Already Sent Final Response", NULL);
439 return;
440 }
441 else if (sr->sr_100rel && !sr->sr_pracked && 200 <= status && status < 300) {
442 /* Save signal until we have received PRACK */
443 if (tags && nua_stack_set_params(nua, nh, nua_i_none, tags) < 0) {
444 sr->sr_application = status;
445 SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
446 }
447 else {
448 su_msg_save(sr->sr_signal, nh->nh_nua->nua_signal);
449 return;
450 }
451 }
452 else {
453 sr->sr_application = status;
454 if (tags && nua_stack_set_params(nua, nh, nua_i_none, tags) < 0)
455 SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
456 else {
457 sr->sr_status = status, sr->sr_phrase = phrase;
458 }
459 }
460
461 nua_server_params(sr, tags);
462 nua_server_respond(sr, tags);
463
464 if (!(sr->sr_method == sip_method_invite && status == 100)) {
465 /* Since we don't change state, do not notify application when
466 we send 100 Trying for INVITE */
467 nua_server_report(sr);
468 }
469 }
470
nua_server_params(nua_server_request_t * sr,tagi_t const * tags)471 int nua_server_params(nua_server_request_t *sr, tagi_t const *tags)
472 {
473 if (sr->sr_methods->sm_params)
474 return sr->sr_methods->sm_params(sr, tags);
475 return 0;
476 }
477
478 #undef nua_base_server_params
479
nua_base_server_params(nua_server_request_t * sr,tagi_t const * tags)480 int nua_base_server_params(nua_server_request_t *sr, tagi_t const *tags)
481 {
482 return 0;
483 }
484
485 /** Return the response to the client.
486 *
487 * @retval 0 when successfully sent
488 * @retval -1 upon an error
489 */
nua_server_trespond(nua_server_request_t * sr,tag_type_t tag,tag_value_t value,...)490 int nua_server_trespond(nua_server_request_t *sr,
491 tag_type_t tag, tag_value_t value, ...)
492 {
493 int retval;
494 ta_list ta;
495 ta_start(ta, tag, value);
496 retval = nua_server_respond(sr, ta_args(ta));
497 ta_end(ta);
498 return retval;
499 }
500
501 /** Return the response to the client.
502 *
503 * @retval 0 when successfully sent
504 * @retval -1 upon an error
505 */
nua_server_respond(nua_server_request_t * sr,tagi_t const * tags)506 int nua_server_respond(nua_server_request_t *sr, tagi_t const *tags)
507 {
508 nua_handle_t *nh = sr->sr_owner;
509 nua_dialog_state_t *ds = nh->nh_ds;
510 sip_method_t method = sr->sr_method;
511 struct { msg_t *msg; sip_t *sip; } next = { NULL, NULL };
512 int retval, user_contact = 1;
513 #if HAVE_OPEN_C
514 /* Nice. And old arm symbian compiler; see below. */
515 tagi_t next_tags[2];
516 #else
517 tagi_t next_tags[2] = {{ SIPTAG_END() }, { TAG_NEXT(tags) }};
518 #endif
519
520 msg_t *msg = sr->sr_response.msg;
521 sip_t *sip = sr->sr_response.sip;
522 sip_contact_t *m = sr->sr_request.sip->sip_contact;
523
524 #if HAVE_OPEN_C
525 next_tags[0].t_tag = siptag_end;
526 next_tags[0].t_value = (tag_value_t)0;
527 next_tags[1].t_tag = tag_next;
528 next_tags[1].t_value = (tag_value_t)(tags);
529 #endif
530
531 if (sr->sr_response.msg == NULL) {
532 //assert(sr->sr_status == 500);
533 SU_DEBUG_0(("sr without msg, sr_status=%u", sr->sr_status));
534 goto internal_error;
535 }
536
537 if (sr->sr_status == 100) {
538 return nta_incoming_treply(sr->sr_irq, SIP_100_TRYING,
539 SIPTAG_USER_AGENT_STR(NH_PGET(nh, user_agent)),
540 TAG_END());
541 return 0;
542 }
543
544 if (sr->sr_status < 200) {
545 next.msg = nta_incoming_create_response(sr->sr_irq, 0, NULL);
546 next.sip = sip_object(next.msg);
547 if (next.sip == NULL)
548 SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
549 }
550
551 if (nta_incoming_complete_response(sr->sr_irq, msg,
552 sr->sr_status,
553 sr->sr_phrase,
554 TAG_NEXT(tags)) < 0)
555 ;
556 else if (!sip->sip_supported && NH_PGET(nh, supported) &&
557 sip_add_dup(msg, sip, (sip_header_t *)NH_PGET(nh, supported)) < 0)
558 ;
559 else if (!sip->sip_user_agent && NH_PGET(nh, user_agent) &&
560 sip_add_make(msg, sip, sip_user_agent_class,
561 NH_PGET(nh, user_agent)) < 0)
562 ;
563 else if (!sip->sip_organization && NH_PGET(nh, organization) &&
564 sip_add_make(msg, sip, sip_organization_class,
565 NH_PGET(nh, organization)) < 0)
566 ;
567 else if (!sip->sip_via && NH_PGET(nh, via) &&
568 sip_add_make(msg, sip, sip_via_class,
569 NH_PGET(nh, via)) < 0)
570 ;
571 else if (!sip->sip_allow && NH_PGET(nh, allow) &&
572 sip_add_dup(msg, sip, (void *)NH_PGET(nh, allow)) < 0)
573 ;
574 else if (!sip->sip_allow_events &&
575 NH_PGET(nh, allow_events) &&
576 (method == sip_method_publish || method == sip_method_subscribe ||
577 method == sip_method_options || method == sip_method_refer ||
578 (sr->sr_initial &&
579 (method == sip_method_invite ||
580 method == sip_method_notify))) &&
581 sip_add_dup(msg, sip, (void *)NH_PGET(nh, allow_events)) < 0)
582 ;
583 else if (!sip->sip_contact && sr->sr_status < 300 && sr->sr_add_contact &&
584 (user_contact = 0,
585 ds->ds_ltarget
586 ? sip_add_dup(msg, sip, (sip_header_t *)ds->ds_ltarget)
587 : nua_registration_add_contact_to_response(nh, msg, sip, NULL, m))
588 < 0)
589 ;
590 else {
591 int term;
592 sip_contact_t *ltarget = NULL;
593
594 term = sip_response_terminates_dialog(sr->sr_status, sr->sr_method, NULL);
595
596 sr->sr_terminating = (term < 0) ? -1 : (term > 0 || sr->sr_terminating);
597
598 if (sr->sr_target_refresh && sr->sr_status < 300 && !sr->sr_terminating &&
599 user_contact && sip->sip_contact) {
600 /* Save Contact given by application */
601 ltarget = sip_contact_dup(nh->nh_home, sip->sip_contact);
602 }
603
604 retval = sr->sr_methods->sm_respond(sr, next_tags);
605
606 if (sr->sr_status < 200)
607 sr->sr_response.msg = next.msg, sr->sr_response.sip = next.sip;
608 else if (next.msg)
609 msg_destroy(next.msg);
610
611 assert(sr->sr_status >= 200 || sr->sr_response.msg);
612
613 if (ltarget) {
614 if (sr->sr_status < 300) {
615 nua_dialog_state_t *ds = nh->nh_ds;
616 msg_header_free(nh->nh_home, (msg_header_t *)ds->ds_ltarget);
617 ds->ds_ltarget = ltarget;
618 }
619 else
620 msg_header_free(nh->nh_home, (msg_header_t *)ltarget);
621 }
622
623 return retval;
624 }
625
626 if (next.msg)
627 msg_destroy(next.msg);
628
629 SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
630
631 msg_destroy(msg);
632
633 internal_error:
634 sr->sr_response.msg = NULL, sr->sr_response.sip = NULL;
635 nta_incoming_treply(sr->sr_irq, sr->sr_status, sr->sr_phrase, TAG_END());
636
637 return 0;
638 }
639
640 /** Return the response to the client.
641 *
642 * @retval 0 when successfully sent
643 * @retval -1 upon an error
644 */
nua_base_server_respond(nua_server_request_t * sr,tagi_t const * tags)645 int nua_base_server_respond(nua_server_request_t *sr, tagi_t const *tags)
646 {
647 msg_t *response = sr->sr_response.msg;
648 sip_t *sip = sr->sr_response.sip;
649
650 sr->sr_response.msg = NULL, sr->sr_response.sip = NULL;
651
652 if (sr->sr_status != sip->sip_status->st_status) {
653 msg_header_remove(response, (msg_pub_t *)sip,
654 (msg_header_t *)sip->sip_status);
655 nta_incoming_complete_response(sr->sr_irq, response,
656 sr->sr_status,
657 sr->sr_phrase,
658 TAG_END());
659 }
660
661 if (sr->sr_status != sip->sip_status->st_status) {
662 msg_destroy(response);
663 SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
664 nta_incoming_treply(sr->sr_irq, sr->sr_status, sr->sr_phrase, TAG_END());
665 return 0;
666 }
667
668 return nta_incoming_mreply(sr->sr_irq, response);
669 }
670
nua_server_report(nua_server_request_t * sr)671 int nua_server_report(nua_server_request_t *sr)
672 {
673 if (sr)
674 return sr->sr_methods->sm_report(sr, NULL);
675 else
676 return 1;
677 }
678
nua_base_server_treport(nua_server_request_t * sr,tag_type_t tag,tag_value_t value,...)679 int nua_base_server_treport(nua_server_request_t *sr,
680 tag_type_t tag, tag_value_t value,
681 ...)
682 {
683 int retval;
684 ta_list ta;
685 ta_start(ta, tag, value);
686 retval = nua_base_server_report(sr, ta_args(ta));
687 ta_end(ta);
688 return retval;
689 }
690
691 /**Report request event to the application.
692 *
693 * @retval 0 request lives
694 * @retval 1 request was destroyed
695 * @retval 2 request and its usage was destroyed
696 * @retval 3 request, all usages and dialog was destroyed
697 * @retval 4 request, all usages, dialog, and handle was destroyed
698 */
nua_base_server_report(nua_server_request_t * sr,tagi_t const * tags)699 int nua_base_server_report(nua_server_request_t *sr, tagi_t const *tags)
700 {
701 nua_handle_t *nh = sr->sr_owner;
702 nua_t *nua = nh->nh_nua;
703 nua_dialog_usage_t *usage = sr->sr_usage;
704 int initial = sr->sr_initial;
705 int status = sr->sr_status;
706 char const *phrase = sr->sr_phrase;
707
708 int terminated;
709 int handle_can_be_terminated = initial && !sr->sr_event;
710
711 if (sr->sr_application) {
712 /* There was an error sending response */
713 if (sr->sr_application != sr->sr_status)
714 nua_stack_event(nua, nh, NULL, nua_i_error, status, phrase, tags);
715 sr->sr_application = 0;
716 }
717 else if (status < 300 && !sr->sr_event) {
718 msg_t *msg = msg_ref_create(sr->sr_request.msg);
719 nua_event_t e = (enum nua_event_e)sr->sr_methods->sm_event;
720 sr->sr_event = 1;
721 nua_stack_event(nua, nh, msg, e, status, phrase, tags);
722 }
723
724 if (status < 200)
725 return 0; /* sr lives on until final response is sent */
726
727 if (sr->sr_method == sip_method_invite && status < 300)
728 return 0; /* INVITE lives on until ACK is received */
729
730 if (initial && 300 <= status)
731 terminated = 1;
732 else if (sr->sr_terminating && status < 300)
733 terminated = 1;
734 else
735 terminated = sip_response_terminates_dialog(status, sr->sr_method, NULL);
736
737 if (usage && terminated)
738 nua_dialog_usage_remove(nh, nh->nh_ds, usage, NULL, sr);
739
740 nua_server_request_destroy(sr);
741
742 if (!terminated)
743 return 1;
744
745 if (!initial) {
746 if (terminated > 0)
747 return 2;
748
749 /* Remove all usages of the dialog */
750 nua_dialog_deinit(nh, nh->nh_ds);
751
752 return 3;
753 }
754 else if (!handle_can_be_terminated) {
755 return 3;
756 }
757 else {
758 if (nh != nh->nh_nua->nua_dhandle)
759 nh_destroy(nh->nh_nua, nh);
760
761 return 4;
762 }
763 }
764