belle_sip_headers_impl.c 46.3 KB
Newer Older
jehan's avatar
jehan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    This program 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.

    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/



#include "belle-sip/headers.h"
jehan's avatar
jehan committed
22
#include "belle-sip/parameters.h"
jehan's avatar
jehan committed
23 24 25 26 27 28
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "belle_sip_messageParser.h"
#include "belle_sip_messageLexer.h"
#include "belle_sip_internal.h"
jehan's avatar
jehan committed
29
#include "listeningpoint_internal.h"
jehan's avatar
jehan committed
30

jehan's avatar
jehan committed
31 32 33
/************************
 * header
 ***********************/
jehan's avatar
jehan committed
34

jehan's avatar
jehan committed
35
GET_SET_STRING(belle_sip_header,name);
36

jehan's avatar
jehan committed
37 38 39
belle_sip_header_t* belle_sip_header_create (const char* name,const char* value) {
	return BELLE_SIP_HEADER(belle_sip_header_extension_create(name,value));
}
jehan's avatar
jehan committed
40
void belle_sip_header_init(belle_sip_header_t *header) {
Simon Morlat's avatar
Simon Morlat committed
41

jehan's avatar
jehan committed
42
}
43
static void belle_sip_header_clone(belle_sip_header_t *header, const belle_sip_header_t *orig){
44
	CLONE_STRING(belle_sip_header,name,header,orig)
45 46 47 48
	if (belle_sip_header_get_next(orig)) {
		belle_sip_header_set_next(header,BELLE_SIP_HEADER(belle_sip_object_clone(BELLE_SIP_OBJECT(belle_sip_header_get_next(orig))))) ;
	}
}
Simon Morlat's avatar
Simon Morlat committed
49
static void belle_sip_header_destroy(belle_sip_header_t *header){
jehan's avatar
jehan committed
50
	if (header->name) belle_sip_free((void*)header->name);
jehan's avatar
jehan committed
51 52 53 54 55 56 57
	if (header->next) belle_sip_object_unref(BELLE_SIP_OBJECT(header->next));
}
void belle_sip_header_set_next(belle_sip_header_t* header,belle_sip_header_t* next) {
	header->next = next;
}
belle_sip_header_t* belle_sip_header_get_next(const belle_sip_header_t* header) {
	return header->next;
jehan's avatar
jehan committed
58
}
jehan's avatar
jehan committed
59

jehan's avatar
jehan committed
60 61
int belle_sip_header_marshal(belle_sip_header_t* header, char* buff,unsigned int offset,unsigned int buff_size) {
	if (header->name) {
62
		return snprintf(buff+offset,buff_size-offset,"%s: ",header->name);
jehan's avatar
jehan committed
63 64 65 66 67
	} else {
		belle_sip_warning("no header name found");
		return 0;
	}
}
68 69 70

BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_header_t);

Simon Morlat's avatar
Simon Morlat committed
71
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_header_t,belle_sip_object_t,belle_sip_header_destroy,belle_sip_header_clone,belle_sip_header_marshal,TRUE);
Simon Morlat's avatar
Simon Morlat committed
72 73


jehan's avatar
jehan committed
74 75 76 77
/************************
 * header_address
 ***********************/
struct _belle_sip_header_address {
Simon Morlat's avatar
Simon Morlat committed
78
	belle_sip_parameters_t base;
jehan's avatar
jehan committed
79 80 81
	const char* displayname;
	belle_sip_uri_t* uri;
};
Simon Morlat's avatar
Simon Morlat committed
82

jehan's avatar
jehan committed
83 84
static void belle_sip_header_address_init(belle_sip_header_address_t* object){
	belle_sip_parameters_init((belle_sip_parameters_t*)object); /*super*/
jehan's avatar
jehan committed
85 86
}

jehan's avatar
jehan committed
87 88 89
static void belle_sip_header_address_destroy(belle_sip_header_address_t* address) {
	if (address->displayname) belle_sip_free((void*)(address->displayname));
	if (address->uri) belle_sip_object_unref(BELLE_SIP_OBJECT(address->uri));
jehan's avatar
jehan committed
90 91
}

92
static void belle_sip_header_address_clone(belle_sip_header_address_t *addr, const belle_sip_header_address_t *orig){
93 94 95 96
	CLONE_STRING(belle_sip_header_address,displayname,addr,orig)
	if (belle_sip_header_address_get_uri(orig)) {
		belle_sip_header_address_set_uri(addr,BELLE_SIP_URI(belle_sip_object_clone(BELLE_SIP_OBJECT(belle_sip_header_address_get_uri(orig)))));
	}
Simon Morlat's avatar
Simon Morlat committed
97
}
98

jehan's avatar
jehan committed
99 100 101 102 103 104 105
int belle_sip_header_address_marshal(belle_sip_header_address_t* header, char* buff,unsigned int offset,unsigned int buff_size) {
	/*1 display name*/
	unsigned int current_offset=offset;
	if (header->displayname) {
		current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"\"%s\" ",header->displayname);;
	}
	if (header->uri) {
106 107 108 109 110
		/*cases where < is required*/
		if (header->displayname
			|| belle_sip_parameters_get_parameter_names((belle_sip_parameters_t*)header->uri)
			|| belle_sip_uri_get_header_names(header->uri)
			|| belle_sip_parameters_get_parameter_names(&header->base)) {
jehan's avatar
jehan committed
111 112 113
			current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%s","<");
		}
		current_offset+=belle_sip_uri_marshal(header->uri,buff,current_offset,buff_size);
114 115 116 117
		if (header->displayname
				|| belle_sip_parameters_get_parameter_names((belle_sip_parameters_t*)header->uri)
				|| belle_sip_uri_get_header_names(header->uri)
				|| belle_sip_parameters_get_parameter_names(&header->base)) {
jehan's avatar
jehan committed
118 119 120 121 122 123
			current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%s",">");
		}
	}
	current_offset+=belle_sip_parameters_marshal(&header->base,buff,current_offset,buff_size);
	return current_offset-offset;
}
Simon Morlat's avatar
Simon Morlat committed
124

125
BELLE_SIP_NEW_HEADER(header_address,parameters,"header_address")
jehan's avatar
jehan committed
126
BELLE_SIP_PARSE(header_address)
jehan's avatar
jehan committed
127 128
GET_SET_STRING(belle_sip_header_address,displayname);

jehan's avatar
jehan committed
129 130
void belle_sip_header_address_set_quoted_displayname(belle_sip_header_address_t* address,const char* value) {
		if (address->displayname != NULL) belle_sip_free((void*)(address->displayname));
jehan's avatar
jehan committed
131 132 133 134
		if (strlen(value)>2)
			address->displayname=_belle_sip_str_dup_and_unquote_string(value);
		else
			address->displayname=NULL;
jehan's avatar
jehan committed
135
}
136
belle_sip_uri_t* belle_sip_header_address_get_uri(const belle_sip_header_address_t* address) {
jehan's avatar
jehan committed
137 138 139 140
	return address->uri;
}

