Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / libosip2 / src / osipparser2 / osip_record_route.c
1 /*
2   The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
3   Copyright (C) 2001,2002,2003  Aymeric MOIZARD jack@atosc.org
4   
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.
9   
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.
14   
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
18 */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22
23 #include <osipparser2/osip_port.h>
24 #include <osipparser2/osip_message.h>
25 #include <osipparser2/osip_parser.h>
26
27 int
28 osip_record_route_init (osip_record_route_t ** record_route)
29 {
30   return osip_from_init ((osip_from_t **) record_route);
31 }
32
33 /* adds the record_route header to message.         */
34 /* INPUT : const char *hvalue | value of header.    */
35 /* OUTPUT: osip_message_t *sip | structure to save results.  */
36 /* returns -1 on error. */
37 int
38 osip_message_set_record_route (osip_message_t * sip, const char *hvalue)
39 {
40   osip_record_route_t *record_route;
41   int i;
42
43   if (hvalue == NULL || hvalue[0] == '\0')
44     return 0;
45
46   i = osip_record_route_init (&record_route);
47   if (i != 0)
48     return -1;
49   i = osip_record_route_parse (record_route, hvalue);
50   if (i != 0)
51     {
52       osip_record_route_free (record_route);
53       return -1;
54     }
55   sip->message_property = 2;
56   osip_list_add (sip->record_routes, record_route, -1);
57   return 0;
58 }
59
60 /* returns the record_route header.    */
61 /* INPUT : osip_message_t *sip | sip message.   */
62 /* returns null on error. */
63 int
64 osip_message_get_record_route (const osip_message_t * sip, int pos,
65                                osip_record_route_t ** dest)
66 {
67   osip_record_route_t *record_route;
68
69   *dest = NULL;
70   if (osip_list_size (sip->record_routes) <= pos)
71     return -1;                  /* does not exist */
72   record_route =
73     (osip_record_route_t *) osip_list_get (sip->record_routes, pos);
74   *dest = record_route;
75   return pos;
76 }
77
78 int
79 osip_record_route_parse (osip_record_route_t * record_route,
80                          const char *hvalue)
81 {
82   return osip_from_parse ((osip_from_t *) record_route, hvalue);
83 }
84
85 /* returns the record_route header as a string.          */
86 /* INPUT : osip_record_route_t *record_route | record_route header.  */
87 /* returns -1 on error. */
88 int
89 osip_record_route_to_str (const osip_record_route_t * record_route,
90                           char **dest)
91 {
92   char *url;
93   char *buf;
94   int i;
95   size_t len;
96
97   *dest = NULL;
98   if ((record_route == NULL) || (record_route->url == NULL))
99     return -1;
100
101   i = osip_uri_to_str (record_route->url, &url);
102   if (i != 0)
103     return -1;
104
105   if (record_route->displayname == NULL)
106     len = strlen (url) + 5;
107   else
108     len = strlen (url) + strlen (record_route->displayname) + 5;
109
110   buf = (char *) osip_malloc (len);
111   if (buf == NULL)
112     {
113       osip_free (url);
114       return -1;
115     }
116
117   /* route and record-route always use brackets */
118   if (record_route->displayname != NULL)
119     sprintf (buf, "%s <%s>", record_route->displayname, url);
120   else
121     sprintf (buf, "<%s>", url);
122   osip_free (url);
123
124   {
125     int pos = 0;
126     osip_generic_param_t *u_param;
127     size_t plen;
128     char *tmp;
129
130     while (!osip_list_eol (record_route->gen_params, pos))
131       {
132         u_param =
133           (osip_generic_param_t *) osip_list_get (record_route->gen_params,
134                                                   pos);
135
136         if (u_param->gvalue == NULL)
137           plen = strlen (u_param->gname) + 2;
138         else
139           plen = strlen (u_param->gname) + strlen (u_param->gvalue) + 3;
140         len = len + plen;
141         buf = (char *) realloc (buf, len);
142         tmp = buf;
143         tmp = tmp + strlen (tmp);
144         if (u_param->gvalue == NULL)
145           sprintf (tmp, ";%s", u_param->gname);
146         else
147           sprintf (tmp, ";%s=%s", u_param->gname, u_param->gvalue);
148         pos++;
149       }
150   }
151   *dest = buf;
152   return 0;
153 }
154
155 /* deallocates a osip_record_route_t structure.  */
156 /* INPUT : osip_record_route_t *record_route | record_route header. */
157 void
158 osip_record_route_free (osip_record_route_t * record_route)
159 {
160   osip_from_free ((osip_from_t *) record_route);
161 }