2 The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
3 Copyright (C) 2001,2002,2003 Aymeric MOIZARD jack@atosc.org
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <osipparser2/osip_port.h>
25 #include <osipparser2/osip_message.h>
27 /* enable logging of memory accesses */
29 static int freesipcptr = 0;
32 const char *osip_protocol_version = "SIP/2.0";
36 osip_message_init (osip_message_t ** sip)
39 static int comptr = 0;
44 *sip = (osip_message_t *) osip_malloc (sizeof (osip_message_t));
46 osip_trace (__FILE__, __LINE__, TRACE_LEVEL0, stdout,
47 "<msg_write.c> osip_message_init() = %i, malloc-free = %i, address = %x\n",
48 comptr, freesipcptr, *sip);
52 (*sip)->sip_method = NULL;
53 (*sip)->sip_version = NULL;
54 (*sip)->status_code = 0;
55 (*sip)->reason_phrase = NULL;
56 (*sip)->req_uri = NULL;
58 (*sip)->accepts = (osip_list_t *) osip_malloc (sizeof (osip_list_t));
59 osip_list_init ((*sip)->accepts);
60 (*sip)->accept_encodings =
61 (osip_list_t *) osip_malloc (sizeof (osip_list_t));
62 osip_list_init ((*sip)->accept_encodings);
63 (*sip)->accept_languages =
64 (osip_list_t *) osip_malloc (sizeof (osip_list_t));
66 osip_list_init ((*sip)->accept_languages);
67 (*sip)->alert_infos = (osip_list_t *) osip_malloc (sizeof (osip_list_t));
68 osip_list_init ((*sip)->alert_infos);
69 (*sip)->allows = (osip_list_t *) osip_malloc (sizeof (osip_list_t));
70 osip_list_init ((*sip)->allows);
71 (*sip)->authorizations = (osip_list_t *) osip_malloc (sizeof (osip_list_t));
72 osip_list_init ((*sip)->authorizations);
73 (*sip)->call_id = NULL;
74 (*sip)->call_infos = (osip_list_t *) osip_malloc (sizeof (osip_list_t));
75 osip_list_init ((*sip)->call_infos);
76 (*sip)->contacts = (osip_list_t *) osip_malloc (sizeof (osip_list_t));
77 osip_list_init ((*sip)->contacts);
79 /* forget it: this field is not suported */
80 (*sip)->content_dispositions = NULL;
82 (*sip)->content_encodings =
83 (osip_list_t *) osip_malloc (sizeof (osip_list_t));
84 osip_list_init ((*sip)->content_encodings);
85 (*sip)->content_length = NULL;
86 (*sip)->content_type = NULL;
88 (*sip)->error_infos = (osip_list_t *) osip_malloc (sizeof (osip_list_t));
89 osip_list_init ((*sip)->error_infos);
91 (*sip)->mime_version = NULL;
92 (*sip)->proxy_authenticates =
93 (osip_list_t *) osip_malloc (sizeof (osip_list_t));
94 osip_list_init ((*sip)->proxy_authenticates);
95 (*sip)->proxy_authorizations =
96 (osip_list_t *) osip_malloc (sizeof (osip_list_t));
97 osip_list_init ((*sip)->proxy_authorizations);
98 (*sip)->record_routes = (osip_list_t *) osip_malloc (sizeof (osip_list_t));
99 osip_list_init ((*sip)->record_routes);
100 (*sip)->routes = (osip_list_t *) osip_malloc (sizeof (osip_list_t));
101 osip_list_init ((*sip)->routes);
103 (*sip)->vias = (osip_list_t *) osip_malloc (sizeof (osip_list_t));
104 osip_list_init ((*sip)->vias);
105 (*sip)->www_authenticates =
106 (osip_list_t *) osip_malloc (sizeof (osip_list_t));
107 osip_list_init ((*sip)->www_authenticates);
109 (*sip)->bodies = (osip_list_t *) osip_malloc (sizeof (osip_list_t));
110 osip_list_init ((*sip)->bodies);
112 (*sip)->headers = (osip_list_t *) osip_malloc (sizeof (osip_list_t));
113 osip_list_init ((*sip)->headers);
115 (*sip)->message_property = 3;
116 (*sip)->message = NULL; /* buffer to avoid calling osip_message_to_str many times (for retransmission) */
122 osip_message_set_reason_phrase (osip_message_t * sip, char *reason)
124 sip->reason_phrase = reason;
128 osip_message_set_status_code (osip_message_t * sip, int status_code)
130 sip->status_code = status_code;
134 osip_message_set_method (osip_message_t * sip, char *sip_method)
136 sip->sip_method = sip_method;
140 osip_message_set_version (osip_message_t * sip, char *sip_version)
142 sip->sip_version = sip_version;
146 osip_message_set_uri (osip_message_t * sip, osip_uri_t * url)
152 osip_message_free (osip_message_t * sip)
157 static int comptr = 0;
163 osip_trace (__FILE__, __LINE__, TRACE_LEVEL0, stdout,
164 "<msg_write.c> osip_message_free() = %i, malloc-free = %i, address = %x\n",
165 comptr, freesipcptr, sip);
170 osip_free (sip->sip_method);
171 osip_free (sip->sip_version);
172 if (sip->req_uri != NULL)
174 osip_uri_free (sip->req_uri);
176 osip_free (sip->reason_phrase);
179 osip_accept_t *accept;
181 while (!osip_list_eol (sip->accepts, pos))
183 accept = (osip_accept_t *) osip_list_get (sip->accepts, pos);
184 osip_list_remove (sip->accepts, pos);
185 osip_accept_free (accept);
187 osip_free (sip->accepts);
190 osip_accept_encoding_t *accept_encoding;
192 while (!osip_list_eol (sip->accept_encodings, pos))
195 (osip_accept_encoding_t *) osip_list_get (sip->accept_encodings,
197 osip_list_remove (sip->accept_encodings, pos);
198 osip_accept_encoding_free (accept_encoding);
200 osip_free (sip->accept_encodings);
203 osip_accept_language_t *accept_language;
205 while (!osip_list_eol (sip->accept_languages, pos))
208 (osip_accept_language_t *) osip_list_get (sip->accept_languages,
210 osip_list_remove (sip->accept_languages, pos);
211 osip_accept_language_free (accept_language);
213 osip_free (sip->accept_languages);
216 osip_alert_info_t *alert_info;
218 while (!osip_list_eol (sip->alert_infos, pos))
221 (osip_alert_info_t *) osip_list_get (sip->alert_infos, pos);
222 osip_list_remove (sip->alert_infos, pos);
223 osip_alert_info_free (alert_info);
225 osip_free (sip->alert_infos);
230 while (!osip_list_eol (sip->allows, pos))
232 al = (osip_allow_t *) osip_list_get (sip->allows, pos);
233 osip_list_remove (sip->allows, pos);
234 osip_allow_free (al);
236 osip_free (sip->allows);
239 osip_authorization_t *al;
241 while (!osip_list_eol (sip->authorizations, pos))
244 (osip_authorization_t *) osip_list_get (sip->authorizations, pos);
245 osip_list_remove (sip->authorizations, pos);
246 osip_authorization_free (al);
248 osip_free (sip->authorizations);
250 if (sip->call_id != NULL)
252 osip_call_id_free (sip->call_id);
255 osip_call_info_t *call_info;
257 while (!osip_list_eol (sip->call_infos, pos))
259 call_info = (osip_call_info_t *) osip_list_get (sip->call_infos, pos);
260 osip_list_remove (sip->call_infos, pos);
261 osip_call_info_free (call_info);
263 osip_free (sip->call_infos);
266 osip_contact_t *contact;
268 while (!osip_list_eol (sip->contacts, pos))
270 contact = (osip_contact_t *) osip_list_get (sip->contacts, pos);
271 osip_list_remove (sip->contacts, pos);
272 osip_contact_free (contact);
274 osip_free (sip->contacts);
277 osip_content_encoding_t *ce;
279 while (!osip_list_eol (sip->content_encodings, pos))
282 (osip_content_encoding_t *) osip_list_get (sip->content_encodings,
284 osip_list_remove (sip->content_encodings, pos);
285 osip_content_encoding_free (ce);
287 osip_free (sip->content_encodings);
289 if (sip->content_length != NULL)
291 osip_content_length_free (sip->content_length);
293 if (sip->content_type != NULL)
295 osip_content_type_free (sip->content_type);
297 if (sip->cseq != NULL)
299 osip_cseq_free (sip->cseq);
302 osip_error_info_t *error_info;
304 while (!osip_list_eol (sip->error_infos, pos))
307 (osip_error_info_t *) osip_list_get (sip->error_infos, pos);
308 osip_list_remove (sip->error_infos, pos);
309 osip_error_info_free (error_info);
311 osip_free (sip->error_infos);
313 if (sip->from != NULL)
315 osip_from_free (sip->from);
317 if (sip->mime_version != NULL)
319 osip_mime_version_free (sip->mime_version);
322 osip_proxy_authenticate_t *al;
324 while (!osip_list_eol (sip->proxy_authenticates, pos))
327 (osip_proxy_authenticate_t *) osip_list_get (sip->
330 osip_list_remove (sip->proxy_authenticates, pos);
331 osip_proxy_authenticate_free (al);
333 osip_free (sip->proxy_authenticates);
336 osip_proxy_authorization_t *proxy_authorization;
338 while (!osip_list_eol (sip->proxy_authorizations, pos))
340 proxy_authorization =
341 (osip_proxy_authorization_t *) osip_list_get (sip->
342 proxy_authorizations,
344 osip_list_remove (sip->proxy_authorizations, pos);
345 osip_proxy_authorization_free (proxy_authorization);
347 osip_free (sip->proxy_authorizations);
350 osip_record_route_t *record_route;
352 while (!osip_list_eol (sip->record_routes, pos))
355 (osip_record_route_t *) osip_list_get (sip->record_routes, pos);
356 osip_list_remove (sip->record_routes, pos);
357 osip_record_route_free (record_route);
359 osip_free (sip->record_routes);
364 while (!osip_list_eol (sip->routes, pos))
366 route = (osip_route_t *) osip_list_get (sip->routes, pos);
367 osip_list_remove (sip->routes, pos);
368 osip_route_free (route);
370 osip_free (sip->routes);
374 osip_to_free (sip->to);
379 while (!osip_list_eol (sip->vias, pos))
381 via = (osip_via_t *) osip_list_get (sip->vias, pos);
382 osip_list_remove (sip->vias, pos);
385 osip_free (sip->vias);
388 osip_www_authenticate_t *al;
390 while (!osip_list_eol (sip->www_authenticates, pos))
393 (osip_www_authenticate_t *) osip_list_get (sip->www_authenticates,
395 osip_list_remove (sip->www_authenticates, pos);
396 osip_www_authenticate_free (al);
398 osip_free (sip->www_authenticates);
402 osip_header_t *header;
404 while (!osip_list_eol (sip->headers, pos))
406 header = (osip_header_t *) osip_list_get (sip->headers, pos);
407 osip_list_remove (sip->headers, pos);
408 osip_header_free (header);
410 osip_free (sip->headers);
416 while (!osip_list_eol (sip->bodies, pos))
418 body = (osip_body_t *) osip_list_get (sip->bodies, pos);
419 osip_list_remove (sip->bodies, pos);
420 osip_body_free (body);
422 osip_free (sip->bodies);
424 osip_free (sip->message);
430 osip_message_clone (const osip_message_t * sip, osip_message_t ** dest)
432 osip_message_t *copy;
440 i = osip_message_init (©);
444 copy->sip_method = osip_strdup (sip->sip_method);
445 copy->sip_version = osip_strdup (sip->sip_version);
446 copy->status_code = sip->status_code;
447 copy->reason_phrase = osip_strdup (sip->reason_phrase);
448 if (sip->req_uri != NULL)
450 i = osip_uri_clone (sip->req_uri, &(copy->req_uri));
456 osip_accept_t *accept;
457 osip_accept_t *accept2;
460 while (!osip_list_eol (sip->accepts, pos))
462 accept = (osip_accept_t *) osip_list_get (sip->accepts, pos);
463 i = osip_accept_clone (accept, &accept2);
466 osip_list_add (copy->accepts, accept2, -1); /* insert as last element */
471 osip_accept_encoding_t *accept_encoding;
472 osip_accept_encoding_t *accept_encoding2;
475 while (!osip_list_eol (sip->accept_encodings, pos))
478 (osip_accept_encoding_t *) osip_list_get (sip->accept_encodings,
480 i = osip_accept_encoding_clone (accept_encoding, &accept_encoding2);
483 osip_list_add (copy->accept_encodings, accept_encoding2, -1);
488 osip_accept_language_t *accept_language;
489 osip_accept_language_t *accept_language2;
492 while (!osip_list_eol (sip->accept_languages, pos))
495 (osip_accept_language_t *) osip_list_get (sip->accept_languages,
497 i = osip_accept_language_clone (accept_language, &accept_language2);
500 osip_list_add (copy->accept_languages, accept_language2, -1);
505 osip_alert_info_t *alert_info;
506 osip_alert_info_t *alert_info2;
509 while (!osip_list_eol (sip->alert_infos, pos))
512 (osip_alert_info_t *) osip_list_get (sip->alert_infos, pos);
513 i = osip_alert_info_clone (alert_info, &alert_info2);
516 osip_list_add (copy->alert_infos, alert_info2, -1);
522 osip_allow_t *allow2;
525 while (!osip_list_eol (sip->allows, pos))
527 allow = (osip_allow_t *) osip_list_get (sip->allows, pos);
528 i = osip_allow_clone (allow, &allow2);
531 osip_list_add (copy->allows, allow2, -1);
536 osip_authorization_t *authorization;
537 osip_authorization_t *authorization2;
540 while (!osip_list_eol (sip->authorizations, pos))
543 (osip_authorization_t *) osip_list_get (sip->authorizations, pos);
544 i = osip_authorization_clone (authorization, &authorization2);
547 osip_list_add (copy->authorizations, authorization2, -1);
551 if (sip->call_id != NULL)
553 i = osip_call_id_clone (sip->call_id, &(copy->call_id));
558 osip_call_info_t *call_info;
559 osip_call_info_t *call_info2;
562 while (!osip_list_eol (sip->call_infos, pos))
564 call_info = (osip_call_info_t *) osip_list_get (sip->call_infos, pos);
565 i = osip_call_info_clone (call_info, &call_info2);
568 osip_list_add (copy->call_infos, call_info2, -1);
573 osip_contact_t *contact;
574 osip_contact_t *contact2;
577 while (!osip_list_eol (sip->contacts, pos))
579 contact = (osip_contact_t *) osip_list_get (sip->contacts, pos);
580 i = osip_contact_clone (contact, &contact2);
583 osip_list_add (copy->contacts, contact2, -1);
588 osip_content_encoding_t *content_encoding;
589 osip_content_encoding_t *content_encoding2;
592 while (!osip_list_eol (sip->content_encodings, pos))
595 (osip_content_encoding_t *) osip_list_get (sip->content_encodings,
598 osip_content_encoding_clone (content_encoding, &content_encoding2);
601 osip_list_add (copy->content_encodings, content_encoding2, -1);
605 if (sip->content_length != NULL)
608 osip_content_length_clone (sip->content_length,
609 &(copy->content_length));
613 if (sip->content_type != NULL)
615 i = osip_content_type_clone (sip->content_type, &(copy->content_type));
619 if (sip->cseq != NULL)
621 i = osip_cseq_clone (sip->cseq, &(copy->cseq));
626 osip_error_info_t *error_info;
627 osip_error_info_t *error_info2;
630 while (!osip_list_eol (sip->error_infos, pos))
633 (osip_error_info_t *) osip_list_get (sip->error_infos, pos);
634 i = osip_error_info_clone (error_info, &error_info2);
637 osip_list_add (copy->error_infos, error_info2, -1);
641 if (sip->from != NULL)
643 i = osip_from_clone (sip->from, &(copy->from));
647 if (sip->mime_version != NULL)
649 i = osip_mime_version_clone (sip->mime_version, &(copy->mime_version));
654 osip_proxy_authenticate_t *proxy_authenticate;
655 osip_proxy_authenticate_t *proxy_authenticate2;
658 while (!osip_list_eol (sip->proxy_authenticates, pos))
661 (osip_proxy_authenticate_t *) osip_list_get (sip->
665 osip_proxy_authenticate_clone (proxy_authenticate,
666 &proxy_authenticate2);
669 osip_list_add (copy->proxy_authenticates, proxy_authenticate2, -1);
674 osip_proxy_authorization_t *proxy_authorization;
675 osip_proxy_authorization_t *proxy_authorization2;
678 while (!osip_list_eol (sip->proxy_authorizations, pos))
680 proxy_authorization =
681 (osip_proxy_authorization_t *) osip_list_get (sip->
682 proxy_authorizations,
685 osip_proxy_authorization_clone (proxy_authorization,
686 &proxy_authorization2);
689 osip_list_add (copy->proxy_authorizations, proxy_authorization2, -1);
694 osip_record_route_t *record_route;
695 osip_record_route_t *record_route2;
698 while (!osip_list_eol (sip->record_routes, pos))
701 (osip_record_route_t *) osip_list_get (sip->record_routes, pos);
702 i = osip_record_route_clone (record_route, &record_route2);
705 osip_list_add (copy->record_routes, record_route2, -1);
711 osip_route_t *route2;
714 while (!osip_list_eol (sip->routes, pos))
716 route = (osip_route_t *) osip_list_get (sip->routes, pos);
717 i = osip_route_clone (route, &route2);
720 osip_list_add (copy->routes, route2, -1);
726 i = osip_to_clone (sip->to, &(copy->to));
735 while (!osip_list_eol (sip->vias, pos))
737 via = (osip_via_t *) osip_list_get (sip->vias, pos);
738 i = osip_via_clone (via, &via2);
741 osip_list_add (copy->vias, via2, -1);
746 osip_www_authenticate_t *www_authenticate;
747 osip_www_authenticate_t *www_authenticate2;
750 while (!osip_list_eol (sip->www_authenticates, pos))
753 (osip_www_authenticate_t *) osip_list_get (sip->www_authenticates,
756 osip_www_authenticate_clone (www_authenticate, &www_authenticate2);
759 osip_list_add (copy->www_authenticates, www_authenticate2, -1);
765 osip_header_t *header;
766 osip_header_t *header2;
769 while (!osip_list_eol (sip->headers, pos))
771 header = (osip_header_t *) osip_list_get (sip->headers, pos);
772 i = osip_header_clone (header, &header2);
775 osip_list_add (copy->headers, header2, -1);
785 while (!osip_list_eol (sip->bodies, pos))
787 body = (osip_body_t *) osip_list_get (sip->bodies, pos);
788 i = osip_body_clone (body, &body2);
791 osip_list_add (copy->bodies, body2, -1);
796 copy->message = osip_strdup (sip->message);
797 copy->message_property = sip->message_property;
802 osip_message_free (copy);