void belle_sip_header_address_set_uri(belle_sip_header_address_t* address, belle_sip_uri_t* uri) {
141
	address->uri=(belle_sip_uri_t*)belle_sip_object_ref(uri);
jehan's avatar
jehan committed
142 143
}

jehan's avatar
jehan committed
144 145 146 147 148 149
belle_sip_header_address_t* belle_sip_header_address_create(const char* display, belle_sip_uri_t* uri) {
	belle_sip_header_address_t* address = belle_sip_header_address_new();
	belle_sip_header_address_set_displayname(address,display);
	belle_sip_header_address_set_uri(address,uri);
	return address;
}
jehan's avatar
jehan committed
150

jehan's avatar
jehan committed
151
/******************************
152
 * Extension header inherits from header
jehan's avatar
jehan committed
153 154 155 156 157 158
 *
 ******************************/
struct _belle_sip_header_allow  {
	belle_sip_header_t header;
	const char* method;
};
159 160 161
static void belle_sip_header_allow_clone(belle_sip_header_allow_t *allow, const belle_sip_header_allow_t *orig){
	CLONE_STRING(belle_sip_header_allow,method,allow,orig)
}
jehan's avatar
jehan committed
162 163 164 165
static void belle_sip_header_allow_destroy(belle_sip_header_allow_t* allow) {
	if (allow->method) belle_sip_free((void*)allow->method);
}

166

jehan's avatar
jehan committed
167 168 169 170 171 172 173 174 175
int belle_sip_header_allow_marshal(belle_sip_header_allow_t* allow, char* buff,unsigned int offset,unsigned int buff_size) {
	unsigned int current_offset=offset;
	current_offset+=belle_sip_header_marshal(BELLE_SIP_HEADER(allow), buff,current_offset, buff_size);
	current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%s",allow->method);
	return current_offset-offset;

}
BELLE_SIP_NEW_HEADER(header_allow,header,"Allow")
BELLE_SIP_PARSE(header_allow)
jehan's avatar
jehan committed
176 177 178 179 180
belle_sip_header_allow_t* belle_sip_header_allow_create (const char* methods) {
	belle_sip_header_allow_t* allow = belle_sip_header_allow_new();
	belle_sip_header_allow_set_method(allow,methods);
	return allow;
}
jehan's avatar
jehan committed
181 182 183
GET_SET_STRING(belle_sip_header_allow,method);


jehan's avatar
jehan committed
184 185 186 187 188 189 190 191 192

/************************
 * header_contact
 ***********************/
struct _belle_sip_header_contact {
	belle_sip_header_address_t address;
	unsigned int wildcard;
 };

jehan's avatar
jehan committed
193
void belle_sip_header_contact_destroy(belle_sip_header_contact_t* contact) {
Simon Morlat's avatar
Simon Morlat committed
194 195 196
}

void belle_sip_header_contact_clone(belle_sip_header_contact_t *contact, const belle_sip_header_contact_t *orig){
197
	contact->wildcard=orig->wildcard;
jehan's avatar
jehan committed
198
}
jehan's avatar
jehan committed
199 200 201 202 203 204 205 206 207 208
int belle_sip_header_contact_marshal(belle_sip_header_contact_t* contact, char* buff,unsigned int offset,unsigned int buff_size) {
	unsigned int current_offset=offset;
	current_offset+=belle_sip_header_marshal(BELLE_SIP_HEADER(contact), buff,offset, buff_size);
	if (contact->wildcard) {
		current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%s","*");
	} else {
		current_offset+=belle_sip_header_address_marshal(&contact->address, buff,current_offset, buff_size);
	}
	return current_offset-offset;
}
jehan's avatar
jehan committed
209
BELLE_SIP_NEW_HEADER(header_contact,header_address,BELLE_SIP_CONTACT)
jehan's avatar
jehan committed
210
BELLE_SIP_PARSE(header_contact)
jehan's avatar
jehan committed
211 212
belle_sip_header_contact_t* belle_sip_header_contact_create (const belle_sip_header_address_t* contact) {
	belle_sip_header_contact_t* header = belle_sip_header_contact_new();
jehan's avatar
jehan committed
213 214
	_belle_sip_object_copy(BELLE_SIP_OBJECT(header),BELLE_SIP_OBJECT(contact));
	belle_sip_header_set_name(BELLE_SIP_HEADER(header),BELLE_SIP_CONTACT); /*restaure header name*/
jehan's avatar
jehan committed
215 216
	return header;
}
jehan's avatar
jehan committed
217 218
GET_SET_INT_PARAM_PRIVATE(belle_sip_header_contact,expires,int,_)
GET_SET_INT_PARAM_PRIVATE(belle_sip_header_contact,q,float,_);
jehan's avatar
jehan committed
219 220
GET_SET_BOOL(belle_sip_header_contact,wildcard,is);

jehan's avatar
jehan committed
221

jehan's avatar
jehan committed
222 223
int belle_sip_header_contact_set_expires(belle_sip_header_contact_t* contact, int expires) {
	if (expires < 0 ) {
224
		 belle_sip_error("bad expires value [%i] for contact",expires);
jehan's avatar
jehan committed
225 226 227 228 229 230
		return -1;
	}
	_belle_sip_header_contact_set_expires(contact,expires);
	return 0;
 }
int belle_sip_header_contact_set_qvalue(belle_sip_header_contact_t* contact, float qValue) {
jehan's avatar
jehan committed
231
	 if (qValue != -1 && qValue < 0 && qValue >1) {
232
		 belle_sip_error("bad q value [%f] for contact",qValue);
jehan's avatar
jehan committed
233 234
		 return -1;
	 }
jehan's avatar
jehan committed
235
	 _belle_sip_header_contact_set_q(contact,qValue);
jehan's avatar
jehan committed
236 237
	 return 0;
}
238
float	belle_sip_header_contact_get_qvalue(const belle_sip_header_contact_t* contact) {
jehan's avatar
jehan committed
239 240
	return belle_sip_header_contact_get_q(contact);
}
jehan's avatar
jehan committed
241 242
unsigned int belle_sip_header_contact_equals(const belle_sip_header_contact_t* a,const belle_sip_header_contact_t* b) {
	if (!a | !b) return 0;
243
	return belle_sip_uri_equals(belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(a))
jehan's avatar
jehan committed
244 245
								,belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(b)));
}
246 247 248
unsigned int belle_sip_header_contact_not_equals(const belle_sip_header_contact_t* a,const belle_sip_header_contact_t* b) {
	return !belle_sip_header_contact_equals(a,b);
}
jehan's avatar
jehan committed
249
/**************************
jehan's avatar
jehan committed
250
* From header object inherent from header_address
jehan's avatar
jehan committed
251
****************************
jehan's avatar
jehan committed
252
*/
jehan's avatar
jehan committed
253 254 255 256 257
#define BELLE_SIP_FROM_LIKE_MARSHAL(header) \
		unsigned int current_offset=offset; \
		current_offset+=belle_sip_##header_marshal(BELLE_SIP_HEADER(header), buff,current_offset, buff_size);\
		current_offset+=belle_sip_header_address_marshal(&header->address, buff,current_offset, buff_size); \
		return current_offset-offset;
