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>
26 #include <osipparser2/osip_parser.h>
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. */
35 osip_message_set_header (osip_message_t * sip, const char *hname,
44 i = osip_header_init (&h);
48 h->hname = (char *) osip_malloc (strlen (hname) + 1);
55 osip_strncpy (h->hname, hname, strlen (hname));
56 osip_clrspace (h->hname);
59 { /* some headers can be null ("subject:") */
60 h->hvalue = (char *) osip_malloc (strlen (hvalue) + 1);
61 if (h->hvalue == NULL)
66 osip_strncpy (h->hvalue, hvalue, strlen (hvalue));
67 osip_clrspace (h->hvalue);
71 sip->message_property = 2;
72 osip_list_add (sip->headers, h, -1);
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. */
82 osip_message_set_topheader (osip_message_t * sip, const char *hname,
91 i = osip_header_init (&h);
95 h->hname = (char *) osip_malloc (strlen (hname) + 1);
102 osip_strncpy (h->hname, hname, strlen (hname));
103 osip_clrspace (h->hname);
106 { /* some headers can be null ("subject:") */
107 h->hvalue = (char *) osip_malloc (strlen (hvalue) + 1);
108 if (h->hvalue == NULL)
110 osip_header_free (h);
113 osip_strncpy (h->hvalue, hvalue, strlen (hvalue));
114 osip_clrspace (h->hvalue);
118 sip->message_property = 2;
119 osip_list_add (sip->headers, h, 0);
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. */
128 osip_message_get_header (const osip_message_t * sip, int pos,
129 osip_header_t ** dest)
132 if (osip_list_size (sip->headers) <= pos)
133 return -1; /* NULL */
134 *dest = (osip_header_t *) osip_list_get (sip->headers, pos);
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. */
144 osip_message_header_get_byname (const osip_message_t * sip, const char *hname,
145 int pos, osip_header_t ** dest)
152 if (osip_list_size (sip->headers) <= pos)
153 return -1; /* NULL */
154 while (osip_list_size (sip->headers) > i)
156 tmp = (osip_header_t *) osip_list_get (sip->headers, i);
157 if (osip_strcasecmp (tmp->hname, hname) == 0)
164 return -1; /* not found */
168 osip_header_init (osip_header_t ** header)
170 *header = (osip_header_t *) osip_malloc (sizeof (osip_header_t));
173 (*header)->hname = NULL;
174 (*header)->hvalue = NULL;
179 osip_header_free (osip_header_t * header)
183 osip_free (header->hname);
184 osip_free (header->hvalue);
185 header->hname = NULL;
186 header->hvalue = NULL;
191 /* returns the header as a string. */
192 /* INPUT : osip_header_t *header | header. */
193 /* returns null on error. */
195 osip_header_to_str (const osip_header_t * header, char **dest)
200 if ((header == NULL) || (header->hname == NULL))
204 if (header->hvalue != NULL)
205 len = strlen (header->hvalue);
207 *dest = (char *) osip_malloc (strlen (header->hname) + len + 3);
211 if (header->hvalue != NULL)
212 sprintf (*dest, "%s: %s", header->hname, header->hvalue);
214 sprintf (*dest, "%s: ", header->hname);
219 osip_header_get_name (const osip_header_t * header)
223 return header->hname;
227 osip_header_set_name (osip_header_t * header, char *name)
229 header->hname = name;
233 osip_header_get_value (const osip_header_t * header)
237 return header->hvalue;
241 osip_header_set_value (osip_header_t * header, char *value)
243 header->hvalue = value;
247 osip_header_clone (const osip_header_t * header, osip_header_t ** dest)
255 if (header->hname == NULL)
258 i = osip_header_init (&he);
261 he->hname = osip_strdup (header->hname);
262 if (header->hvalue != NULL)
263 he->hvalue = osip_strdup (header->hvalue);