and added files
[bcm963xx.git] / userapps / opensource / libosip2 / src / osipparser2 / osip_call_id.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 /* fills the call_id of message.                    */
28 /* INPUT : const char *hvalue | value of header.    */
29 /* OUTPUT: osip_message_t *sip | structure to save results.  */
30 /* returns -1 on error. */
31 int
32 osip_message_set_call_id (osip_message_t * sip, const char *hvalue)
33 {
34   int i;
35
36   if (hvalue == NULL || hvalue[0] == '\0')
37     return 0;
38
39   if (sip->call_id != NULL)
40     return -1;
41   i = osip_call_id_init (&(sip->call_id));
42   if (i != 0)
43     return -1;
44   sip->message_property = 2;
45   i = osip_call_id_parse (sip->call_id, hvalue);
46   if (i != 0)
47     {
48       osip_call_id_free (sip->call_id);
49       sip->call_id = NULL;
50       return -1;
51     }
52   return 0;
53 }
54
55 /* returns the call_id.               */
56 /* INPUT : osip_message_t *sip | sip message.  */
57 osip_call_id_t *
58 osip_message_get_call_id (const osip_message_t * sip)
59 {
60   return sip->call_id;
61 }
62
63 int
64 osip_call_id_init (osip_call_id_t ** callid)
65 {
66   *callid = (osip_call_id_t *) osip_malloc (sizeof (osip_call_id_t));
67   if (*callid == NULL)
68     return -1;
69   (*callid)->number = NULL;
70   (*callid)->host = NULL;
71   return 0;
72 }
73
74
75 /* deallocates a osip_call_id_t structure. */
76 /* INPUT : osip_call_id_t *| callid.       */
77 void
78 osip_call_id_free (osip_call_id_t * callid)
79 {
80   if (callid == NULL)
81     return;
82   osip_free (callid->number);
83   osip_free (callid->host);
84
85   callid->number = NULL;
86   callid->host = NULL;
87
88   osip_free (callid);
89 }
90
91 /* fills the call_id structure.                           */
92 /* INPUT : char *hvalue | value of header.                */
93 /* OUTPUT: osip_call_id_t *callid | structure to save results.  */
94 /* returns -1 on error. */
95 int
96 osip_call_id_parse (osip_call_id_t * callid, const char *hvalue)
97 {
98   const char *host;
99   const char *end;
100
101   callid->number = NULL;
102   callid->host = NULL;
103
104   host = strchr (hvalue, '@');  /* SEARCH FOR '@' */
105   end = hvalue + strlen (hvalue);
106
107   if (host == NULL)
108     host = end;
109   else
110     {
111       if (end - host + 1 < 2)
112         return -1;
113       callid->host = (char *) osip_malloc (end - host);
114       if (callid->host == NULL)
115         return -1;
116       osip_strncpy (callid->host, host + 1, end - host - 1);
117       osip_clrspace (callid->host);
118     }
119   if (host - hvalue + 1 < 2)
120     return -1;
121   callid->number = (char *) osip_malloc (host - hvalue + 1);
122   if (callid->number == NULL)
123     return -1;
124   osip_strncpy (callid->number, hvalue, host - hvalue);
125   osip_clrspace (callid->number);
126
127   return 0;                     /* ok */
128 }
129
130 /* returns the call_id as a string.          */
131 /* INPUT : osip_call_id_t *call_id | call_id.  */
132 /* returns null on error. */
133 int
134 osip_call_id_to_str (const osip_call_id_t * callid, char **dest)
135 {
136   *dest = NULL;
137   if ((callid == NULL) || (callid->number == NULL))
138     return -1;
139
140   if (callid->host == NULL)
141     {
142       *dest = (char *) osip_malloc (strlen (callid->number) + 1);
143       if (*dest == NULL)
144         return -1;
145       sprintf (*dest, "%s", callid->number);
146     }
147   else
148     {
149       *dest =
150         (char *) osip_malloc (strlen (callid->number) +
151                               strlen (callid->host) + 2);
152       if (*dest == NULL)
153         return -1;
154       sprintf (*dest, "%s@%s", callid->number, callid->host);
155     }
156   return 0;
157 }
158
159 char *
160 osip_call_id_get_number (osip_call_id_t * callid)
161 {
162   if (callid == NULL)
163     return NULL;
164   return callid->number;
165 }
166
167 char *
168 osip_call_id_get_host (osip_call_id_t * callid)
169 {
170   if (callid == NULL)
171     return NULL;
172   return callid->host;
173 }
174
175 void
176 osip_call_id_set_number (osip_call_id_t * callid, char *number)
177 {
178   callid->number = number;
179 }
180
181 void
182 osip_call_id_set_host (osip_call_id_t * callid, char *host)
183 {
184   callid->host = host;
185 }
186
187 int
188 osip_call_id_clone (const osip_call_id_t * callid, osip_call_id_t ** dest)
189 {
190   int i;
191   osip_call_id_t *ci;
192
193   *dest = NULL;
194   if (callid == NULL)
195     return -1;
196   if (callid->number == NULL)
197     return -1;
198
199   i = osip_call_id_init (&ci);
200   if (i == -1)                  /* allocation failed */
201     return -1;
202   ci->number = osip_strdup (callid->number);
203   if (callid->host != NULL)
204     ci->host = osip_strdup (callid->host);
205
206   *dest = ci;
207   return 0;
208 }
209
210 int
211 osip_call_id_match (osip_call_id_t * callid1, osip_call_id_t * callid2)
212 {
213
214   if (callid1 == NULL || callid2 == NULL)
215     return -1;
216   if (callid1->number == NULL || callid2->number == NULL)
217     return -1;
218
219   if (0 != strcmp (callid1->number, callid2->number))
220     return -1;
221
222   if ((callid1->host == NULL) && (callid2->host == NULL))
223     return 0;
224   if ((callid1->host == NULL) && (callid2->host != NULL))
225     return -1;
226   if ((callid1->host != NULL) && (callid2->host == NULL))
227     return -1;
228   if (0 != strcmp (callid1->host, callid2->host))
229     return -1;
230
231   return 0;
232 }