summaryrefslogtreecommitdiffstats
path: root/branches/1.0/tinySDP/src/tsdp_message.c
blob: 646bf11d5e1f73355bb7d1595f00b472dee1fad4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright (C) 2009-2010 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango.org>
*	
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*	
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*	
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/

/**@file tsdp_message.c
 * @brief SDP message.
 *
 * @author Mamadou Diop <diopmamadou(at)doubango.org>
 *
 * @date Created: Sat Nov 8 16:54:58 2009 mdiop
 */

#include "tinysdp/tsdp_message.h"

#include "tinysdp/headers/tsdp_header_O.h"
#include "tinysdp/headers/tsdp_header_S.h"
#include "tinysdp/headers/tsdp_header_T.h"
#include "tinysdp/headers/tsdp_header_V.h"

#include "tsk_debug.h"

#define TSDP_LINE_S_VALUE_DEFAULT "-"	/* as per RFC 3264 subclause 5 */

#define TSDP_LINE_O_USERNAME_DEFAULT	"doubango"
#define TSDP_LINE_O_SESSION_VER_DEFAULT	2301
#define TSDP_LINE_O_SESSION_ID_DEFAULT	1983

/*== Predicate function to find tsdp_header_t object by type. */
static int __pred_find_header_by_type(const tsk_list_item_t *item, const void *tsdp_htype)
{
	if(item && item->data){
		tsdp_header_t *header = item->data;
		tsdp_header_type_t htype = *((tsdp_header_type_t*)tsdp_htype);
		return (header->type - htype);
	}
	return -1;
}

/*== Predicate function to find tsdp_header_t object by name. */
static int __pred_find_header_by_name(const tsk_list_item_t *item, const void *name)
{
	if(item && item->data && name){
		tsdp_header_t *header = item->data;
		return tsdp_header_get_nameex(header) - *((const char*)name);
	}
	return -1;
}

/*== Predicate function to find media object by name. */
static int __pred_find_media_by_name(const tsk_list_item_t *item, const void *name)
{
	if(item && item->data && name){
		tsdp_header_t *header = item->data;
		if(header->type == tsdp_htype_M){
			return tsk_stricmp(((tsdp_header_M_t*)header)->media, (const char*)name);
		}
	}
	return -1;
}

tsdp_message_t* tsdp_message_create()
{
	return tsk_object_new(tsdp_message_def_t);
}

/*== Add headers/fmt to the media line */
int __add_headers(tsdp_header_M_t* m, va_list *ap)
{
	const tsk_object_def_t* objdef;
	tsdp_header_t *header;
	tsdp_fmt_t* fmt;
	
	if(!m){
		return -1;
	}
	
	while((objdef = va_arg(*ap, const tsk_object_def_t*))){
		if(objdef == tsdp_fmt_def_t){
			if((fmt = tsk_object_new_2(objdef, ap))){
				tsk_list_push_back_data(m->FMTs, (void**)&fmt);
			}
		}
		else{
			if((header = tsk_object_new_2(objdef, ap))){
				tsdp_header_M_add(m, header);
				TSK_OBJECT_SAFE_FREE(header);
			}
		}
	}
	return 0;
}

int tsdp_message_add_header(tsdp_message_t *self, const tsdp_header_t *hdr)
{
	if(self && hdr){
		tsdp_header_t *header = tsk_object_ref((void*)hdr);
		tsk_list_push_ascending_data(self->headers, (void**)&header); // Very important: Headers MUST appear in a fixed order (see ranks def).

		return 0;
	}
	return -1;
}

int tsdp_message_add_headers(tsdp_message_t *self, ...)
{
	const tsk_object_def_t* objdef;
	tsdp_header_t *header;
	va_list ap;

	if(!self){
		return -1;
	}

	va_start(ap, self);
	while((objdef = va_arg(ap, const tsk_object_def_t*))){
		if((header = tsk_object_new_2(objdef, &ap))){
			tsdp_message_add_header(self, header);
			TSK_OBJECT_SAFE_FREE(header);
		}
	}
	va_end(ap);

	return 0;
}

const tsdp_header_t *tsdp_message_get_headerAt(const tsdp_message_t *self, tsdp_header_type_t type, tsk_size_t index)
{
	tsk_size_t pos = 0;
	const tsk_list_item_t *item;
	const tsdp_header_t *hdr;

	if(!self || !self->headers){
		return tsk_null;
	}

	tsk_list_foreach(item, self->headers){
		hdr = item->data;
		if(hdr->type == type){
			if(pos++ >= index){
				return hdr;
			}
		}
	}
	
	return tsk_null;
}

const tsdp_header_t *tsdp_message_get_header(const tsdp_message_t *self, tsdp_header_type_t type)
{
	return tsdp_message_get_headerAt(self, type, 0);
}

const tsdp_header_t *tsdp_message_get_headerByName(const tsdp_message_t *self, char name)
{
	if(self){
		return tsk_list_find_object_by_pred(self->headers, __pred_find_header_by_name, &name);
	}
	else{
		TSK_DEBUG_ERROR("Invalid parameter");
		return tsk_null;
	}
}

int tsdp_message_serialize(const tsdp_message_t *self, tsk_buffer_t *output)
{
	const tsk_list_item_t* item;

	if(!self || !output){
		return -1;
	}
	
	tsk_list_foreach(item, self->headers){
		if(tsdp_header_serialize(TSDP_HEADER(item->data), output)){
			// Abort?
		}
	}
	
	return 0;
}