258

jehan's avatar
jehan committed
259 260 261 262
struct _belle_sip_header_from  {
	belle_sip_header_address_t address;
};

jehan's avatar
jehan committed
263 264 265
static void belle_sip_header_from_destroy(belle_sip_header_from_t* from) {
}

Simon Morlat's avatar
Simon Morlat committed
266 267
static void belle_sip_header_from_clone(belle_sip_header_from_t* from, const belle_sip_header_from_t* cloned) {
}
jehan's avatar
jehan committed
268 269 270
int belle_sip_header_from_marshal(belle_sip_header_from_t* from, char* buff,unsigned int offset,unsigned int buff_size) {
	BELLE_SIP_FROM_LIKE_MARSHAL(from);
}
Simon Morlat's avatar
Simon Morlat committed
271

jehan's avatar
jehan committed
272
belle_sip_header_from_t* belle_sip_header_from_create2(const char *address, const char *tag){
273 274
	char *tmp=belle_sip_strdup_printf("From: %s",address);
	belle_sip_header_from_t *from=belle_sip_header_from_parse(tmp);
275
	if (from){
276
		if (tag) belle_sip_header_from_set_tag(from,tag);
277
	}
278 279 280
	belle_sip_free(tmp);
	return from;
}
jehan's avatar
jehan committed
281 282
belle_sip_header_from_t* belle_sip_header_from_create(const belle_sip_header_address_t* address, const char *tag) {
	belle_sip_header_from_t* header= belle_sip_header_from_new();
Simon Morlat's avatar
Simon Morlat committed
283
	_belle_sip_object_copy((belle_sip_object_t*)header,(belle_sip_object_t*)address);
jehan's avatar
jehan committed
284
	belle_sip_header_set_name(BELLE_SIP_HEADER(header),BELLE_SIP_FROM); /*restaure header name*/
285
	if (tag) belle_sip_header_from_set_tag(header,tag);
jehan's avatar
jehan committed
286 287
	return header;
}
jehan's avatar
jehan committed
288
BELLE_SIP_NEW_HEADER(header_from,header_address,BELLE_SIP_FROM)
jehan's avatar
jehan committed
289
BELLE_SIP_PARSE(header_from)
290
GET_SET_STRING_PARAM2(belle_sip_header_from,tag,raw_tag);
jehan's avatar
jehan committed
291

292 293 294
void belle_sip_header_from_set_random_tag(belle_sip_header_from_t *obj){
	char tmp[8];
	/*not less than 32bit */
295 296 297 298 299 300 301 302 303 304
	belle_sip_header_from_set_raw_tag(obj,belle_sip_random_token(tmp,sizeof(tmp)));
}

void belle_sip_header_from_set_tag(belle_sip_header_from_t *obj, const char *tag){
	if (tag==BELLE_SIP_RANDOM_TAG) belle_sip_header_from_set_random_tag(obj);
	else belle_sip_header_from_set_raw_tag(obj,tag);
}

const char *belle_sip_header_from_get_tag(const belle_sip_header_from_t *obj){
	return belle_sip_header_from_get_raw_tag(obj);
305 306
}

jehan's avatar
jehan committed
307
/**************************
Simon Morlat's avatar
Simon Morlat committed
308
* To header object inherits from header_address
jehan's avatar
jehan committed
309 310 311 312 313
****************************
*/
struct _belle_sip_header_to  {
	belle_sip_header_address_t address;
};
jehan's avatar
jehan committed
314

jehan's avatar
jehan committed
315
static void belle_sip_header_to_destroy(belle_sip_header_to_t* to) {
Simon Morlat's avatar
Simon Morlat committed
316 317 318
}

void belle_sip_header_to_clone(belle_sip_header_to_t *contact, const belle_sip_header_to_t *orig){
jehan's avatar
jehan committed
319
}
jehan's avatar
jehan committed
320 321 322
int belle_sip_header_to_marshal(belle_sip_header_to_t* to, char* buff,unsigned int offset,unsigned int buff_size) {
	BELLE_SIP_FROM_LIKE_MARSHAL(to)
}
jehan's avatar
jehan committed
323

jehan's avatar
fix tag  
jehan committed
324
BELLE_SIP_NEW_HEADER(header_to,header_address,BELLE_SIP_TO)
jehan's avatar
jehan committed
325
BELLE_SIP_PARSE(header_to)
326
GET_SET_STRING_PARAM2(belle_sip_header_to,tag,raw_tag);
327

jehan's avatar
jehan committed
328
belle_sip_header_to_t* belle_sip_header_to_create2(const char *address, const char *tag){
329 330
	char *tmp=belle_sip_strdup_printf("To: %s",address);
	belle_sip_header_to_t *to=belle_sip_header_to_parse(tmp);
331
	if (to){
332
		if (tag) belle_sip_header_to_set_tag(to,tag);
333
	}
334 335 336
	belle_sip_free(tmp);
	return to;
}
jehan's avatar
jehan committed
337 338
belle_sip_header_to_t* belle_sip_header_to_create(const belle_sip_header_address_t* address, const char *tag) {
	belle_sip_header_to_t* header= belle_sip_header_to_new();
Simon Morlat's avatar
Simon Morlat committed
339
	_belle_sip_object_copy((belle_sip_object_t*)header,(belle_sip_object_t*)address);
jehan's avatar
jehan committed
340
	belle_sip_header_set_name(BELLE_SIP_HEADER(header),BELLE_SIP_TO); /*restaure header name*/
jehan's avatar
fix tag  
jehan committed
341
	if (tag) belle_sip_header_to_set_tag(header,tag);
jehan's avatar
jehan committed
342 343
	return header;
}
344 345 346 347 348 349
void belle_sip_header_to_set_random_tag(belle_sip_header_to_t *obj){
	char tmp[8];
	/*not less than 32bit */
	belle_sip_header_to_set_tag(obj,belle_sip_random_token(tmp,sizeof(tmp)));
}

350 351 352 353 354 355 356 357 358 359
void belle_sip_header_to_set_tag(belle_sip_header_to_t *obj, const char *tag){
	if (tag==BELLE_SIP_RANDOM_TAG) belle_sip_header_to_set_random_tag(obj);
	else belle_sip_header_to_set_raw_tag(obj,tag);
}

const char *belle_sip_header_to_get_tag(const belle_sip_header_to_t *obj){
	return belle_sip_header_to_get_raw_tag(obj);
}


