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