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 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
38 #include "sip_miscdefs.h"
39 #include "sip_parse_generic.h"
42 * Response consists of SIP version, response code, response phrase and CRLF.
44 #define SIP_RESPONSE "%s %d %s%s"
46 void sip_free_content(_sip_msg_t
*);
49 * Allocate a new sip msg struct.
56 sip_msg
= calloc(1, sizeof (_sip_msg_t
));
57 if (sip_msg
!= NULL
) {
58 sip_msg
->sip_msg_ref_cnt
= 1;
59 (void) pthread_mutex_init(&sip_msg
->sip_msg_mutex
, NULL
);
61 return ((sip_msg_t
)sip_msg
);
65 * Free all resources. The lock is taken by SIP_MSG_REFCNT_DECR. The
66 * thread that decrements the last refcount should take care that
67 * the message is not accessible to other threads before doing so.
68 * Else, if the message is still accessible to others, it is
69 * possible that the other thread could be waiting to take the
70 * lock when we proceed to destroy it.
73 sip_destroy_msg(_sip_msg_t
*_sip_msg
)
76 assert(mutex_held(&_sip_msg
->sip_msg_mutex
));
78 (void) sip_delete_start_line_locked(_sip_msg
);
79 assert(_sip_msg
->sip_msg_ref_cnt
== 0);
80 sip_delete_all_headers((sip_msg_t
)_sip_msg
);
81 sip_free_content(_sip_msg
);
82 if (_sip_msg
->sip_msg_buf
!= NULL
)
83 free(_sip_msg
->sip_msg_buf
);
85 if (_sip_msg
->sip_msg_old_buf
!= NULL
)
86 free(_sip_msg
->sip_msg_old_buf
);
88 while (_sip_msg
->sip_msg_req_res
!= NULL
) {
89 sip_message_type_t
*sip_msg_type_ptr
;
91 sip_msg_type_ptr
= _sip_msg
->sip_msg_req_res
->sip_next
;
92 if (_sip_msg
->sip_msg_req_res
->is_request
) {
93 sip_request_t
*reqline
;
95 reqline
= &_sip_msg
->sip_msg_req_res
->U
.sip_request
;
96 if (reqline
->sip_parse_uri
!= NULL
) {
97 sip_free_parsed_uri(reqline
->sip_parse_uri
);
98 reqline
->sip_parse_uri
= NULL
;
101 free(_sip_msg
->sip_msg_req_res
);
102 _sip_msg
->sip_msg_req_res
= sip_msg_type_ptr
;
104 (void) pthread_mutex_destroy(&_sip_msg
->sip_msg_mutex
);
109 * Free a sip msg struct.
112 sip_free_msg(sip_msg_t sip_msg
)
117 SIP_MSG_REFCNT_DECR((_sip_msg_t
*)sip_msg
);
121 * Hold a sip msg struct.
124 sip_hold_msg(sip_msg_t sip_msg
)
130 SIP_MSG_REFCNT_INCR((_sip_msg_t
*)sip_msg
);
137 sip_clone_msg(sip_msg_t sip_msg
)
140 _sip_msg_t
*_sip_msg
;
141 sip_content_t
*sip_content
;
142 sip_content_t
*msg_content
;
143 sip_content_t
*new_content
= NULL
;
148 new_msg
= (_sip_msg_t
*)sip_new_msg();
151 _sip_msg
= (_sip_msg_t
*)sip_msg
;
155 if (sip_copy_start_line(_sip_msg
, new_msg
) != 0) {
156 sip_free_msg((sip_msg_t
)new_msg
);
159 if (sip_copy_all_headers(_sip_msg
, new_msg
) != 0) {
160 sip_free_msg((sip_msg_t
)new_msg
);
163 (void) pthread_mutex_lock(&_sip_msg
->sip_msg_mutex
);
164 sip_content
= _sip_msg
->sip_msg_content
;
165 while (sip_content
!= NULL
) {
166 msg_content
= calloc(1, sizeof (sip_content_t
));
167 if (msg_content
== NULL
) {
168 sip_free_msg((sip_msg_t
)new_msg
);
169 (void) pthread_mutex_unlock(&_sip_msg
->sip_msg_mutex
);
172 len
= sip_content
->sip_content_end
-
173 sip_content
->sip_content_start
;
174 msg_content
->sip_content_start
= malloc(len
+ 1);
175 if (msg_content
->sip_content_start
== NULL
) {
176 sip_free_msg((sip_msg_t
)new_msg
);
177 (void) pthread_mutex_unlock(&_sip_msg
->sip_msg_mutex
);
180 (void) strncpy(msg_content
->sip_content_start
,
181 sip_content
->sip_content_start
, len
);
182 msg_content
->sip_content_start
[len
] = '\0';
183 msg_content
->sip_content_current
=
184 msg_content
->sip_content_start
;
185 msg_content
->sip_content_end
= msg_content
->sip_content_start
+
187 msg_content
->sip_content_allocated
= B_TRUE
;
188 new_msg
->sip_msg_content_len
+= len
;
189 new_msg
->sip_msg_len
+= len
;
190 if (new_msg
->sip_msg_content
== NULL
)
191 new_msg
->sip_msg_content
= msg_content
;
193 new_content
->sip_content_next
= msg_content
;
194 new_content
= msg_content
;
195 sip_content
= sip_content
->sip_content_next
;
197 (void) pthread_mutex_unlock(&_sip_msg
->sip_msg_mutex
);
199 * Since this is a new message, no threads should be referring
200 * to this, so it is not necessary to take the lock, however,
201 * since sip_msg_to_msgbuf() expects the lock to be held, we'll
204 (void) pthread_mutex_lock(&new_msg
->sip_msg_mutex
);
205 new_msg
->sip_msg_buf
= sip_msg_to_msgbuf((sip_msg_t
)new_msg
, NULL
);
206 if (new_msg
->sip_msg_buf
== NULL
) {
207 (void) pthread_mutex_unlock(&new_msg
->sip_msg_mutex
);
208 sip_free_msg((sip_msg_t
)new_msg
);
211 new_msg
->sip_msg_cannot_be_modified
= B_TRUE
;
212 (void) pthread_mutex_unlock(&new_msg
->sip_msg_mutex
);
214 return ((sip_msg_t
)new_msg
);
218 * Return the SIP message as a string. Caller frees the string
221 sip_msg_to_str(sip_msg_t sip_msg
, int *error
)
226 if (sip_msg
== NULL
) {
231 msg
= (_sip_msg_t
*)sip_msg
;
232 (void) pthread_mutex_lock(&msg
->sip_msg_mutex
);
233 msgstr
= sip_msg_to_msgbuf(msg
, error
);
234 (void) pthread_mutex_unlock(&msg
->sip_msg_mutex
);
239 * Given a message generate a string that includes all the headers and the
243 sip_msg_to_msgbuf(_sip_msg_t
*msg
, int *error
)
245 _sip_header_t
*header
;
249 sip_content_t
*sip_content
;
264 assert(mutex_held(&msg
->sip_msg_mutex
));
267 p
= (char *)malloc(msg
->sip_msg_len
+ 1);
278 if (msg
->sip_msg_start_line
!= NULL
) {
279 len
= msg
->sip_msg_start_line
->sip_hdr_end
-
280 msg
->sip_msg_start_line
->sip_hdr_start
;
281 (void) strncpy(e
, msg
->sip_msg_start_line
->sip_hdr_start
, len
);
287 header
= sip_search_for_header(msg
, NULL
, NULL
);
288 while (header
!= NULL
) {
289 if (header
->sip_header_state
!= SIP_HEADER_DELETED
) {
290 if (header
->sip_header_state
==
291 SIP_HEADER_DELETED_VAL
) {
292 len
= sip_copy_values(e
, header
);
294 len
= header
->sip_hdr_end
-
295 header
->sip_hdr_start
;
296 (void) strncpy(e
, header
->sip_hdr_start
, len
);
300 assert(tlen
<= msg
->sip_msg_len
);
303 header
= sip_search_for_header(msg
, NULL
, header
);
306 sip_content
= msg
->sip_msg_content
;
307 while (sip_content
!= NULL
) {
308 len
= sip_content
->sip_content_end
-
309 sip_content
->sip_content_start
;
312 assert(clen
<= msg
->sip_msg_content_len
);
314 assert(tlen
<= msg
->sip_msg_len
);
316 (void) strncpy(e
, sip_content
->sip_content_start
, len
);
318 sip_content
= sip_content
->sip_content_next
;
320 p
[msg
->sip_msg_len
] = '\0';
325 * This is called just before sending the message to the transport. It
326 * creates the sip_msg_buf from the SIP headers.
329 sip_adjust_msgbuf(_sip_msg_t
*msg
)
331 _sip_header_t
*header
;
341 (void) pthread_mutex_lock(&msg
->sip_msg_mutex
);
342 if ((msg
->sip_msg_buf
!= NULL
) && (!msg
->sip_msg_modified
)) {
344 * We could just be forwarding the message we
347 (void) pthread_mutex_unlock(&msg
->sip_msg_mutex
);
352 * We are sending a new message or a message that we received
353 * but have modified it. We keep the old
354 * msgbuf till the message is freed as some
355 * headers still point to it.
358 assert(msg
->sip_msg_old_buf
== NULL
);
359 msg
->sip_msg_old_buf
= msg
->sip_msg_buf
;
361 * We add the content-length header here, if it has not
362 * already been added.
364 header
= sip_search_for_header(msg
, SIP_CONTENT_LENGTH
, NULL
);
365 if (header
!= NULL
) {
367 * Mark the previous header as deleted.
369 header
->sip_header_state
= SIP_HEADER_DELETED
;
370 header
->sip_hdr_sipmsg
->sip_msg_len
-= header
->sip_hdr_end
-
371 header
->sip_hdr_start
;
373 (void) pthread_mutex_unlock(&msg
->sip_msg_mutex
);
374 ret
= sip_add_content_length(msg
, msg
->sip_msg_content_len
);
376 (void) pthread_mutex_unlock(&msg
->sip_msg_mutex
);
379 (void) pthread_mutex_lock(&msg
->sip_msg_mutex
);
380 msg
->sip_msg_modified
= B_FALSE
;
382 msg
->sip_msg_buf
= sip_msg_to_msgbuf((sip_msg_t
)msg
, &ret
);
383 if (msg
->sip_msg_buf
== NULL
) {
384 (void) pthread_mutex_unlock(&msg
->sip_msg_mutex
);
388 * Once the message has been sent it can not be modified
389 * any furthur as we keep a pointer to it for retransmission
391 msg
->sip_msg_cannot_be_modified
= B_TRUE
;
393 (void) pthread_mutex_unlock(&msg
->sip_msg_mutex
);
398 * Copy header values into ptr
401 sip_copy_values(char *ptr
, _sip_header_t
*header
)
403 sip_header_value_t value
;
406 boolean_t first
= B_TRUE
;
409 boolean_t crlf_present
= B_FALSE
;
411 if (sip_parse_goto_values(header
) != 0)
414 len
= header
->sip_hdr_current
- header
->sip_hdr_start
;
415 (void) strncpy(p
, header
->sip_hdr_start
, len
);
418 value
= header
->sip_hdr_parsed
->value
;
419 while (value
!= NULL
) {
420 if (value
->value_state
!= SIP_VALUE_DELETED
) {
421 crlf_present
= B_FALSE
;
422 len
= value
->value_end
- value
->value_start
;
424 (void) strncpy(p
, value
->value_start
, len
);
427 s
= value
->value_start
;
428 while (*s
!= SIP_COMMA
)
430 len
+= value
->value_start
- s
;
431 (void) strncpy(p
, s
, len
);
435 s
= value
->value_end
;
436 while (s
!= value
->value_start
) {
437 if (*s
== '\r' && strncmp(s
, SIP_CRLF
,
438 strlen(SIP_CRLF
)) == 0) {
439 crlf_present
= B_TRUE
;
445 if (value
->next
== NULL
&& !first
&& !crlf_present
) {
446 s
= value
->value_end
;
449 len
= value
->value_end
- s
;
450 (void) strncpy(p
, s
, len
);
462 * Add content (message body) to sip_msg
465 sip_add_content(sip_msg_t sip_msg
, char *content
)
469 sip_content_t
*msg_content
;
470 _sip_msg_t
*_sip_msg
;
472 if (sip_msg
== NULL
|| content
== NULL
|| strlen(content
) == 0)
474 len
= strlen(content
);
475 _sip_msg
= (_sip_msg_t
*)sip_msg
;
476 (void) pthread_mutex_lock(&_sip_msg
->sip_msg_mutex
);
478 if (_sip_msg
->sip_msg_cannot_be_modified
) {
479 (void) pthread_mutex_unlock(&_sip_msg
->sip_msg_mutex
);
483 msg_content
= calloc(1, sizeof (sip_content_t
));
484 if (msg_content
== NULL
) {
485 (void) pthread_mutex_unlock(&_sip_msg
->sip_msg_mutex
);
488 msg_content
->sip_content_start
= malloc(strlen(content
) + 1);
489 if (msg_content
->sip_content_start
== NULL
) {
490 (void) pthread_mutex_unlock(&_sip_msg
->sip_msg_mutex
);
494 (void) strncpy(msg_content
->sip_content_start
, content
,
496 msg_content
->sip_content_start
[strlen(content
)] = '\0';
497 msg_content
->sip_content_current
= msg_content
->sip_content_start
;
498 msg_content
->sip_content_end
= msg_content
->sip_content_start
+
499 strlen(msg_content
->sip_content_start
);
500 msg_content
->sip_content_allocated
= B_TRUE
;
502 loc
= &_sip_msg
->sip_msg_content
;
504 loc
= &((*loc
)->sip_content_next
);
507 _sip_msg
->sip_msg_content_len
+= len
;
508 _sip_msg
->sip_msg_len
+= len
;
509 if (_sip_msg
->sip_msg_buf
!= NULL
)
510 _sip_msg
->sip_msg_modified
= B_TRUE
;
511 (void) pthread_mutex_unlock(&_sip_msg
->sip_msg_mutex
);
516 * Free the message content
519 sip_free_content(_sip_msg_t
*sip_msg
)
521 sip_content_t
*content
;
525 content
= sip_msg
->sip_msg_content
;
526 while (content
!= NULL
) {
527 sip_content_t
*content_tmp
;
529 content_tmp
= content
;
530 content
= content
->sip_content_next
;
531 if (content_tmp
->sip_content_allocated
)
532 free(content_tmp
->sip_content_start
);
535 sip_msg
->sip_msg_content
= NULL
;
540 * Add a response line to sip_response
543 sip_add_response_line(sip_msg_t sip_response
, int response
, char *response_code
)
545 _sip_header_t
*new_header
;
547 _sip_msg_t
*_sip_response
;
550 if (sip_response
== NULL
|| response
< 0 || response_code
== NULL
)
552 _sip_response
= (_sip_msg_t
*)sip_response
;
553 (void) pthread_mutex_lock(&_sip_response
->sip_msg_mutex
);
554 if (_sip_response
->sip_msg_cannot_be_modified
) {
555 (void) pthread_mutex_unlock(&_sip_response
->sip_msg_mutex
);
558 header_size
= strlen(SIP_VERSION
) + SIP_SPACE_LEN
+
559 SIP_SIZE_OF_STATUS_CODE
+ SIP_SPACE_LEN
+ strlen(response_code
) +
562 new_header
= sip_new_header(header_size
);
563 if (new_header
== NULL
) {
564 (void) pthread_mutex_unlock(&_sip_response
->sip_msg_mutex
);
567 new_header
->sip_hdr_sipmsg
= _sip_response
;
569 (void) snprintf(new_header
->sip_hdr_start
, header_size
+ 1,
570 SIP_RESPONSE
, SIP_VERSION
, response
, response_code
, SIP_CRLF
);
572 new_header
->sip_hdr_next
= _sip_response
->sip_msg_start_line
;
573 _sip_response
->sip_msg_start_line
= new_header
;
574 _sip_response
->sip_msg_len
+= header_size
;
575 ret
= sip_parse_first_line(_sip_response
->sip_msg_start_line
,
576 &_sip_response
->sip_msg_req_res
);
577 if (_sip_response
->sip_msg_buf
!= NULL
)
578 _sip_response
->sip_msg_modified
= B_TRUE
;
579 (void) pthread_mutex_unlock(&_sip_response
->sip_msg_mutex
);
584 * create a response based on the sip_request.
585 * Copies Call-ID, CSeq, From, To and Via headers from the request.
588 sip_create_response(sip_msg_t sip_request
, int response
, char *response_code
,
589 char *totag
, char *mycontact
)
592 _sip_msg_t
*_sip_request
;
593 boolean_t ttag_present
;
595 if (sip_request
== NULL
|| response_code
== NULL
)
598 ttag_present
= sip_get_to_tag(sip_request
, NULL
) != NULL
;
600 new_msg
= (_sip_msg_t
*)sip_new_msg();
603 _sip_request
= (_sip_msg_t
*)sip_request
;
605 (void) pthread_mutex_lock(&_sip_request
->sip_msg_mutex
);
610 if (sip_add_response_line(new_msg
, response
, response_code
) != 0)
616 if (_sip_find_and_copy_all_header(_sip_request
, new_msg
, SIP_VIA
) != 0)
622 if (_sip_find_and_copy_header(_sip_request
, new_msg
, SIP_FROM
,
627 * Copy To header. If To tag is present, copy it, if not then
628 * add one if the repsonse is not provisional.
630 if (ttag_present
|| (totag
== NULL
&& response
== SIP_TRYING
)) {
631 if (_sip_find_and_copy_header(_sip_request
, new_msg
, SIP_TO
,
637 boolean_t tag_alloc
= B_FALSE
;
646 taglen
= strlen(SIP_TAG
) + strlen(totag
) + 1;
647 xtra_param
= (char *)malloc(taglen
);
648 if (xtra_param
== NULL
) {
653 (void) snprintf(xtra_param
, taglen
, "%s%s", SIP_TAG
, totag
);
656 if (_sip_find_and_copy_header(_sip_request
, new_msg
,
657 SIP_TO
, xtra_param
, B_FALSE
)) {
665 * Copy Call-ID header.
667 if (_sip_find_and_copy_header(_sip_request
, new_msg
, SIP_CALL_ID
, NULL
,
674 if (_sip_find_and_copy_header(_sip_request
, new_msg
, SIP_CSEQ
, NULL
,
679 * Copy RECORD-ROUTE header, if present.
681 if (sip_search_for_header(_sip_request
, SIP_RECORD_ROUTE
, NULL
) !=
683 if (_sip_find_and_copy_all_header(_sip_request
, new_msg
,
684 SIP_RECORD_ROUTE
) != 0) {
688 if (mycontact
!= NULL
) {
689 if (sip_add_contact(new_msg
, NULL
, mycontact
, B_FALSE
,
694 (void) pthread_mutex_unlock(&_sip_request
->sip_msg_mutex
);
695 return ((sip_msg_t
)new_msg
);
697 sip_free_msg((sip_msg_t
)new_msg
);
698 (void) pthread_mutex_unlock(&_sip_request
->sip_msg_mutex
);
703 * NON OK ACK : MUST contain values for the Call-ID, From, and Request-URI
704 * that are equal to the values of those header fields in the orig request
705 * passed to the transport. The To header field in the ACK MUST equal the To
706 * header field in the response being acknowledged. The ACK MUST contain the
707 * top Via header field of the original request. The CSeq header field in
708 * the ACK MUST contain the same value for the sequence number as was
709 * present in the original request, but the method parameter MUST be equal
713 sip_create_nonOKack(sip_msg_t request
, sip_msg_t response
, sip_msg_t ack_msg
)
717 _sip_msg_t
*_request
;
718 _sip_msg_t
*_response
;
719 _sip_msg_t
*_ack_msg
;
722 if (request
== NULL
|| response
== NULL
|| ack_msg
== NULL
||
723 request
== ack_msg
) {
726 _request
= (_sip_msg_t
*)request
;
727 _response
= (_sip_msg_t
*)response
;
728 _ack_msg
= (_sip_msg_t
*)ack_msg
;
730 (void) pthread_mutex_lock(&_request
->sip_msg_mutex
);
731 if (_request
->sip_msg_req_res
== NULL
) {
732 if ((ret
= sip_parse_first_line(_request
->sip_msg_start_line
,
733 &_request
->sip_msg_req_res
)) != 0) {
734 (void) pthread_mutex_unlock(&_request
->sip_msg_mutex
);
738 if (_request
->sip_msg_req_res
->U
.sip_request
.sip_request_uri
.
739 sip_str_ptr
== NULL
) {
740 (void) pthread_mutex_unlock(&_request
->sip_msg_mutex
);
743 uri
= (char *)malloc(_request
->sip_msg_req_res
->U
.sip_request
.
744 sip_request_uri
.sip_str_len
+ 1);
746 (void) pthread_mutex_unlock(&_request
->sip_msg_mutex
);
750 _request
->sip_msg_req_res
->U
.sip_request
.sip_request_uri
.
751 sip_str_ptr
, _request
->sip_msg_req_res
->U
.sip_request
.
752 sip_request_uri
.sip_str_len
);
753 uri
[_request
->sip_msg_req_res
->U
.sip_request
.
754 sip_request_uri
.sip_str_len
] = '\0';
755 if ((ret
= sip_add_request_line(_ack_msg
, ACK
, uri
)) != 0) {
756 (void) pthread_mutex_unlock(&_request
->sip_msg_mutex
);
760 if ((ret
= _sip_find_and_copy_header(_request
, _ack_msg
, SIP_VIA
,
761 NULL
, B_TRUE
)) != 0) {
762 (void) pthread_mutex_unlock(&_request
->sip_msg_mutex
);
765 (void) _sip_find_and_copy_header(_request
, _ack_msg
,
766 SIP_MAX_FORWARDS
, NULL
, B_TRUE
);
768 (void) pthread_mutex_lock(&_response
->sip_msg_mutex
);
769 if ((ret
= _sip_find_and_copy_header(_response
, _ack_msg
, SIP_TO
,
770 NULL
, B_TRUE
)) != 0) {
771 (void) pthread_mutex_unlock(&_response
->sip_msg_mutex
);
774 (void) pthread_mutex_unlock(&_response
->sip_msg_mutex
);
775 if ((ret
= _sip_find_and_copy_header(_request
, _ack_msg
, SIP_FROM
,
776 NULL
, B_TRUE
)) != 0) {
777 (void) pthread_mutex_unlock(&_request
->sip_msg_mutex
);
780 if ((ret
= _sip_find_and_copy_header(_request
, _ack_msg
, SIP_CALL_ID
,
781 NULL
, B_TRUE
)) != 0) {
782 (void) pthread_mutex_unlock(&_request
->sip_msg_mutex
);
785 (void) pthread_mutex_unlock(&_request
->sip_msg_mutex
);
786 seqno
= sip_get_callseq_num(_request
, &ret
);
789 if ((ret
= sip_add_cseq(_ack_msg
, ACK
, seqno
)) != 0)
791 if ((ret
= sip_adjust_msgbuf(_ack_msg
)) != 0)
797 * This is a 2XX ACK, for others ACK is constructed differently,
798 * esp. the branch id is retained.
801 sip_create_OKack(sip_msg_t response
, sip_msg_t ack_msg
, char *transport
,
802 char *sent_by
, int sent_by_port
, char *via_params
)
806 sip_parsed_header_t
*parsed_header
;
807 sip_hdr_value_t
*contact_value
;
808 _sip_header_t
*header
;
809 _sip_msg_t
*_response
;
810 _sip_msg_t
*_ack_msg
;
813 if (response
== NULL
|| response
== NULL
|| transport
== NULL
)
815 _response
= (_sip_msg_t
*)response
;
816 _ack_msg
= (_sip_msg_t
*)ack_msg
;
819 * Get URI from the response, Contact field
821 (void) pthread_mutex_lock(&_response
->sip_msg_mutex
);
822 if ((header
= sip_search_for_header(_response
, SIP_CONTACT
,
824 (void) pthread_mutex_unlock(&_response
->sip_msg_mutex
);
827 if ((ret
= sip_parse_cftr_header(header
, (void *)&parsed_header
)) !=
829 (void) pthread_mutex_unlock(&_response
->sip_msg_mutex
);
832 contact_value
= (sip_hdr_value_t
*)parsed_header
->value
;
833 if (contact_value
->cftr_uri
.sip_str_ptr
== NULL
) {
834 (void) pthread_mutex_unlock(&_response
->sip_msg_mutex
);
837 uri
= (char *)malloc(contact_value
->cftr_uri
.sip_str_len
+ 1);
839 (void) pthread_mutex_unlock(&_response
->sip_msg_mutex
);
842 (void) strncpy(uri
, contact_value
->cftr_uri
.sip_str_ptr
,
843 contact_value
->cftr_uri
.sip_str_len
);
844 uri
[contact_value
->cftr_uri
.sip_str_len
] = '\0';
845 if ((ret
= sip_add_request_line(_ack_msg
, ACK
, uri
)) != 0) {
846 (void) pthread_mutex_unlock(&_response
->sip_msg_mutex
);
850 if ((ret
= sip_add_via(_ack_msg
, transport
, sent_by
, sent_by_port
,
852 (void) pthread_mutex_unlock(&_response
->sip_msg_mutex
);
856 if ((ret
= _sip_find_and_copy_header(_response
, _ack_msg
, SIP_TO
,
857 NULL
, B_TRUE
)) != 0) {
858 (void) pthread_mutex_unlock(&_response
->sip_msg_mutex
);
861 if ((ret
= _sip_find_and_copy_header(_response
, _ack_msg
, SIP_FROM
,
862 NULL
, B_TRUE
)) != 0) {
863 (void) pthread_mutex_unlock(&_response
->sip_msg_mutex
);
866 if ((ret
= _sip_find_and_copy_header(_response
, _ack_msg
, SIP_CALL_ID
,
867 NULL
, B_TRUE
)) != 0) {
868 (void) pthread_mutex_unlock(&_response
->sip_msg_mutex
);
872 * Copy Max-Forward if present
874 if (sip_search_for_header(_response
, SIP_MAX_FORWARDS
, NULL
) != NULL
) {
875 if ((ret
= _sip_find_and_copy_header(_response
, _ack_msg
,
876 SIP_MAX_FORWARDS
, NULL
, B_TRUE
)) != 0) {
877 (void) pthread_mutex_unlock(&_response
->sip_msg_mutex
);
881 (void) pthread_mutex_unlock(&_response
->sip_msg_mutex
);
882 seqno
= sip_get_callseq_num(_response
, &ret
);
885 if ((ret
= sip_add_cseq(_ack_msg
, ACK
, seqno
)) != 0)
892 * Request-Line = Method SP Request-URI SP SIP-Version CRLF
895 sip_add_request_line(sip_msg_t sip_request
, sip_method_t method
,
898 _sip_header_t
*new_header
;
900 _sip_msg_t
*_sip_request
;
902 if (method
< INVITE
|| method
>= MAX_SIP_METHODS
||
903 request_uri
== NULL
|| sip_request
== NULL
) {
907 _sip_request
= (_sip_msg_t
*)sip_request
;
908 (void) pthread_mutex_lock(&_sip_request
->sip_msg_mutex
);
909 if (_sip_request
->sip_msg_cannot_be_modified
) {
910 (void) pthread_mutex_unlock(&_sip_request
->sip_msg_mutex
);
914 header_size
= strlen(sip_methods
[method
].name
) + SIP_SPACE_LEN
+
915 strlen(request_uri
) + SIP_SPACE_LEN
+ strlen(SIP_VERSION
) +
918 new_header
= sip_new_header(header_size
);
919 if (new_header
== NULL
) {
920 (void) pthread_mutex_unlock(&_sip_request
->sip_msg_mutex
);
923 new_header
->sip_hdr_sipmsg
= _sip_request
;
925 (void) snprintf(new_header
->sip_hdr_start
, header_size
+ 1,
926 "%s %s %s%s", sip_methods
[method
].name
, request_uri
,
927 SIP_VERSION
, SIP_CRLF
);
929 new_header
->sip_hdr_next
= _sip_request
->sip_msg_start_line
;
930 _sip_request
->sip_msg_start_line
= new_header
;
931 _sip_request
->sip_msg_len
+= header_size
;
932 (void) sip_parse_first_line(_sip_request
->sip_msg_start_line
,
933 &_sip_request
->sip_msg_req_res
);
934 if (_sip_request
->sip_msg_buf
!= NULL
)
935 _sip_request
->sip_msg_modified
= B_TRUE
;
936 (void) pthread_mutex_unlock(&_sip_request
->sip_msg_mutex
);