jehan's avatar
jehan committed
360
/******************************
361
 * User-Agent header inherits from header
jehan's avatar
jehan committed
362 363 364 365 366 367 368 369 370 371 372 373
 *
 ******************************/
struct _belle_sip_header_user_agent  {
	belle_sip_header_t header;
	belle_sip_list_t* products;
};

static void belle_sip_header_user_agent_destroy(belle_sip_header_user_agent_t* user_agent) {
	belle_sip_header_user_agent_set_products(user_agent,NULL);
}

static void belle_sip_header_user_agent_clone(belle_sip_header_user_agent_t* user_agent, const belle_sip_header_user_agent_t* orig){
374 375 376 377
	belle_sip_list_t* list=orig->products;
	for(;list!=NULL;list=list->next){
		belle_sip_header_user_agent_add_product(user_agent,(const char *)list->data);
	}
jehan's avatar
jehan committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391
}
int belle_sip_header_user_agent_marshal(belle_sip_header_user_agent_t* user_agent, char* buff,unsigned int offset,unsigned int buff_size) {
	unsigned int current_offset=offset;
	belle_sip_list_t* list = user_agent->products;
	current_offset+=belle_sip_header_marshal(BELLE_SIP_HEADER(user_agent), buff,current_offset, buff_size);
	for(;list!=NULL;list=list->next){
		current_offset+=snprintf(	buff+current_offset
									,buff_size-current_offset
									," %s"
									,(const char *)list->data);
	}
	return current_offset-offset;

}
392
BELLE_SIP_NEW_HEADER(header_user_agent,header,"User-Agent")
jehan's avatar
jehan committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
BELLE_SIP_PARSE(header_user_agent)
belle_sip_list_t* belle_sip_header_user_agent_get_products(const belle_sip_header_user_agent_t* user_agent) {
	return user_agent->products;
}
void belle_sip_header_user_agent_set_products(belle_sip_header_user_agent_t* user_agent,belle_sip_list_t* products) {
	belle_sip_list_t* list;
	if (user_agent->products) {
		for (list=user_agent->products;list !=NULL; list=list->next) {
			belle_sip_free((void*)list->data);

		}
		belle_sip_list_free(user_agent->products);
	}
	user_agent->products=products;
}
void belle_sip_header_user_agent_add_product(belle_sip_header_user_agent_t* user_agent,const char* product) {
	user_agent->products = belle_sip_list_append(user_agent->products ,belle_sip_strdup(product));
}
jehan's avatar
jehan committed
411 412 413 414 415 416 417 418 419 420 421 422 423
int belle_sip_header_user_agent_get_products_as_string(const belle_sip_header_user_agent_t* user_agent,char* value,unsigned int value_size) {
	int result = 0;
	belle_sip_list_t* list = user_agent->products;
	for(;list!=NULL;list=list->next){
		result+=snprintf(value+result
						,value_size-result
						,"%s "
						,(const char *)list->data);
	}
	if (result>0) value[result]='\0'; /*remove last space */

	return result-1;
}
jehan's avatar
jehan committed
424

jehan's avatar
jehan committed
425
/**************************
Simon Morlat's avatar
Simon Morlat committed
426
* Via header object inherits from parameters
jehan's avatar
jehan committed
427 428 429
****************************
*/
struct _belle_sip_header_via  {
430
	belle_sip_parameters_t params_list;
Simon Morlat's avatar
Simon Morlat committed
431 432 433
	char* protocol;
	char* transport;
	char* host;
434
	int port;
jehan's avatar
jehan committed
435 436
};

437
static void belle_sip_header_via_destroy(belle_sip_header_via_t* via) {
Simon Morlat's avatar
Simon Morlat committed
438
	if (via->protocol) belle_sip_free(via->protocol);
439 440
	if (via->transport) belle_sip_free(via->transport);
	if (via->host) belle_sip_free(via->host);
Simon Morlat's avatar
Simon Morlat committed
441 442 443
}

static void belle_sip_header_via_clone(belle_sip_header_via_t* via, const belle_sip_header_via_t*orig){
444 445 446 447
	CLONE_STRING(belle_sip_header_via,protocol,via,orig)
	CLONE_STRING(belle_sip_header_via,transport,via,orig)
	CLONE_STRING(belle_sip_header_via,host,via,orig)
	via->port=orig->port;
jehan's avatar
jehan committed
448
}
449

jehan's avatar
jehan committed
450 451 452 453 454 455 456 457 458 459 460
int belle_sip_header_via_marshal(belle_sip_header_via_t* via, char* buff,unsigned int offset,unsigned int buff_size) {
	unsigned int current_offset=offset;
	current_offset+=belle_sip_header_marshal(BELLE_SIP_HEADER(via), buff,current_offset, buff_size);
	current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%s/%s",via->protocol,via->transport);
	current_offset+=snprintf(buff+current_offset,buff_size-current_offset," %s",via->host);
	if (via->port > 0) {
		current_offset+=snprintf(buff+current_offset,buff_size-current_offset,":%i",via->port);
	}
	current_offset+=belle_sip_parameters_marshal(&via->params_list, buff,current_offset, buff_size);
	return current_offset-offset;
}
jehan's avatar
jehan committed
461

462 463 464 465 466 467 468 469 470 471
belle_sip_header_via_t* belle_sip_header_via_create(const char *host, int port, const char *transport, const char *branch){
	belle_sip_header_via_t *via=belle_sip_header_via_new();
	via->host=belle_sip_strdup(host);
	via->port=port;
	via->transport=belle_sip_strdup(transport);
	via->protocol=belle_sip_strdup("SIP/2.0");
	belle_sip_header_via_set_branch(via,branch);
	return via;
}

jehan's avatar
jehan committed
472
BELLE_SIP_NEW_HEADER(header_via,parameters,BELLE_SIP_VIA)
jehan's avatar
jehan committed
473
BELLE_SIP_PARSE(header_via)
474 475 476 477 478 479 480 481 482 483 484
GET_SET_STRING(belle_sip_header_via,protocol);
GET_SET_STRING(belle_sip_header_via,transport);
GET_SET_STRING(belle_sip_header_via,host);
GET_SET_INT_PRIVATE(belle_sip_header_via,port,int,_);

GET_SET_STRING_PARAM(belle_sip_header_via,branch);
GET_SET_STRING_PARAM(belle_sip_header_via,maddr);
GET_SET_STRING_PARAM(belle_sip_header_via,received);

GET_SET_INT_PARAM_PRIVATE(belle_sip_header_via,rport,int,_)
GET_SET_INT_PARAM_PRIVATE(belle_sip_header_via,ttl,int,_)
Simon Morlat's avatar
Simon Morlat committed
485

