4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
39 #include "sip_miscdefs.h"
40 #include "sip_xaction.h"
41 #include "sip_dialog.h"
42 #include "sip_parse_generic.h"
44 void (*sip_ulp_recv
)(const sip_conn_object_t
, sip_msg_t
,
45 const sip_dialog_t
) = NULL
;
46 uint_t (*sip_stack_timeout
)(void *, void (*func
)(void *),
47 struct timeval
*) = NULL
;
48 boolean_t (*sip_stack_untimeout
)(uint_t
) = NULL
;
49 int (*sip_stack_send
)(sip_conn_object_t xonn_object
, char *, int) =
51 void (*sip_refhold_conn
)(sip_conn_object_t
) = NULL
;
52 void (*sip_refrele_conn
)(sip_conn_object_t
) = NULL
;
53 boolean_t (*sip_is_conn_stream
)(sip_conn_object_t
) = NULL
;
54 boolean_t (*sip_is_conn_reliable
)(sip_conn_object_t
) = NULL
;
55 int (*sip_conn_rem_addr
)(sip_conn_object_t
, struct sockaddr
*,
57 int (*sip_conn_local_addr
)(sip_conn_object_t
, struct sockaddr
*,
59 int (*sip_conn_transport
)(sip_conn_object_t
) = NULL
;
60 int (*sip_conn_timer1
)(sip_conn_object_t
) = NULL
;
61 int (*sip_conn_timer2
)(sip_conn_object_t
) = NULL
;
62 int (*sip_conn_timer4
)(sip_conn_object_t
) = NULL
;
63 int (*sip_conn_timerd
)(sip_conn_object_t
) = NULL
;
65 boolean_t sip_manage_dialog
= B_FALSE
;
67 uint64_t sip_hash_salt
= 0;
70 * Defaults, overridden by configured values, if any
72 int sip_timer_T1
= SIP_TIMER_T1
;
73 int sip_timer_T2
= SIP_TIMER_T2
;
74 int sip_timer_T4
= SIP_TIMER_T4
;
75 int sip_timer_TD
= 32 * SIP_SECONDS
;
78 * list of sent-by values registered by the UA
80 sent_by_list_t
*sip_sent_by
= NULL
;
81 int sip_sent_by_count
= 0;
82 pthread_mutex_t sip_sent_by_lock
;
85 * Create and send an error response
88 sip_send_resp(sip_conn_object_t conn_obj
, _sip_msg_t
*sip_msg
, int resp
)
90 _sip_msg_t
*sip_msg_resp
;
92 sip_msg_resp
= (_sip_msg_t
*)sip_create_response((sip_msg_t
)sip_msg
,
93 resp
, sip_get_resp_desc(resp
), NULL
, NULL
);
94 if (sip_msg_resp
== NULL
) {
96 * Message was too bad to even create a
97 * response. Just drop the messge.
102 * We directly send it to the transport here.
104 if (sip_adjust_msgbuf(sip_msg_resp
) != 0) {
105 sip_free_msg((sip_msg_t
)sip_msg_resp
);
109 SIP_UPDATE_COUNTERS(B_FALSE
, 0, resp
, B_TRUE
, sip_msg_resp
->
111 (void) sip_stack_send(conn_obj
, sip_msg_resp
->sip_msg_buf
,
112 sip_msg_resp
->sip_msg_len
);
113 sip_free_msg((sip_msg_t
)sip_msg_resp
);
117 * Validate some of the common headers
120 sip_check_common_headers(sip_conn_object_t conn_obj
, _sip_msg_t
*sip_msg
)
124 if (sip_get_to_uri_str((sip_msg_t
)sip_msg
, &err
) == NULL
)
126 if (sip_get_from_uri_str((sip_msg_t
)sip_msg
, &err
) == NULL
)
128 if (sip_get_callseq_num((sip_msg_t
)sip_msg
, &err
) < 0)
130 if (sip_get_callid((sip_msg_t
)sip_msg
, &err
) == NULL
)
134 sip_send_resp(conn_obj
, sip_msg
, SIP_BAD_REQUEST
);
139 * setup pointers to where the headers are.
142 sip_setup_header_pointers(_sip_msg_t
*sip_msg
)
145 _sip_header_t
*sip_msg_header
;
148 msg
= sip_msg
->sip_msg_buf
;
149 end
= sip_msg
->sip_msg_buf
+ sip_msg
->sip_msg_len
;
153 while (isspace(*msg
)) {
161 * We consider Request and Response line as a header
167 if (strncmp(SIP_CRLF
, msg
, strlen(SIP_CRLF
)) == 0) {
168 if (sip_msg
->sip_msg_headers_end
!= NULL
) {
170 sip_msg
->sip_msg_headers_end
->sip_hdr_end
= msg
;
174 * Check for empty line.
176 if (strncmp(SIP_CRLF
, msg
, strlen(SIP_CRLF
)) == 0) {
178 * empty line, start of content.
181 sip_msg
->sip_msg_headers_end
->sip_hdr_end
= msg
;
185 * store start of header.
187 sip_msg_header
= calloc(1, sizeof (_sip_header_t
));
188 if (sip_msg_header
== NULL
)
190 sip_msg_header
->sip_hdr_start
= msg
;
191 sip_msg_header
->sip_hdr_current
= msg
;
192 sip_msg_header
->sip_hdr_allocated
= B_FALSE
;
193 sip_msg_header
->sip_hdr_prev
=
194 sip_msg
->sip_msg_headers_end
;
195 sip_msg_header
->sip_hdr_next
= NULL
;
196 sip_msg_header
->sip_hdr_sipmsg
= sip_msg
;
197 sip_msg
->sip_msg_headers_end
->sip_hdr_next
=
199 sip_msg
->sip_msg_headers_end
= sip_msg_header
;
201 if (sip_msg
->sip_msg_headers_start
== NULL
) {
203 * Allocate first header structure.
205 sip_msg_header
= calloc(1,
206 sizeof (_sip_header_t
));
207 if (sip_msg_header
== NULL
)
209 sip_msg_header
->sip_hdr_allocated
= B_FALSE
;
210 sip_msg_header
->sip_hdr_start
= msg
;
211 sip_msg_header
->sip_hdr_current
= msg
;
212 sip_msg_header
->sip_hdr_sipmsg
= sip_msg
;
213 sip_msg
->sip_msg_headers_start
= sip_msg_header
;
214 sip_msg
->sip_msg_headers_end
= sip_msg_header
;
219 * We have reached the end without hitting the empty line.
221 if (msg
- sip_msg
->sip_msg_buf
>= sip_msg
->sip_msg_len
)
225 if (sip_msg
->sip_msg_headers_start
== NULL
)
229 * Move start line to be a separate line.
231 sip_msg
->sip_msg_start_line
= sip_msg
->sip_msg_headers_start
;
232 sip_msg
->sip_msg_headers_start
=
233 sip_msg
->sip_msg_headers_start
->sip_hdr_next
;
234 sip_msg
->sip_msg_start_line
->sip_hdr_prev
= NULL
;
235 sip_msg
->sip_msg_start_line
->sip_hdr_next
= NULL
;
237 if (sip_msg
->sip_msg_headers_start
== NULL
)
239 sip_msg
->sip_msg_headers_start
->sip_hdr_prev
= NULL
;
245 sip_msg
->sip_msg_content
= calloc(1, sizeof (sip_content_t
));
246 sip_msg
->sip_msg_content
->sip_content_start
= msg
;
247 sip_msg
->sip_msg_content
->sip_content_end
= sip_msg
->sip_msg_buf
+
248 sip_msg
->sip_msg_len
;
249 sip_msg
->sip_msg_content
->sip_content_allocated
= B_FALSE
;
250 sip_msg
->sip_msg_content_len
=
251 sip_msg
->sip_msg_content
->sip_content_end
-
252 sip_msg
->sip_msg_content
->sip_content_start
;
257 * The send interface to the sip stack. Used by upper layers.
260 sip_sendmsg(sip_conn_object_t obj
, sip_msg_t sip_msg
, sip_dialog_t dialog
,
263 sip_xaction_t
*sip_trans
= NULL
;
265 sip_message_type_t
*sip_msg_info
;
266 _sip_msg_t
*_sip_msg
;
267 boolean_t stateful
= flags
& SIP_SEND_STATEFUL
;
268 boolean_t dlg_on_fork
= flags
& SIP_DIALOG_ON_FORK
;
270 sip_refhold_conn(obj
);
272 _sip_msg
= (_sip_msg_t
*)sip_msg
;
273 if ((ret
= sip_adjust_msgbuf(_sip_msg
)) != 0) {
274 sip_refrele_conn(obj
);
278 assert(_sip_msg
->sip_msg_req_res
!= NULL
);
279 sip_msg_info
= _sip_msg
->sip_msg_req_res
;
281 * Send it statefully if:
282 * if stateful is set in 'flags' AND
283 * this is not an ACK request, if it is a request (should the upper
284 * layer set stateful in the latter case?, i.e is the check
287 if (stateful
&& (!sip_msg_info
->is_request
||
288 sip_msg_info
->sip_req_method
!= ACK
)) {
289 sip_trans
= (sip_xaction_t
*)sip_xaction_get(obj
, sip_msg
,
290 B_TRUE
, sip_msg_info
->is_request
? SIP_CLIENT_TRANSACTION
:
291 SIP_SERVER_TRANSACTION
, &ret
);
292 if (sip_trans
== NULL
) {
293 sip_refrele_conn(obj
);
296 ret
= sip_xaction_output(obj
, sip_trans
, _sip_msg
);
297 SIP_XACTION_REFCNT_DECR(sip_trans
);
299 sip_refrele_conn(obj
);
304 * If the appln wants us to create the dialog, create a partial
305 * dialog at this stage, when we get the response, we will
308 if (sip_manage_dialog
) {
309 if (sip_msg_info
->is_request
&& dialog
== NULL
) {
310 dialog
= (sip_dialog_t
)sip_seed_dialog(obj
, sip_msg
,
311 dlg_on_fork
, SIP_UAC_DIALOG
);
312 } else if (dialog
!= NULL
&& (!sip_msg_info
->is_request
||
313 sip_msg_info
->sip_req_method
== NOTIFY
)) {
314 (void) sip_update_dialog(dialog
, _sip_msg
);
315 } else if (dialog
!= NULL
) {
317 * Dialog is in CONFIRMED state. If logging is enabled
318 * track the SIP message sent within a dialog.
320 (void) pthread_mutex_lock(&dialog
->sip_dlg_mutex
);
321 dialog
->sip_dlg_msgcnt
++;
322 sip_add_log(&dialog
->sip_dlg_log
[dialog
->sip_dlg_state
],
323 (sip_msg_t
)sip_msg
, dialog
->sip_dlg_msgcnt
,
325 (void) pthread_mutex_unlock(&dialog
->sip_dlg_mutex
);
327 if (sip_msg_info
->is_request
&& sip_msg_info
->
328 sip_req_method
== INVITE
) {
329 (void) sip_dialog_add_new_contact(dialog
,
335 * if measure sip traffic is enabled, capture the measurements
336 * Is this the right place to measure or should I put this after
337 * the call to sip_stack_send()
339 if (sip_msg_info
->is_request
) {
340 SIP_UPDATE_COUNTERS(B_TRUE
, sip_msg_info
->sip_req_method
, 0,
341 B_TRUE
, _sip_msg
->sip_msg_len
);
343 SIP_UPDATE_COUNTERS(B_FALSE
, 0, sip_msg_info
->sip_resp_code
,
344 B_TRUE
, _sip_msg
->sip_msg_len
);
346 if ((ret
= sip_stack_send(obj
, _sip_msg
->sip_msg_buf
,
347 _sip_msg
->sip_msg_len
)) != 0) {
348 if (sip_trans
!= NULL
) {
349 sip_xaction_terminate(sip_trans
, _sip_msg
,
350 sip_conn_transport(obj
));
352 sip_refrele_conn(obj
);
355 sip_refrele_conn(obj
);
360 * Given a sent-by value check if it is in the registered list. If no values
361 * have been registered, the check passes.
364 sip_sent_by_registered(const sip_str_t
*sb_val
)
369 (void) pthread_mutex_lock(&sip_sent_by_lock
);
370 if (sip_sent_by
== NULL
) {
371 (void) pthread_mutex_unlock(&sip_sent_by_lock
);
375 for (count
= 0; count
< sip_sent_by_count
; count
++) {
376 if (strncasecmp(sb
->sb_val
, sb_val
->sip_str_ptr
,
377 sb_val
->sip_str_len
) == 0) {
378 (void) pthread_mutex_unlock(&sip_sent_by_lock
);
383 (void) pthread_mutex_unlock(&sip_sent_by_lock
);
388 * Given a response, check if the sent-by in the VIA header is valid.
391 sip_valid_sent_by(sip_msg_t sip_msg
)
394 sip_header_value_t value
= NULL
;
396 const sip_str_t
*sent_by
= NULL
;
398 via
= (sip_header_t
)sip_get_header(sip_msg
, SIP_VIA
, NULL
, &error
);
399 if (via
== NULL
|| error
!= 0)
401 value
= (sip_header_value_t
)sip_get_header_value(via
, &error
);
402 if (value
== NULL
|| error
!= 0)
404 sent_by
= sip_get_via_sent_by_host(value
, &error
);
405 if (sent_by
== NULL
|| error
!= 0)
407 if (sip_sent_by_registered(sent_by
))
414 * The receive interface to the transport layer.
417 sip_process_new_packet(sip_conn_object_t conn_object
, void *msgstr
,
421 sip_message_type_t
*sip_msg_info
;
422 sip_xaction_t
*sip_trans
;
423 sip_dialog_t dialog
= NULL
;
424 boolean_t dialog_created
= B_FALSE
;
428 sip_refhold_conn(conn_object
);
429 transport
= sip_conn_transport(conn_object
);
430 if (transport
== IPPROTO_TCP
) {
432 msgstr
= (char *)sip_get_tcp_msg(conn_object
, (char *)msgstr
,
434 if (msgstr
== NULL
) {
435 sip_refrele_conn(conn_object
);
439 msgbuf
= (char *)malloc(msglen
+ 1);
440 if (msgbuf
== NULL
) {
441 sip_refrele_conn(conn_object
);
444 (void) strncpy(msgbuf
, msgstr
, msglen
);
445 msgbuf
[msglen
] = '\0';
448 sip_msg
= (_sip_msg_t
*)sip_new_msg();
449 if (sip_msg
== NULL
) {
452 sip_refrele_conn(conn_object
);
455 sip_msg
->sip_msg_buf
= (char *)msgstr
;
456 sip_msg
->sip_msg_len
= msglen
;
457 (void) pthread_mutex_lock(&sip_msg
->sip_msg_mutex
);
458 if (sip_setup_header_pointers(sip_msg
) != 0) {
459 (void) pthread_mutex_unlock(&sip_msg
->sip_msg_mutex
);
460 sip_refrele_conn(conn_object
);
461 sip_free_msg((sip_msg_t
)sip_msg
);
464 if (sip_parse_first_line(sip_msg
->sip_msg_start_line
,
465 &sip_msg
->sip_msg_req_res
)) {
466 (void) pthread_mutex_unlock(&sip_msg
->sip_msg_mutex
);
467 sip_refrele_conn(conn_object
);
468 sip_free_msg((sip_msg_t
)sip_msg
);
471 sip_msg_info
= sip_msg
->sip_msg_req_res
;
472 (void) pthread_mutex_unlock(&sip_msg
->sip_msg_mutex
);
474 if (sip_check_common_headers(conn_object
, sip_msg
)) {
475 sip_refrele_conn(conn_object
);
476 sip_free_msg((sip_msg_t
)sip_msg
);
481 * Silently discard the response if the top VIA has a sent-by value AND
482 * the UA has registered sent-by values AND the one in the VIA is
483 * not part of the registerd sent-by values.
485 if (!sip_msg_info
->is_request
&& !sip_valid_sent_by(sip_msg
)) {
486 sip_refrele_conn(conn_object
);
487 sip_free_msg((sip_msg_t
)sip_msg
);
491 sip_trans
= (sip_xaction_t
*)sip_xaction_get(conn_object
,
493 B_FALSE
, sip_msg_info
->is_request
? SIP_SERVER_TRANSACTION
:
494 SIP_CLIENT_TRANSACTION
, NULL
);
495 if (sip_trans
!= NULL
) {
496 if (sip_xaction_input(conn_object
, sip_trans
, &sip_msg
) != 0) {
497 SIP_XACTION_REFCNT_DECR(sip_trans
);
498 sip_refrele_conn(conn_object
);
499 sip_free_msg((sip_msg_t
)sip_msg
);
502 SIP_XACTION_REFCNT_DECR(sip_trans
);
505 * msg was retransmission - handled by the transaction
511 * If we are getting an INVITE request, let us send a
512 * 100 TRYING response here, as in 17.2.1:
513 * "The server transaction MUST generate a 100 (Trying)
514 * response unless it knows that the TU will generate a
515 * provisional or final response within 200 ms".
517 if (sip_msg_info
->is_request
&&
518 sip_msg_info
->sip_req_method
== INVITE
) {
519 sip_send_resp(conn_object
, sip_msg
, SIP_TRYING
);
522 if (sip_manage_dialog
) {
523 dialog
= sip_dialog_find(sip_msg
);
524 if (dialog
== NULL
) {
525 if (sip_msg_info
->is_request
) {
527 * sip_seed_dialog will check for the
528 * method in the request
530 dialog
= (sip_dialog_t
)sip_seed_dialog(
531 conn_object
, sip_msg
,
532 B_FALSE
, SIP_UAS_DIALOG
);
533 dialog_created
= B_TRUE
;
535 } else if (sip_incomplete_dialog(dialog
)) {
536 if (!sip_msg_info
->is_request
||
537 sip_msg_info
->sip_req_method
== NOTIFY
) {
538 dialog
= sip_update_dialog(dialog
, sip_msg
);
540 } else if (sip_dialog_process(sip_msg
, &dialog
) != 0) {
542 sip_release_dialog(dialog
);
544 * cseq number in error, send a
545 * SIP_SERVER_INTERNAL_ERROR response.
547 if (sip_msg_info
->is_request
) {
548 sip_send_resp(conn_object
, sip_msg
,
549 SIP_SERVER_INTERNAL_ERROR
);
551 sip_refrele_conn(conn_object
);
552 sip_free_msg((sip_msg_t
)sip_msg
);
556 if (sip_msg_info
->is_request
) {
557 SIP_UPDATE_COUNTERS(B_TRUE
, sip_msg_info
->sip_req_method
, 0,
558 B_FALSE
, sip_msg
->sip_msg_len
);
560 SIP_UPDATE_COUNTERS(B_FALSE
, 0, sip_msg_info
->sip_resp_code
,
561 B_FALSE
, sip_msg
->sip_msg_len
);
563 sip_ulp_recv(conn_object
, (sip_msg_t
)sip_msg
, dialog
);
564 sip_free_msg((sip_msg_t
)sip_msg
);
565 if (dialog
!= NULL
&& !dialog_created
)
566 sip_release_dialog(dialog
);
569 * Check if there are more complete messages in the TCP fragment list
572 if (transport
== IPPROTO_TCP
) {
577 sip_refrele_conn(conn_object
);
581 * Initialize the stack. The connection manager functions, upper layer
582 * receive functions are mandatory.
585 sip_stack_init(sip_stack_init_t
*stack_val
)
588 struct timespec tspec
;
592 * If the stack has already been configured, return error
594 if (sip_stack_send
!= NULL
||
595 stack_val
->sip_version
!= SIP_STACK_VERSION
) {
598 if (stack_val
->sip_io_pointers
== NULL
||
599 stack_val
->sip_ulp_pointers
== NULL
) {
602 sip_ulp_recv
= stack_val
->sip_ulp_pointers
->sip_ulp_recv
;
603 sip_manage_dialog
= stack_val
->sip_stack_flags
& SIP_STACK_DIALOGS
;
605 sip_stack_send
= stack_val
->sip_io_pointers
->sip_conn_send
;
606 sip_refhold_conn
= stack_val
->sip_io_pointers
->sip_hold_conn_object
;
607 sip_refrele_conn
= stack_val
->sip_io_pointers
->sip_rel_conn_object
;
608 sip_is_conn_stream
= stack_val
->sip_io_pointers
->sip_conn_is_stream
;
609 sip_is_conn_reliable
= stack_val
->sip_io_pointers
->sip_conn_is_reliable
;
610 sip_conn_rem_addr
= stack_val
->sip_io_pointers
->sip_conn_remote_address
;
611 sip_conn_local_addr
=
612 stack_val
->sip_io_pointers
->sip_conn_local_address
;
613 sip_conn_transport
= stack_val
->sip_io_pointers
->sip_conn_transport
;
614 sip_header_function_table_external
= stack_val
->sip_function_table
;
616 if (sip_ulp_recv
== NULL
|| sip_stack_send
== NULL
||
617 sip_refhold_conn
== NULL
|| sip_refrele_conn
== NULL
||
618 sip_is_conn_stream
== NULL
|| sip_is_conn_reliable
== NULL
||
619 sip_conn_rem_addr
== NULL
|| sip_conn_local_addr
== NULL
||
620 sip_conn_transport
== NULL
) {
623 sip_stack_send
= NULL
;
624 sip_refhold_conn
= NULL
;
625 sip_refrele_conn
= NULL
;
626 sip_is_conn_stream
= NULL
;
627 sip_is_conn_reliable
= NULL
;
628 sip_conn_rem_addr
= NULL
;
629 sip_conn_local_addr
= NULL
;
630 sip_conn_transport
= NULL
;
631 sip_header_function_table_external
= NULL
;
632 sip_stack_timeout
= NULL
;
633 sip_stack_untimeout
= NULL
;
637 sip_conn_timer1
= stack_val
->sip_io_pointers
->sip_conn_timer1
;
638 sip_conn_timer2
= stack_val
->sip_io_pointers
->sip_conn_timer2
;
639 sip_conn_timer4
= stack_val
->sip_io_pointers
->sip_conn_timer4
;
640 sip_conn_timerd
= stack_val
->sip_io_pointers
->sip_conn_timerd
;
643 * Use Appln timeout routines, if provided
645 if (stack_val
->sip_ulp_pointers
->sip_ulp_timeout
!= NULL
) {
646 if (stack_val
->sip_ulp_pointers
->sip_ulp_untimeout
== NULL
)
649 stack_val
->sip_ulp_pointers
->sip_ulp_timeout
;
650 sip_stack_untimeout
=
651 stack_val
->sip_ulp_pointers
->sip_ulp_untimeout
;
653 if (stack_val
->sip_ulp_pointers
->sip_ulp_untimeout
!= NULL
)
656 sip_stack_timeout
= sip_timeout
;
657 sip_stack_untimeout
= sip_untimeout
;
663 if (sip_manage_dialog
) {
664 sip_dialog_init(stack_val
->sip_ulp_pointers
->sip_ulp_dlg_del
,
665 stack_val
->sip_ulp_pointers
->sip_ulp_dlg_state_cb
);
667 sip_xaction_init(stack_val
->sip_ulp_pointers
->sip_ulp_trans_error
,
668 stack_val
->sip_ulp_pointers
->sip_ulp_trans_state_cb
);
671 * Initialize SIP traffic counter mutex
673 (void) pthread_mutex_init(&sip_counters
.sip_counter_mutex
, NULL
);
676 * Initialize SIP logfile structures mutex
678 (void) pthread_mutex_init(&trans_log
.sip_logfile_mutex
, NULL
);
679 (void) pthread_mutex_init(&dialog_log
.sip_logfile_mutex
, NULL
);
682 if (clock_gettime(CLOCK_REALTIME
, &tspec
) != 0)
684 sip_hash_salt
= tspec
.tv_nsec
;
686 sip_hash_salt
= gethrtime();
688 (void) pthread_mutex_init(&sip_sent_by_lock
, NULL
);