1 /*
2 The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
3 Copyright (C) 2001-2020 Aymeric MOIZARD amoizard@antisip.com
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 <osipparser2/internal.h>
21
22 #include <osipparser2/osip_port.h>
23 #include <osipparser2/osip_message.h>
24
25 /* enable logging of memory accesses */
26
27 const char *osip_protocol_version = "SIP/2.0";
28
osip_message_init(osip_message_t ** sip)29 int osip_message_init(osip_message_t **sip) {
30 *sip = (osip_message_t *) osip_malloc(sizeof(osip_message_t));
31
32 if (*sip == NULL)
33 return OSIP_NOMEM;
34
35 memset(*sip, 0, sizeof(osip_message_t));
36
37 #ifndef MINISIZE
38 osip_list_init(&(*sip)->accepts);
39 osip_list_init(&(*sip)->accept_encodings);
40
41 osip_list_init(&(*sip)->accept_languages);
42 osip_list_init(&(*sip)->alert_infos);
43 osip_list_init(&(*sip)->allows);
44 osip_list_init(&(*sip)->authentication_infos);
45 #endif
46 osip_list_init(&(*sip)->authorizations);
47 (*sip)->call_id = NULL;
48 osip_list_init(&(*sip)->call_infos);
49 osip_list_init(&(*sip)->contacts);
50
51 #ifndef MINISIZE
52 osip_list_init(&(*sip)->content_encodings);
53 #endif
54 (*sip)->content_length = NULL;
55 (*sip)->content_type = NULL;
56 (*sip)->cseq = NULL;
57 #ifndef MINISIZE
58 osip_list_init(&(*sip)->error_infos);
59 #endif
60 (*sip)->from = NULL;
61 (*sip)->mime_version = NULL;
62 osip_list_init(&(*sip)->proxy_authenticates);
63 #ifndef MINISIZE
64 osip_list_init(&(*sip)->proxy_authentication_infos);
65 #endif
66 osip_list_init(&(*sip)->proxy_authorizations);
67 osip_list_init(&(*sip)->record_routes);
68 osip_list_init(&(*sip)->routes);
69 (*sip)->to = NULL;
70 osip_list_init(&(*sip)->vias);
71 osip_list_init(&(*sip)->www_authenticates);
72
73 osip_list_init(&(*sip)->bodies);
74
75 osip_list_init(&(*sip)->headers);
76
77 (*sip)->message_property = 3;
78 (*sip)->message = NULL; /* buffer to avoid calling osip_message_to_str many times (for retransmission) */
79 (*sip)->message_length = 0;
80
81 (*sip)->application_data = NULL;
82 return OSIP_SUCCESS; /* ok */
83 }
84
osip_message_set_reason_phrase(osip_message_t * sip,char * reason)85 void osip_message_set_reason_phrase(osip_message_t *sip, char *reason) {
86 sip->reason_phrase = reason;
87 }
88
osip_message_set_status_code(osip_message_t * sip,int status_code)89 void osip_message_set_status_code(osip_message_t *sip, int status_code) {
90 sip->status_code = status_code;
91 }
92
osip_message_set_method(osip_message_t * sip,char * sip_method)93 void osip_message_set_method(osip_message_t *sip, char *sip_method) {
94 sip->sip_method = sip_method;
95 }
96
osip_message_set_version(osip_message_t * sip,char * sip_version)97 void osip_message_set_version(osip_message_t *sip, char *sip_version) {
98 sip->sip_version = sip_version;
99 }
100
osip_message_set_uri(osip_message_t * sip,osip_uri_t * url)101 void osip_message_set_uri(osip_message_t *sip, osip_uri_t *url) {
102 sip->req_uri = url;
103 }
104
osip_message_free(osip_message_t * sip)105 void osip_message_free(osip_message_t *sip) {
106 if (sip == NULL)
107 return;
108
109 osip_free(sip->sip_method);
110 osip_free(sip->sip_version);
111
112 if (sip->req_uri != NULL)
113 osip_uri_free(sip->req_uri);
114
115 osip_free(sip->reason_phrase);
116
117 #ifndef MINISIZE
118 osip_list_special_free(&sip->accepts, (void (*)(void *)) & osip_accept_free);
119 #endif
120 osip_list_special_free(&sip->authorizations, (void (*)(void *)) & osip_authorization_free);
121
122 if (sip->call_id != NULL)
123 osip_call_id_free(sip->call_id);
124
125 #ifndef MINISIZE
126 osip_list_special_free(&sip->accept_encodings, (void (*)(void *)) & osip_accept_encoding_free);
127 osip_list_special_free(&sip->accept_languages, (void (*)(void *)) & osip_accept_language_free);
128 osip_list_special_free(&sip->alert_infos, (void (*)(void *)) & osip_alert_info_free);
129 osip_list_special_free(&sip->allows, (void (*)(void *)) & osip_allow_free);
130 osip_list_special_free(&sip->authentication_infos, (void (*)(void *)) & osip_authentication_info_free);
131 osip_list_special_free(&sip->content_encodings, (void (*)(void *)) & osip_content_encoding_free);
132 osip_list_special_free(&sip->error_infos, (void (*)(void *)) & osip_error_info_free);
133 osip_list_special_free(&sip->proxy_authentication_infos, (void (*)(void *)) & osip_proxy_authentication_info_free);
134 #endif
135 osip_list_special_free(&sip->call_infos, (void (*)(void *)) & osip_call_info_free);
136 osip_list_special_free(&sip->contacts, (void (*)(void *)) & osip_contact_free);
137
138 if (sip->content_length != NULL)
139 osip_content_length_free(sip->content_length);
140
141 if (sip->content_type != NULL)
142 osip_content_type_free(sip->content_type);
143
144 if (sip->cseq != NULL)
145 osip_cseq_free(sip->cseq);
146
147 if (sip->from != NULL)
148 osip_from_free(sip->from);
149
150 if (sip->mime_version != NULL)
151 osip_mime_version_free(sip->mime_version);
152
153 osip_list_special_free(&sip->proxy_authenticates, (void (*)(void *)) & osip_proxy_authenticate_free);
154 osip_list_special_free(&sip->proxy_authorizations, (void (*)(void *)) & osip_proxy_authorization_free);
155 osip_list_special_free(&sip->record_routes, (void (*)(void *)) & osip_record_route_free);
156 osip_list_special_free(&sip->routes, (void (*)(void *)) & osip_route_free);
157
158 if (sip->to != NULL)
159 osip_to_free(sip->to);
160
161 osip_list_special_free(&sip->vias, (void (*)(void *)) & osip_via_free);
162 osip_list_special_free(&sip->www_authenticates, (void (*)(void *)) & osip_www_authenticate_free);
163 osip_list_special_free(&sip->headers, (void (*)(void *)) & osip_header_free);
164 osip_list_special_free(&sip->bodies, (void (*)(void *)) & osip_body_free);
165 osip_free(sip->message);
166 osip_free(sip);
167 }
168
osip_message_clone(const osip_message_t * sip,osip_message_t ** dest)169 int osip_message_clone(const osip_message_t *sip, osip_message_t **dest) {
170 osip_message_t *copy;
171 int i;
172
173 *dest = NULL;
174
175 if (sip == NULL)
176 return OSIP_BADPARAMETER;
177
178 i = osip_message_init(©);
179
180 if (i != 0)
181 return i;
182
183 copy->sip_method = osip_strdup(sip->sip_method);
184
185 if (sip->sip_method != NULL && copy->sip_method == NULL) {
186 osip_message_free(copy);
187 return OSIP_NOMEM;
188 }
189
190 copy->sip_version = osip_strdup(sip->sip_version);
191
192 if (sip->sip_version != NULL && copy->sip_version == NULL) {
193 osip_message_free(copy);
194 return OSIP_NOMEM;
195 }
196
197 copy->status_code = sip->status_code;
198 copy->reason_phrase = osip_strdup(sip->reason_phrase);
199
200 if (sip->reason_phrase != NULL && copy->reason_phrase == NULL) {
201 osip_message_free(copy);
202 return OSIP_NOMEM;
203 }
204
205 if (sip->req_uri != NULL) {
206 i = osip_uri_clone(sip->req_uri, &(copy->req_uri));
207
208 if (i != 0) {
209 osip_message_free(copy);
210 return i;
211 }
212 }
213
214 #ifndef MINISIZE
215 i = osip_list_clone(&sip->accepts, ©->accepts, (int (*)(void *, void **)) & osip_accept_clone);
216
217 if (i != 0) {
218 osip_message_free(copy);
219 return i;
220 }
221
222 i = osip_list_clone(&sip->accept_encodings, ©->accept_encodings, (int (*)(void *, void **)) & osip_accept_encoding_clone);
223
224 if (i != 0) {
225 osip_message_free(copy);
226 return i;
227 }
228
229 i = osip_list_clone(&sip->accept_languages, ©->accept_languages, (int (*)(void *, void **)) & osip_accept_language_clone);
230
231 if (i != 0) {
232 osip_message_free(copy);
233 return i;
234 }
235
236 i = osip_list_clone(&sip->alert_infos, ©->alert_infos, (int (*)(void *, void **)) & osip_alert_info_clone);
237
238 if (i != 0) {
239 osip_message_free(copy);
240 return i;
241 }
242
243 i = osip_list_clone(&sip->allows, ©->allows, (int (*)(void *, void **)) & osip_allow_clone);
244
245 if (i != 0) {
246 osip_message_free(copy);
247 return i;
248 }
249
250 i = osip_list_clone(&sip->authentication_infos, ©->authentication_infos, (int (*)(void *, void **)) & osip_authentication_info_clone);
251
252 if (i != 0) {
253 osip_message_free(copy);
254 return i;
255 }
256
257 i = osip_list_clone(&sip->content_encodings, ©->content_encodings, (int (*)(void *, void **)) & osip_content_encoding_clone);
258
259 if (i != 0) {
260 osip_message_free(copy);
261 return i;
262 }
263
264 i = osip_list_clone(&sip->error_infos, ©->error_infos, (int (*)(void *, void **)) & osip_error_info_clone);
265
266 if (i != 0) {
267 osip_message_free(copy);
268 return i;
269 }
270
271 i = osip_list_clone(&sip->proxy_authentication_infos, ©->proxy_authentication_infos, (int (*)(void *, void **)) & osip_proxy_authentication_info_clone);
272
273 if (i != 0) {
274 osip_message_free(copy);
275 return i;
276 }
277
278 #endif
279 i = osip_list_clone(&sip->call_infos, ©->call_infos, (int (*)(void *, void **)) & osip_call_info_clone);
280
281 if (i != 0) {
282 osip_message_free(copy);
283 return i;
284 }
285
286 i = osip_list_clone(&sip->authorizations, ©->authorizations, (int (*)(void *, void **)) & osip_authorization_clone);
287
288 if (i != 0) {
289 osip_message_free(copy);
290 return i;
291 }
292
293 if (sip->call_id != NULL) {
294 i = osip_call_id_clone(sip->call_id, &(copy->call_id));
295
296 if (i != 0) {
297 osip_message_free(copy);
298 return i;
299 }
300 }
301
302 i = osip_list_clone(&sip->contacts, ©->contacts, (int (*)(void *, void **)) & osip_contact_clone);
303
304 if (i != 0) {
305 osip_message_free(copy);
306 return i;
307 }
308
309 if (sip->content_length != NULL) {
310 i = osip_content_length_clone(sip->content_length, &(copy->content_length));
311
312 if (i != 0) {
313 osip_message_free(copy);
314 return i;
315 }
316 }
317
318 if (sip->content_type != NULL) {
319 i = osip_content_type_clone(sip->content_type, &(copy->content_type));
320
321 if (i != 0) {
322 osip_message_free(copy);
323 return i;
324 }
325 }
326
327 if (sip->cseq != NULL) {
328 i = osip_cseq_clone(sip->cseq, &(copy->cseq));
329
330 if (i != 0) {
331 osip_message_free(copy);
332 return i;
333 }
334 }
335
336 if (sip->from != NULL) {
337 i = osip_from_clone(sip->from, &(copy->from));
338
339 if (i != 0) {
340 osip_message_free(copy);
341 return i;
342 }
343 }
344
345 if (sip->mime_version != NULL) {
346 i = osip_mime_version_clone(sip->mime_version, &(copy->mime_version));
347
348 if (i != 0) {
349 osip_message_free(copy);
350 return i;
351 }
352 }
353
354 i = osip_list_clone(&sip->proxy_authenticates, ©->proxy_authenticates, (int (*)(void *, void **)) & osip_proxy_authenticate_clone);
355
356 if (i != 0) {
357 osip_message_free(copy);
358 return i;
359 }
360
361 i = osip_list_clone(&sip->proxy_authorizations, ©->proxy_authorizations, (int (*)(void *, void **)) & osip_proxy_authorization_clone);
362
363 if (i != 0) {
364 osip_message_free(copy);
365 return i;
366 }
367
368 i = osip_list_clone(&sip->record_routes, ©->record_routes, (int (*)(void *, void **)) & osip_record_route_clone);
369
370 if (i != 0) {
371 osip_message_free(copy);
372 return i;
373 }
374
375 i = osip_list_clone(&sip->routes, ©->routes, (int (*)(void *, void **)) & osip_route_clone);
376
377 if (i != 0) {
378 osip_message_free(copy);
379 return i;
380 }
381
382 if (sip->to != NULL) {
383 i = osip_to_clone(sip->to, &(copy->to));
384
385 if (i != 0) {
386 osip_message_free(copy);
387 return i;
388 }
389 }
390
391 i = osip_list_clone(&sip->vias, ©->vias, (int (*)(void *, void **)) & osip_via_clone);
392
393 if (i != 0) {
394 osip_message_free(copy);
395 return i;
396 }
397
398 i = osip_list_clone(&sip->www_authenticates, ©->www_authenticates, (int (*)(void *, void **)) & osip_www_authenticate_clone);
399
400 if (i != 0) {
401 osip_message_free(copy);
402 return i;
403 }
404
405 i = osip_list_clone(&sip->headers, ©->headers, (int (*)(void *, void **)) & osip_header_clone);
406
407 if (i != 0) {
408 osip_message_free(copy);
409 return i;
410 }
411
412 i = osip_list_clone(&sip->bodies, ©->bodies, (int (*)(void *, void **)) & osip_body_clone);
413
414 if (i != 0) {
415 osip_message_free(copy);
416 return i;
417 }
418
419 copy->message_length = sip->message_length;
420 copy->message = osip_strdup(sip->message);
421
422 if (copy->message == NULL && sip->message != NULL) {
423 osip_message_free(copy);
424 return OSIP_NOMEM;
425 }
426
427 copy->message_property = sip->message_property;
428 copy->application_data = sip->application_data;
429
430 *dest = copy;
431 return OSIP_SUCCESS;
432 }
433
osip_message_get_knownheaderlist(osip_list_t * header_list,int pos,void ** dest)434 int osip_message_get_knownheaderlist(osip_list_t *header_list, int pos, void **dest) {
435 *dest = NULL;
436
437 if (osip_list_size(header_list) <= pos)
438 return OSIP_UNDEFINED_ERROR; /* does not exist */
439
440 *dest = (void *) osip_list_get(header_list, pos);
441 return pos;
442 }
443