486
int belle_sip_header_via_set_rport (belle_sip_header_via_t* obj,int  value) {
jehan's avatar
jehan committed
487 488 489 490 491
	if (value == -1) {
		belle_sip_parameters_set_parameter(BELLE_SIP_PARAMETERS(obj),"rport",NULL);
		return 0;
	}
	if (value>0 && value<65536) {
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
		_belle_sip_header_via_set_rport(obj,value);
		return 0;
	} else {
		belle_sip_error("bad rport value [%i] for via",value);
		return -1;
	}
}
int belle_sip_header_via_set_ttl (belle_sip_header_via_t* obj,int  value) {
	if (value ==-1 || (value>0 && value<=255)) {
		_belle_sip_header_via_set_ttl(obj,value);
		return 0;
	} else {
		belle_sip_error("bad ttl value [%i] for via",value);
		return -1;
	}
}
508

509 510 511 512 513 514 515 516 517
int belle_sip_header_via_set_port (belle_sip_header_via_t* obj,int  value) {
	if (value ==-1 || (value>0 && value<65536)) {
		_belle_sip_header_via_set_port(obj,value);
		return 0;
	} else {
		belle_sip_error("bad port value [%i] for via",value);
		return -1;
	}
}
Simon Morlat's avatar
Simon Morlat committed
518

519
int belle_sip_header_via_get_listening_port(const belle_sip_header_via_t *via){
Simon Morlat's avatar
Simon Morlat committed
520 521 522 523
	int ret=belle_sip_header_via_get_port(via);
	if (ret==-1) ret=belle_sip_listening_point_get_well_known_port(via->protocol);
	return ret;
}
524 525 526 527 528 529 530 531 532 533
const char*	belle_sip_header_via_get_transport_lowercase(const belle_sip_header_via_t* via) {
	if (strcasecmp("udp",via->transport)==0) return "udp";
	else if (strcasecmp("tcp",via->transport)==0) return "tcp";
	else if (strcasecmp("tls",via->transport)==0) return "tls";
	else if (strcasecmp("dtls",via->transport)==0) return "dtls";
	else {
		belle_sip_warning("Cannot convert [%s] to lower case",via->transport);
		return via->transport;
	}
}
534
/**************************
Simon Morlat's avatar
Simon Morlat committed
535
* call_id header object inherits from object
536 537
****************************
*/
jehan's avatar
jehan committed
538
struct _belle_sip_header_call_id  {
jehan's avatar
jehan committed
539
	belle_sip_header_t header;
jehan's avatar
jehan committed
540
	const char* call_id;
541
};
jehan's avatar
jehan committed
542

jehan's avatar
jehan committed
543 544
static void belle_sip_header_call_id_destroy(belle_sip_header_call_id_t* call_id) {
	if (call_id->call_id) belle_sip_free((void*)call_id->call_id);
Simon Morlat's avatar
Simon Morlat committed
545
}
jehan's avatar
jehan committed
546

Simon Morlat's avatar
Simon Morlat committed
547
static void belle_sip_header_call_id_clone(belle_sip_header_call_id_t* call_id,const belle_sip_header_call_id_t *orig){
548
	CLONE_STRING(belle_sip_header_call_id,call_id,call_id,orig);
549
}
jehan's avatar
jehan committed
550 551 552 553 554 555
int belle_sip_header_call_id_marshal(belle_sip_header_call_id_t* call_id, char* buff,unsigned int offset,unsigned int buff_size) {
	unsigned int current_offset=offset;
	current_offset+=belle_sip_header_marshal(BELLE_SIP_HEADER(call_id), buff,current_offset, buff_size);
	current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%s",call_id->call_id);
	return current_offset-offset;
}
556 557 558
unsigned int belle_sip_header_call_id_equals(const belle_sip_header_call_id_t* a,const belle_sip_header_call_id_t* b) {
	return strcasecmp(a->call_id,b->call_id) == 0;
}
jehan's avatar
fix tag  
jehan committed
559
BELLE_SIP_NEW_HEADER(header_call_id,header,BELLE_SIP_CALL_ID)
jehan's avatar
jehan committed
560 561
BELLE_SIP_PARSE(header_call_id)
GET_SET_STRING(belle_sip_header_call_id,call_id);
562 563 564 565 566
/**************************
* cseq header object inherent from object
****************************
*/
struct _belle_sip_header_cseq  {
jehan's avatar
jehan committed
567
	belle_sip_header_t header;
568
	char* method;
569 570
	unsigned int seq_number;
};
jehan's avatar
jehan committed
571

572
static void belle_sip_header_cseq_destroy(belle_sip_header_cseq_t* cseq) {
573
	if (cseq->method) belle_sip_free(cseq->method);
Simon Morlat's avatar
Simon Morlat committed
574
}
575

Simon Morlat's avatar
Simon Morlat committed
576
static void belle_sip_header_cseq_clone(belle_sip_header_cseq_t* cseq, const belle_sip_header_cseq_t *orig) {
577 578
	CLONE_STRING(belle_sip_header_cseq,method,cseq,orig)
	cseq->seq_number=orig->seq_number;
579
}
jehan's avatar
jehan committed
580 581 582 583 584 585
int belle_sip_header_cseq_marshal(belle_sip_header_cseq_t* cseq, char* buff,unsigned int offset,unsigned int buff_size) {
	unsigned int current_offset=offset;
	current_offset+=belle_sip_header_marshal(BELLE_SIP_HEADER(cseq), buff,current_offset, buff_size);
	current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%i %s",cseq->seq_number,cseq->method);
	return current_offset-offset;
}
586 587
belle_sip_header_cseq_t * belle_sip_header_cseq_create(unsigned int number, const char *method){
	belle_sip_header_cseq_t *cseq=belle_sip_header_cseq_new();
jehan's avatar
jehan committed
588
	belle_sip_header_cseq_set_method(cseq,method);
589 590 591
	cseq->seq_number=number;
	return cseq;
}
592
BELLE_SIP_NEW_HEADER(header_cseq,header,BELLE_SIP_CSEQ)
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
BELLE_SIP_PARSE(header_cseq)
GET_SET_STRING(belle_sip_header_cseq,method);
GET_SET_INT(belle_sip_header_cseq,seq_number,unsigned int)
/**************************
* content type header object inherent from parameters
****************************
*/
struct _belle_sip_header_content_type  {
	belle_sip_parameters_t params_list;
	const char* type;
	const char* subtype;
};

static void belle_sip_header_content_type_destroy(belle_sip_header_content_type_t* content_type) {
	if (content_type->type) belle_sip_free((void*)content_type->type);
	if (content_type->subtype) belle_sip_free((void*)content_type->subtype);
}
jehan's avatar
jehan committed
610

