and added files
[bcm963xx.git] / userapps / opensource / libosip2 / src / osipparser2 / osip_header.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
21 #include <stdlib.h>
22 #include <stdio.h>
23
24 #include <osipparser2/osip_port.h>
25 #include <osipparser2/osip_message.h>
26 #include <osipparser2/osip_parser.h>
27
28
29 /* Add a header to a SIP message.                           */
30 /* INPUT :  char *hname | pointer to a header name.         */
31 /* INPUT :  char *hvalue | pointer to a header value.       */
32 /* OUTPUT: osip_message_t *sip | structure to save results.          */
33 /* returns -1 on error. */
34 int
35 osip_message_set_header (osip_message_t * sip, const char *hname,
36                          const char *hvalue)
37 {
38   osip_header_t *h;
39   int i;
40
41   if (hname == NULL)
42     return -1;
43
44   i = osip_header_init (&h);
45   if (i != 0)
46     return -1;
47
48   h->hname = (char *) osip_malloc (strlen (hname) + 1);
49
50   if (h->hname == NULL)
51     {
52       osip_header_free (h);
53       return -1;
54     }
55   osip_strncpy (h->hname, hname, strlen (hname));
56   osip_clrspace (h->hname);
57
58   if (hvalue != NULL)
59     {                           /* some headers can be null ("subject:") */
60       h->hvalue = (char *) osip_malloc (strlen (hvalue) + 1);
61       if (h->hvalue == NULL)
62         {
63           osip_header_free (h);
64           return -1;
65         }
66       osip_strncpy (h->hvalue, hvalue, strlen (hvalue));
67       osip_clrspace (h->hvalue);
68     }
69   else
70     h->hvalue = NULL;
71   sip->message_property = 2;
72   osip_list_add (sip->headers, h, -1);
73   return 0;                     /* ok */
74 }
75
76 /* Add a header to a SIP message at the top of the list.    */
77 /* INPUT :  char *hname | pointer to a header name.         */
78 /* INPUT :  char *hvalue | pointer to a header value.       */
79 /* OUTPUT: osip_message_t *sip | structure to save results.          */
80 /* returns -1 on error. */
81 int
82 osip_message_set_topheader (osip_message_t * sip, const char *hname,
83                             const char *hvalue)
84 {
85   osip_header_t *h;
86   int i;
87
88   if (hname == NULL)
89     return -1;
90
91   i = osip_header_init (&h);
92   if (i != 0)
93     return -1;
94
95   h->hname = (char *) osip_malloc (strlen (hname) + 1);
96
97   if (h->hname == NULL)
98     {
99       osip_header_free (h);
100       return -1;
101     }
102   osip_strncpy (h->hname, hname, strlen (hname));
103   osip_clrspace (h->hname);
104
105   if (hvalue != NULL)
106     {                           /* some headers can be null ("subject:") */
107       h->hvalue = (char *) osip_malloc (strlen (hvalue) + 1);
108       if (h->hvalue == NULL)
109         {
110           osip_header_free (h);
111           return -1;
112         }
113       osip_strncpy (h->hvalue, hvalue, strlen (hvalue));
114       osip_clrspace (h->hvalue);
115     }
116   else
117     h->hvalue = NULL;
118   sip->message_property = 2;
119   osip_list_add (sip->headers, h, 0);
120   return 0;                     /* ok */
121 }
122
123 /* Get a header in a SIP message.                       */
124 /* INPUT : int pos | position of number in message.     */
125 /* OUTPUT: osip_message_t *sip | structure to scan for a header .*/
126 /* return null on error. */
127 int
128 osip_message_get_header (const osip_message_t * sip, int pos,
129                          osip_header_t ** dest)
130 {
131   *dest = NULL;
132   if (osip_list_size (sip->headers) <= pos)
133     return -1;                  /* NULL */
134   *dest = (osip_header_t *) osip_list_get (sip->headers, pos);
135   return 0;
136 }
137
138 /* Get a header in a SIP message.                       */
139 /* INPUT : int pos | position where we start the search */
140 /* OUTPUT: osip_message_t *sip | structure to look for header.   */
141 /* return the current position of the header found      */
142 /* and -1 on error. */
143 int
144 osip_message_header_get_byname (const osip_message_t * sip, const char *hname,
145                                 int pos, osip_header_t ** dest)
146 {
147   int i;
148   osip_header_t *tmp;
149
150   *dest = NULL;
151   i = pos;
152   if (osip_list_size (sip->headers) <= pos)
153     return -1;                  /* NULL */
154   while (osip_list_size (sip->headers) > i)
155     {
156       tmp = (osip_header_t *) osip_list_get (sip->headers, i);
157       if (osip_strcasecmp (tmp->hname, hname) == 0)
158         {
159           *dest = tmp;
160           return i;
161         }
162       i++;
163     }
164   return -1;                    /* not found */
165 }
166
167 int
168 osip_header_init (osip_header_t ** header)
169 {
170   *header = (osip_header_t *) osip_malloc (sizeof (osip_header_t));
171   if (*header == NULL)
172     return -1;
173   (*header)->hname = NULL;
174   (*header)->hvalue = NULL;
175   return 0;
176 }
177
178 void
179 osip_header_free (osip_header_t * header)
180 {
181   if (header == NULL)
182     return;
183   osip_free (header->hname);
184   osip_free (header->hvalue);
185   header->hname = NULL;
186   header->hvalue = NULL;
187
188   osip_free (header);
189 }
190
191 /* returns the header as a string.    */
192 /* INPUT : osip_header_t *header | header. */
193 /* returns null on error. */
194 int
195 osip_header_to_str (const osip_header_t * header, char **dest)
196 {
197   size_t len;
198
199   *dest = NULL;
200   if ((header == NULL) || (header->hname == NULL))
201     return -1;
202
203   len = 0;
204   if (header->hvalue != NULL)
205     len = strlen (header->hvalue);
206
207   *dest = (char *) osip_malloc (strlen (header->hname) + len + 3);
208   if (*dest == NULL)
209     return -1;
210
211   if (header->hvalue != NULL)
212     sprintf (*dest, "%s: %s", header->hname, header->hvalue);
213   else
214     sprintf (*dest, "%s: ", header->hname);
215   return 0;
216 }
217
218 char *
219 osip_header_get_name (const osip_header_t * header)
220 {
221   if (header == NULL)
222     return NULL;
223   return header->hname;
224 }
225
226 void
227 osip_header_set_name (osip_header_t * header, char *name)
228 {
229   header->hname = name;
230 }
231
232 char *
233 osip_header_get_value (const osip_header_t * header)
234 {
235   if (header == NULL)
236     return NULL;
237   return header->hvalue;
238 }
239
240 void
241 osip_header_set_value (osip_header_t * header, char *value)
242 {
243   header->hvalue = value;
244 }
245
246 int
247 osip_header_clone (const osip_header_t * header, osip_header_t ** dest)
248 {
249   int i;
250   osip_header_t *he;
251
252   *dest = NULL;
253   if (header == NULL)
254     return -1;
255   if (header->hname == NULL)
256     return -1;
257
258   i = osip_header_init (&he);
259   if (i != 0)
260     return -1;
261   he->hname = osip_strdup (header->hname);
262   if (header->hvalue != NULL)
263     he->hvalue = osip_strdup (header->hvalue);
264
265   *dest = he;
266   return 0;
267 }