char* tsdp_message_tostring(const tsdp_message_t *self)
{
	tsk_buffer_t* output = tsk_buffer_create_null();
	char* ret = tsk_null;

	if(!tsdp_message_serialize(self, output)){
		ret = tsk_strndup(TSK_BUFFER_DATA(output), TSK_BUFFER_SIZE(output));
	}

	TSK_OBJECT_SAFE_FREE(output);
	return ret;
}

tsdp_message_t* tsdp_message_create_empty(const char* addr, tsk_bool_t ipv6, uint32_t version)
{
	tsdp_message_t* ret = 0;

	if(!(ret = tsdp_message_create())){
		return tsk_null;
	}

	/*	RFC 3264 - 5 Generating the Initial Offer
		The numeric value of the session id and version in the o line MUST be 
		representable with a 64 bit signed integer.  The initial value of the version MUST be less than
	   (2**62)-1, to avoid rollovers.
	*/
	TSDP_MESSAGE_ADD_HEADER(ret, TSDP_HEADER_V_VA_ARGS(0));
	TSDP_MESSAGE_ADD_HEADER(ret, TSDP_HEADER_O_VA_ARGS(
		TSDP_LINE_O_USERNAME_DEFAULT,
		TSDP_LINE_O_SESSION_ID_DEFAULT,
		version,
		"IN",
		ipv6 ? "IP6" : "IP4",
		addr));

	/*	RFC 3264 - 5 Generating the Initial Offer
		The SDP "s=" line conveys the subject of the session, which is
		reasonably defined for multicast, but ill defined for unicast.  For
		unicast sessions, it is RECOMMENDED that it consist of a single space
		character (0x20) or a dash (-).

		Unfortunately, SDP does not allow the "s=" line to be empty.
	*/
	TSDP_MESSAGE_ADD_HEADER(ret, TSDP_HEADER_S_VA_ARGS(TSDP_LINE_S_VALUE_DEFAULT));

	/*	RFC 3264 - 5 Generating the Initial Offer
		The SDP "t=" line conveys the time of the session.  Generally,
		streams for unicast sessions are created and destroyed through
		external signaling means, such as SIP.  In that case, the "t=" line
		SHOULD have a value of "0 0".
	*/
	TSDP_MESSAGE_ADD_HEADER(ret, TSDP_HEADER_T_VA_ARGS(0, 0));
	
	return ret;
}

tsdp_message_t* tsdp_message_clone(const tsdp_message_t *self)
{
	tsdp_message_t* clone = tsk_null;
	tsk_list_item_t* item;
	tsdp_header_t* header;

	if(!self){
		goto bail;
	}

	if((clone =  tsdp_message_create())){
		tsk_list_foreach(item, self->headers){
			if((header = tsdp_header_clone(TSDP_HEADER(item->data)))){
				tsk_list_push_back_data(clone->headers, (void**)&header);
			}
		}
	}


bail:
	return clone;
}

int tsdp_message_add_media(tsdp_message_t *self, const char* media, uint32_t port, const char* proto, ...)
{
	va_list ap;
	int ret;
		
	va_start(ap, proto);
	ret = tsdp_message_add_media_2(self, media, port, proto, &ap);
	va_end(ap);

	return ret;
}

int tsdp_message_add_media_2(tsdp_message_t *self, const char* media, uint32_t port, const char* proto, va_list *ap)
{
	int ret = -1;
	tsdp_header_M_t* m;

	if(!self){
		return -1;
	}

	if((m = tsdp_header_M_create(media, port, proto))){
		__add_headers(m, ap);
		
		ret = tsdp_message_add_header(self, TSDP_HEADER(m));
		TSK_OBJECT_SAFE_FREE(m);
	}
	
	return ret;
}

int tsdp_message_remove_media(tsdp_message_t *self, const char* media)
{
	int ret = -1;

	if(!self){
		goto bail;
	}

	tsk_list_remove_item_by_pred(self->headers, __pred_find_media_by_name, media);

bail:
	return ret;
}



/* ================= 3GPP TS 34.610 :: Communication HOLD (HOLD) using IP Multimedia (IM) Core ================*/
int tsdp_message_hold(tsdp_message_t* self, const char* media)
{
	tsdp_header_M_t* M;
	const tsk_list_item_t* item;

	if(!self){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}
	// 3GPP TS 34.610-900 - 4.5.2.1	Actions at the invoking UE
	if((item = tsk_list_find_item_by_pred(self->headers, __pred_find_media_by_name, media))){
		M = TSDP_HEADER_M(item->data);
		tsdp_header_M_hold(M, tsk_true);
	}

	return 0;
}

int tsdp_message_resume(tsdp_message_t* self, const char* media)
{
	tsdp_header_M_t* M;
	const tsk_list_item_t* item;

	if(!self){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}
	// 3GPP TS 34.610-900 - 4.5.2.1	Actions at the invoking UE
	if((item = tsk_list_find_item_by_pred(self->headers, __pred_find_media_by_name, media))){
		M = TSDP_HEADER_M(item->data);
		tsdp_header_M_resume(M, tsk_true);
	}

	return 0;
}




















//=================================================================================================
//	SDP object definition
//
static void* tsdp_message_ctor(void * self, va_list * app)
{
	tsdp_message_t *message = self;
	if(message){
		message->headers = tsk_list_create();
	}
	return self;
}

static void* tsdp_message_dtor(void * self)
{ 
	tsdp_message_t *message = self;
	if(message){
		TSK_OBJECT_SAFE_FREE(message->headers);
	}
	return self;
}

static const tsk_object_def_t tsdp_message_def_s = 
{
	sizeof(tsdp_message_t),
	tsdp_message_ctor, 
	tsdp_message_dtor,
	tsk_null, 
};
const tsk_object_def_t *tsdp_message_def_t = &tsdp_message_def_s;
OpenPOWER on IntegriCloud