Simon Morlat's avatar
Simon Morlat committed
611
static void belle_sip_header_content_type_clone(belle_sip_header_content_type_t* content_type, const belle_sip_header_content_type_t* orig){
612 613
	CLONE_STRING(belle_sip_header_content_type,type,content_type,orig);
	CLONE_STRING(belle_sip_header_content_type,subtype,content_type,orig);
Simon Morlat's avatar
Simon Morlat committed
614
}
jehan's avatar
jehan committed
615 616 617 618 619 620 621
int belle_sip_header_content_type_marshal(belle_sip_header_content_type_t* content_type, char* buff,unsigned int offset,unsigned int buff_size) {
	unsigned int current_offset=offset;
	current_offset+=belle_sip_header_marshal(BELLE_SIP_HEADER(content_type), buff,current_offset, buff_size);
	current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%s/%s",content_type->type, content_type->subtype);
	current_offset+=belle_sip_parameters_marshal(&content_type->params_list, buff,current_offset, buff_size);
	return current_offset-offset;
}
jehan's avatar
jehan committed
622
BELLE_SIP_NEW_HEADER(header_content_type,parameters,BELLE_SIP_CONTENT_TYPE)
623
BELLE_SIP_PARSE(header_content_type)
jehan's avatar
jehan committed
624 625 626 627 628 629
belle_sip_header_content_type_t* belle_sip_header_content_type_create (const char* type,const char* sub_type) {
	belle_sip_header_content_type_t* header = belle_sip_header_content_type_new();
	belle_sip_header_content_type_set_type(header,type);
	belle_sip_header_content_type_set_subtype(header,sub_type);
	return header;
}
630 631
GET_SET_STRING(belle_sip_header_content_type,type);
GET_SET_STRING(belle_sip_header_content_type,subtype);
jehan's avatar
jehan committed
632 633 634 635 636 637 638 639 640
/**************************
* Route header object inherent from header_address
****************************
*/
struct _belle_sip_header_route  {
	belle_sip_header_address_t address;
};

static void belle_sip_header_route_destroy(belle_sip_header_route_t* route) {
Simon Morlat's avatar
Simon Morlat committed
641 642 643
}

static void belle_sip_header_route_clone(belle_sip_header_route_t* route, const belle_sip_header_route_t* orig) {
jehan's avatar
jehan committed
644
}
jehan's avatar
jehan committed
645 646 647
int belle_sip_header_route_marshal(belle_sip_header_route_t* route, char* buff,unsigned int offset,unsigned int buff_size) {
	BELLE_SIP_FROM_LIKE_MARSHAL(route)
}
jehan's avatar
jehan committed
648
BELLE_SIP_NEW_HEADER(header_route,header_address,BELLE_SIP_ROUTE)
jehan's avatar
jehan committed
649
BELLE_SIP_PARSE(header_route)
jehan's avatar
jehan committed
650 651
belle_sip_header_route_t* belle_sip_header_route_create(const belle_sip_header_address_t* route) {
	belle_sip_header_route_t* header= belle_sip_header_route_new();
jehan's avatar
jehan committed
652 653
	_belle_sip_object_copy((belle_sip_object_t*)header,(belle_sip_object_t*)route);
	belle_sip_header_set_name(BELLE_SIP_HEADER(header),BELLE_SIP_ROUTE); /*restaure header name*/
jehan's avatar
jehan committed
654 655
	return header;
}
jehan's avatar
jehan committed
656 657 658 659 660 661 662 663 664
/**************************
* Record route header object inherent from header_address
****************************
*/
struct _belle_sip_header_record_route  {
	belle_sip_header_address_t address;
};

static void belle_sip_header_record_route_destroy(belle_sip_header_record_route_t* record_route) {
Simon Morlat's avatar
Simon Morlat committed
665 666 667 668
}

static void belle_sip_header_record_route_clone(belle_sip_header_record_route_t* record_route,
                                const belle_sip_header_record_route_t* orig               ) {
jehan's avatar
jehan committed
669
}
jehan's avatar
jehan committed
670 671 672
int belle_sip_header_record_route_marshal(belle_sip_header_record_route_t* record_route, char* buff,unsigned int offset,unsigned int buff_size) {
	BELLE_SIP_FROM_LIKE_MARSHAL(record_route)
}
jehan's avatar
jehan committed
673
BELLE_SIP_NEW_HEADER(header_record_route,header_address,"Record-Route")
jehan's avatar
jehan committed
674
BELLE_SIP_PARSE(header_record_route)
jehan's avatar
jehan committed
675 676 677 678 679
/**************************
* content length header object inherent from object
****************************
*/
struct _belle_sip_header_content_length  {
jehan's avatar
jehan committed
680
	belle_sip_header_t header;
jehan's avatar
jehan committed
681 682 683 684
	unsigned int content_length;
};

static void belle_sip_header_content_length_destroy(belle_sip_header_content_length_t* content_length) {
Simon Morlat's avatar
Simon Morlat committed
685
}
jehan's avatar
jehan committed
686

Simon Morlat's avatar
Simon Morlat committed
687 688
static void belle_sip_header_content_length_clone(belle_sip_header_content_length_t* content_length,
                                                 const belle_sip_header_content_length_t *orig ) {
689
	content_length->content_length=orig->content_length;
jehan's avatar
jehan committed
690
}
Simon Morlat's avatar
Simon Morlat committed
691

jehan's avatar
jehan committed
692 693 694 695 696 697
int belle_sip_header_content_length_marshal(belle_sip_header_content_length_t* content_length, char* buff,unsigned int offset,unsigned int buff_size) {
	unsigned int current_offset=offset;
	current_offset+=belle_sip_header_marshal(BELLE_SIP_HEADER(content_length), buff,current_offset, buff_size);
	current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%i",content_length->content_length);
	return current_offset-offset;
}
jehan's avatar
jehan committed
698
BELLE_SIP_NEW_HEADER(header_content_length,header,BELLE_SIP_CONTENT_LENGTH)
jehan's avatar
jehan committed
699 700
BELLE_SIP_PARSE(header_content_length)
GET_SET_INT(belle_sip_header_content_length,content_length,unsigned int)
jehan's avatar
jehan committed
701 702 703 704 705 706
belle_sip_header_content_length_t* belle_sip_header_content_length_create (int content_length)  {
	belle_sip_header_content_length_t* obj;
	obj = belle_sip_header_content_length_new();
	belle_sip_header_content_length_set_content_length(obj,content_length);
	return obj;
}
jehan's avatar
jehan committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720
/**************************
* Expires header object inherent from header
****************************
*/
struct _belle_sip_header_expires  {
	belle_sip_header_t header;
	int expires;
};

static void belle_sip_header_expires_destroy(belle_sip_header_expires_t* expires) {
}

static void belle_sip_header_expires_clone(belle_sip_header_expires_t* expires,
                                                 const belle_sip_header_expires_t *orig ) {
721
	expires->expires=orig->expires;
jehan's avatar
jehan committed
722 723 724 725 726 727 728 729
}

int belle_sip_header_expires_marshal(belle_sip_header_expires_t* expires, char* buff,unsigned int offset,unsigned int buff_size) {
	unsigned int current_offset=offset;
	current_offset+=belle_sip_header_marshal(BELLE_SIP_HEADER(expires), buff,current_offset, buff_size);
	current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%i",expires->expires);
	return current_offset-offset;
}
730
BELLE_SIP_NEW_HEADER(header_expires,header,BELLE_SIP_EXPIRES)
jehan's avatar
jehan committed
731 732
BELLE_SIP_PARSE(header_expires)
GET_SET_INT(belle_sip_header_expires,expires,int)
jehan's avatar
jehan committed
733 734 735 736 737
belle_sip_header_expires_t* belle_sip_header_expires_create(int expires) {
	belle_sip_header_expires_t* obj = belle_sip_header_expires_new();
	belle_sip_header_expires_set_expires(obj,expires);
	return obj;
}
jehan's avatar
jehan committed
738 739 740 741 742 743 744 745 746 747 748 749 750 751
/******************************
 * Extension header hinerite from header
 *
 ******************************/
struct _belle_sip_header_extension  {
	belle_sip_header_t header;
	const char* value;
};

static void belle_sip_header_extension_destroy(belle_sip_header_extension_t* extension) {
	if (extension->value) belle_sip_free((void*)extension->value);
}

static void belle_sip_header_extension_clone(belle_sip_header_extension_t* extension, const belle_sip_header_extension_t* orig){
752
	CLONE_STRING(belle_sip_header_extension,value,extension,orig)
jehan's avatar
jehan committed
753
}
jehan's avatar
jehan committed
754 755 756 757 758
int belle_sip_header_extension_marshal(belle_sip_header_extension_t* extension, char* buff,unsigned int offset,unsigned int buff_size) {
	unsigned int current_offset=offset;
	current_offset+=belle_sip_header_marshal(BELLE_SIP_HEADER(extension), buff,current_offset, buff_size);
	current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%s",extension->value);
	return current_offset-offset;
jehan's avatar
jehan committed
759

jehan's avatar
jehan committed
760
}
jehan's avatar
jehan committed
761 762
BELLE_SIP_NEW_HEADER(header_extension,header,NULL)

jehan's avatar
jehan committed
763 764 765 766 767
belle_sip_header_extension_t* belle_sip_header_extension_create (const char* name,const char* value) {
	belle_sip_header_extension_t* ext = belle_sip_header_extension_new();
	belle_sip_header_set_name(BELLE_SIP_HEADER(ext),name);
	belle_sip_header_extension_set_value(ext,value);
	return ext;
jehan's avatar
jehan committed
768

jehan's avatar
jehan committed
769
}
jehan's avatar
jehan committed
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
/**
 * special case for this header. I don't know why
 */
belle_sip_header_extension_t* belle_sip_header_extension_parse (const char* value) {
	pANTLR3_INPUT_STREAM           input;
	pbelle_sip_messageLexer               lex;
	pANTLR3_COMMON_TOKEN_STREAM    tokens;
	pbelle_sip_messageParser              parser;
	input  = antlr3NewAsciiStringCopyStream	(
			(pANTLR3_UINT8)value,
			(ANTLR3_UINT32)strlen(value),
			((void *)0));
	lex    = belle_sip_messageLexerNew                (input);
	tokens = antlr3CommonTokenStreamSourceNew  (1025, lex->pLexer->rec->state->tokSource);
	parser = belle_sip_messageParserNew               (tokens);
	belle_sip_messageParser_header_extension_return l_parsed_object = parser->header_extension(parser,FALSE);
	parser ->free(parser);
	tokens ->free(tokens);
	lex    ->free(lex);
	input  ->close(input);
	if (l_parsed_object.ret == NULL) belle_sip_error("Parser error for [%s]",value);\
	return BELLE_SIP_HEADER_EXTENSION(l_parsed_object.ret);
}
GET_SET_STRING(belle_sip_header_extension,value);
794
/**************************
795
*Authorization header object inherent from parameters
796 797
****************************
*/
jehan's avatar
jehan committed
798 799 800 801 802 803
#define AUTH_BASE \
	belle_sip_parameters_t params_list; \
	const char* scheme; \
	const char* realm; \
	const char* nonce; \
	const char* algorithm; \
jehan's avatar
jehan committed
804 805 806 807
	const char* opaque;



jehan's avatar
jehan committed
808 809 810 811 812 813 814

#define AUTH_BASE_DESTROY(obj) \
	if (obj->scheme) belle_sip_free((void*)obj->scheme);\
	if (obj->realm) belle_sip_free((void*)obj->realm);\
	if (obj->nonce) belle_sip_free((void*)obj->nonce);\
	if (obj->algorithm) belle_sip_free((void*)obj->algorithm);\
	if (obj->opaque) belle_sip_free((void*)obj->opaque);\
jehan's avatar
jehan committed
815

jehan's avatar
jehan committed
816 817
	/*if (obj->params_list) FIXME free list*/

818 819 820 821 822
#define AUTH_BASE_CLONE(object_type,dest,src) \
		CLONE_STRING(object_type,scheme,dest,src)\
		CLONE_STRING(object_type,realm,dest,src)\
		CLONE_STRING(object_type,nonce,dest,src)\
		CLONE_STRING(object_type,algorithm,dest,src)\
jehan's avatar
jehan committed
823 824
		CLONE_STRING(object_type,opaque,dest,src) \

jehan's avatar
jehan committed
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858

#define AUTH_BASE_MARSHAL(header) \
	unsigned int current_offset=offset;\
	char* border=" ";\
	current_offset+=belle_sip_header_marshal(BELLE_SIP_HEADER(header), buff,current_offset, buff_size);\
	const belle_sip_list_t* list=belle_sip_parameters_get_parameters(&header->params_list);\
	if (header->scheme) { \
		current_offset+=snprintf(buff+current_offset,buff_size-current_offset," %s",header->scheme);\
		} else { \
			belle_sip_error("missing mandatory scheme"); \
		} \
	for(;list!=NULL;list=list->next){\
		belle_sip_param_pair_t* container = list->data;\
		current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%s%s=%s",border, container->name,container->value);\
		border=", ";\
	}\
	if (header->realm) {\
		current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%srealm=\"%s\"",border,header->realm);\
		border=", ";\
		}\
	if (header->nonce) {\
		current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%snonce=\"%s\"",border,header->nonce);\
		border=", ";\
		}\
	if (header->algorithm) {\
		current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%salgorithm=%s",border,header->algorithm);\
		border=", ";\
		}\
	if (header->opaque) {\
		current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%sopaque=\"%s\"",border,header->opaque);\
		border=", ";\
		}

	struct _belle_sip_header_authorization  {
jehan's avatar
jehan committed
859
	AUTH_BASE
860
	const char* username;
jehan's avatar
jehan committed
861
	belle_sip_uri_t* uri;
jehan's avatar
jehan committed
862
	const char* response;
863 864
	const char* cnonce;
	int nonce_count;
jehan's avatar
jehan committed
865
	const char* qop;
866 867 868 869 870 871

};


static void belle_sip_header_authorization_destroy(belle_sip_header_authorization_t* authorization) {
	if (authorization->username) belle_sip_free((void*)authorization->username);
jehan's avatar
jehan committed
872
	if (authorization->uri) {
Simon Morlat's avatar
Simon Morlat committed
873
			belle_sip_object_unref(authorization->uri);
jehan's avatar
jehan committed
874
	}
875
	if (authorization->cnonce) belle_sip_free((void*)authorization->cnonce);
jehan's avatar
jehan committed
876
	AUTH_BASE_DESTROY(authorization)
jehan's avatar
jehan committed
877 878
	DESTROY_STRING(authorization,response);
	DESTROY_STRING(authorization,qop);
879 880 881 882
}

static void belle_sip_header_authorization_clone(belle_sip_header_authorization_t* authorization,
                                                 const belle_sip_header_authorization_t *orig ) {
883 884 885 886 887 888 889 890
	AUTH_BASE_CLONE(belle_sip_header_authorization,authorization,orig)
	CLONE_STRING(belle_sip_header_authorization,username,authorization,orig)
	if (belle_sip_header_authorization_get_uri(orig)) {
		belle_sip_header_authorization_set_uri(authorization,BELLE_SIP_URI(belle_sip_object_clone(BELLE_SIP_OBJECT(belle_sip_header_authorization_get_uri(orig)))));
	}
	CLONE_STRING(belle_sip_header_authorization,response,authorization,orig)
	CLONE_STRING(belle_sip_header_authorization,cnonce,authorization,orig)
	authorization->nonce_count=orig->nonce_count;
jehan's avatar
jehan committed
891
	CLONE_STRING(belle_sip_header_authorization,qop,authorization,orig)
892
}
893 894
static void belle_sip_header_authorization_init(belle_sip_header_authorization_t* authorization) {
}
895

jehan's avatar
jehan committed
896 897 898
belle_sip_uri_t* belle_sip_header_authorization_get_uri(const belle_sip_header_authorization_t* authorization) {
	return authorization->uri;
}
899

jehan's avatar
jehan committed
900
void belle_sip_header_authorization_set_uri(belle_sip_header_authorization_t* authorization, belle_sip_uri_t* uri) {
Simon Morlat's avatar
Simon Morlat committed
901
	if (uri) belle_sip_object_ref(uri);
jehan's avatar
jehan committed
902 903 904 905 906
	if (authorization->uri) {
		belle_sip_object_unref(BELLE_SIP_OBJECT(authorization->uri));
	}
	authorization->uri=uri;
}
jehan's avatar
jehan committed
907
int belle_sip_header_authorization_marshal(belle_sip_header_authorization_t* authorization, char* buff,unsigned int offset,unsigned int buff_size) {
jehan's avatar
jehan committed
908
	char nonce_count[10];
jehan's avatar
jehan committed
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
	AUTH_BASE_MARSHAL(authorization)
	if (authorization->username) {
		current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%susername=\"%s\"",border,authorization->username);\
		border=", ";
		}
	if (authorization->uri) {
		current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%s uri=\"",border);
		border=", ";
		current_offset+=belle_sip_uri_marshal(authorization->uri,buff,current_offset,buff_size);
		current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%s","\"");
		}
	if (authorization->algorithm) {
		current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%salgorithm=%s",border,authorization->algorithm);
		border=", ";
		}
	if (authorization->response) {
		current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%sresponse=\"%s\"",border,authorization->response);
		border=", ";
		}
	if (authorization->cnonce) {
		current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%scnonce=\"%s\"",border,authorization->cnonce);
		border=", ";
		}
	if (authorization->nonce_count>0) {
jehan's avatar
jehan committed
933 934
		belle_sip_header_authorization_get_nonce_count_as_string(authorization,nonce_count);
		current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%snc=%s",border,nonce_count);
jehan's avatar
jehan committed
935 936 937
		border=", ";
	}
	if (authorization->qop) {
jehan's avatar
jehan committed
938
	   current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%sqop=%s",border,authorization->qop);
jehan's avatar
jehan committed
939
	}
jehan's avatar
jehan committed
940 941
	return current_offset-offset;
}
942
BELLE_SIP_NEW_HEADER(header_authorization,parameters,BELLE_SIP_AUTHORIZATION)
943
BELLE_SIP_PARSE(header_authorization)
jehan's avatar
jehan committed
944
GET_SET_STRING(belle_sip_header_authorization,scheme);
945
GET_SET_STRING(belle_sip_header_authorization,username);
jehan's avatar
jehan committed
946 947
GET_SET_STRING(belle_sip_header_authorization,realm);
GET_SET_STRING(belle_sip_header_authorization,nonce);
jehan's avatar
jehan committed
948
GET_SET_STRING(belle_sip_header_authorization,response);
jehan's avatar
jehan committed
949
GET_SET_STRING(belle_sip_header_authorization,algorithm);
950 951
GET_SET_STRING(belle_sip_header_authorization,cnonce);
GET_SET_STRING(belle_sip_header_authorization,opaque);
jehan's avatar
jehan committed
952
GET_SET_STRING(belle_sip_header_authorization,qop);
953
GET_SET_INT(belle_sip_header_authorization,nonce_count,int)
jehan's avatar
jehan committed
954 955 956 957 958 959 960 961 962 963

int belle_sip_header_authorization_get_nonce_count_as_string(const belle_sip_header_authorization_t* authorization,char nounce_count[9]) {
	nounce_count[0]='\0';
	if (authorization->nonce_count>0) {
		snprintf(nounce_count,9,"%08x",authorization->nonce_count);
		return 0;
	} else {
		return -1;
	}
}
jehan's avatar
jehan committed
964

965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
/**************************
*Proxy-Authorization header object inherent from parameters
****************************
*/
struct _belle_sip_header_proxy_authorization  {
	belle_sip_header_authorization_t authorization;
};


static void belle_sip_header_proxy_authorization_destroy(belle_sip_header_proxy_authorization_t* proxy_authorization) {
}

static void belle_sip_header_proxy_authorization_clone(belle_sip_header_proxy_authorization_t* proxy_authorization,
                                                 const belle_sip_header_proxy_authorization_t *orig ) {
}
jehan's avatar
jehan committed
980 981 982
int belle_sip_header_proxy_authorization_marshal(belle_sip_header_proxy_authorization_t* proxy_authorization, char* buff,unsigned int offset,unsigned int buff_size) {
	return belle_sip_header_authorization_marshal(&proxy_authorization->authorization,buff,offset,buff_size);
}
983
BELLE_SIP_NEW_HEADER(header_proxy_authorization,header_authorization,BELLE_SIP_PROXY_AUTHORIZATION)
984
BELLE_SIP_PARSE(header_proxy_authorization)