belle_sip_internal.h 22.8 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
/*
	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/>.
*/
jehan's avatar
jehan committed
18 19
#ifndef belle_utils_h
#define belle_utils_h
20 21 22

#include <stdlib.h>
#include <string.h>
Simon Morlat's avatar
Simon Morlat committed
23

24
#include <stdio.h>
25 26
#include <stdint.h>
#include <errno.h>
Simon Morlat's avatar
Simon Morlat committed
27
#include <unistd.h>
28

29 30 31 32 33 34
#ifndef WIN32
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif

Simon Morlat's avatar
Simon Morlat committed
35
/* include all public headers*/
Simon Morlat's avatar
Simon Morlat committed
36
#include "belle-sip/belle-sip.h"
jehan's avatar
jehan committed
37

38 39 40 41
/*etc*/

#define BELLE_SIP_INTERFACE_GET_METHODS(obj,interface) \
	((BELLE_SIP_INTERFACE_METHODS_TYPE(interface)*)belle_sip_object_get_interface_methods((belle_sip_object_t*)obj,BELLE_SIP_INTERFACE_ID(interface)))
Simon Morlat's avatar
Simon Morlat committed
42

Simon Morlat's avatar
Simon Morlat committed
43
#define __BELLE_SIP_INVOKE_LISTENER_BEGIN(list,interface_name,method) \
44 45 46 47
	if (list!=NULL) {\
		const belle_sip_list_t *__elem=list;\
		do{\
			interface_name *__obj=(interface_name*)__elem->data;\
Simon Morlat's avatar
Simon Morlat committed
48 49
			void *__method=BELLE_SIP_INTERFACE_GET_METHODS(__obj,interface_name)->method;\
			if (__method) BELLE_SIP_INTERFACE_GET_METHODS(__obj,interface_name)->
50 51 52 53 54 55 56

#define __BELLE_SIP_INVOKE_LISTENER_END \
			__elem=__elem->next;\
		}while(__elem!=NULL);\
	}

#define BELLE_SIP_INVOKE_LISTENERS_VOID(list,interface_name,method) \
Simon Morlat's avatar
Simon Morlat committed
57
			__BELLE_SIP_INVOKE_LISTENER_BEGIN(list,interface_name,method)\
58 59 60 61
			method(__obj);\
			__BELLE_SIP_INVOKE_LISTENER_END

#define BELLE_SIP_INVOKE_LISTENERS_ARG(list,interface_name,method,arg) \
Simon Morlat's avatar
Simon Morlat committed
62
	__BELLE_SIP_INVOKE_LISTENER_BEGIN(list,interface_name,method)\
63 64 65 66 67
	method(__obj,arg);\
	__BELLE_SIP_INVOKE_LISTENER_END


#define BELLE_SIP_INVOKE_LISTENERS_ARG1_ARG2(list,interface_name,method,arg1,arg2) \
Simon Morlat's avatar
Simon Morlat committed
68
			__BELLE_SIP_INVOKE_LISTENER_BEGIN(list,interface_name,method)\
69 70 71 72 73 74 75 76 77 78 79 80 81 82
			method(__obj,arg1,arg2);\
			__BELLE_SIP_INVOKE_LISTENER_END

#define BELLE_SIP_INVOKE_LISTENERS_ARG1_ARG2_ARG3(list,interface_name,method,arg1,arg2,arg3) \
			__BELLE_SIP_INVOKE_LISTENER_BEGIN(list,interface_name)\
			method(__obj,arg1,arg2,arg3);\
			__BELLE_SIP_INVOKE_LISTENER_END

typedef struct weak_ref{
	struct weak_ref *next;
	belle_sip_object_destroy_notify_t notify;
	void *userpointer;
}weak_ref_t;

Simon Morlat's avatar
Simon Morlat committed
83

84
void *belle_sip_object_get_interface_methods(belle_sip_object_t *obj, belle_sip_interface_id_t ifid);
Simon Morlat's avatar
Simon Morlat committed
85

86

87
#define BELLE_SIP_OBJECT_VPTR(obj,object_type) ((BELLE_SIP_OBJECT_VPTR_TYPE(object_type)*)(((belle_sip_object_t*)obj)->vptr))
Simon Morlat's avatar
Simon Morlat committed
88 89 90 91 92
#define belle_sip_object_init(obj)		/*nothing*/


/*list of all vptrs (classes) used in belle-sip*/
BELLE_SIP_DECLARE_VPTR(belle_sip_object_t);
93 94 95 96 97 98 99 100 101 102 103 104
BELLE_SIP_DECLARE_VPTR(belle_sip_stack_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_datagram_listening_point_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_provider_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_main_loop_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_source_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_resolver_context_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_dialog_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_header_address_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_header_contact_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_header_from_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_header_to_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_header_via_t);
Simon Morlat's avatar
Simon Morlat committed
105
BELLE_SIP_DECLARE_VPTR(belle_sip_uri_t);
106 107 108
BELLE_SIP_DECLARE_VPTR(belle_sip_message_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_request_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_response_t);
Simon Morlat's avatar
Simon Morlat committed
109
BELLE_SIP_DECLARE_VPTR(belle_sip_parameters_t);
110 111 112 113 114 115 116 117 118 119 120
BELLE_SIP_DECLARE_VPTR(belle_sip_header_call_id_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_header_cseq_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_header_content_type_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_header_route_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_header_record_route_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_header_user_agent_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_header_content_length_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_header_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_header_extension_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_header_authorization_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_header_www_authenticate_t);
jehan's avatar
jehan committed
121
BELLE_SIP_DECLARE_VPTR(belle_sip_header_proxy_authenticate_t);
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
BELLE_SIP_DECLARE_VPTR(belle_sip_header_proxy_authorization_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_header_max_forwards_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_header_expires_t);
BELLE_SIP_DECLARE_VPTR(belle_sip_header_allow_t);
BELLE_SIP_DECLARE_VPTR(belle_sdp_attribute_t);
BELLE_SIP_DECLARE_VPTR(belle_sdp_bandwidth_t);
BELLE_SIP_DECLARE_VPTR(belle_sdp_connection_t);
BELLE_SIP_DECLARE_VPTR(belle_sdp_email_t);
BELLE_SIP_DECLARE_VPTR(belle_sdp_info_t);
BELLE_SIP_DECLARE_VPTR(belle_sdp_key_t);
BELLE_SIP_DECLARE_VPTR(belle_sdp_media_t);
BELLE_SIP_DECLARE_VPTR(belle_sdp_media_description_t);
BELLE_SIP_DECLARE_VPTR(belle_sdp_origin_t);
BELLE_SIP_DECLARE_VPTR(belle_sdp_phone_t);
BELLE_SIP_DECLARE_VPTR(belle_sdp_repeate_time_t);
BELLE_SIP_DECLARE_VPTR(belle_sdp_session_description_t);
BELLE_SIP_DECLARE_VPTR(belle_sdp_session_name_t);
BELLE_SIP_DECLARE_VPTR(belle_sdp_time_t);
BELLE_SIP_DECLARE_VPTR(belle_sdp_time_description_t);
BELLE_SIP_DECLARE_VPTR(belle_sdp_uri_t);
BELLE_SIP_DECLARE_VPTR(belle_sdp_version_t);
BELLE_SIP_DECLARE_VPTR(belle_sdp_base_description_t);
144
BELLE_SIP_DECLARE_VPTR(belle_sdp_mime_parameter_t);
145

jehan's avatar
jehan committed
146

jehan's avatar
jehan committed
147

Simon Morlat's avatar
Simon Morlat committed
148
typedef void (*belle_sip_source_remove_callback_t)(belle_sip_source_t *);
jehan's avatar
jehan committed
149

jehan's avatar
jehan committed
150 151 152



Simon Morlat's avatar
Simon Morlat committed
153
struct belle_sip_source{
154
	belle_sip_object_t base;
Simon Morlat's avatar
Simon Morlat committed
155 156
	belle_sip_list_t node;
	unsigned long id;
jehan's avatar
jehan committed
157
	belle_sip_fd_t fd;
Simon Morlat's avatar
Simon Morlat committed
158 159 160 161 162 163 164
	unsigned int events;
	int timeout;
	void *data;
	uint64_t expire_ms;
	int index; /* index in pollfd table */
	belle_sip_source_func_t notify;
	belle_sip_source_remove_callback_t on_remove;
Simon Morlat's avatar
Simon Morlat committed
165 166
	unsigned char cancelled;
	unsigned char expired;
Simon Morlat's avatar
Simon Morlat committed
167
};
jehan's avatar
jehan committed
168

jehan's avatar
jehan committed
169
void belle_sip_fd_source_init(belle_sip_source_t *s, belle_sip_source_func_t func, void *data, belle_sip_fd_t fd, unsigned int events, unsigned int timeout_value_ms);
jehan's avatar
jehan committed
170 171 172

#define belle_list_next(elem) ((elem)->next)

173
/* include private headers */
174
#include "channel.h"
jehan's avatar
jehan committed
175

176

jehan's avatar
jehan committed
177 178 179 180
#ifdef __cplusplus
extern "C"{
#endif

181

182 183 184 185

#define belle_sip_new(type) (type*)belle_sip_malloc(sizeof(type))
#define belle_sip_new0(type) (type*)belle_sip_malloc0(sizeof(type))
	
186
belle_sip_list_t *belle_sip_list_new(void *data);
187
belle_sip_list_t*  belle_sip_list_append_link(belle_sip_list_t* elem,belle_sip_list_t *new_elem);
188
belle_sip_list_t *belle_sip_list_find_custom(belle_sip_list_t *list, belle_sip_compare_func compare_func, const void *user_data);
Simon Morlat's avatar
Simon Morlat committed
189
belle_sip_list_t *belle_sip_list_delete_custom(belle_sip_list_t *list, belle_sip_compare_func compare_func, const void *user_data);
190
belle_sip_list_t * belle_sip_list_free(belle_sip_list_t *list);
191

jehan's avatar
jehan committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205
#define belle_sip_list_next(elem) ((elem)->next)





#undef MIN
#define MIN(a,b)	((a)>(b) ? (b) : (a))
#undef MAX
#define MAX(a,b)	((a)>(b) ? (a) : (b))


char * belle_sip_concat (const char *str, ...);

206 207
uint64_t belle_sip_time_ms(void);

208 209 210 211 212
unsigned int belle_sip_random(void);

char *belle_sip_strdup_printf(const char *fmt,...);


jehan's avatar
jehan committed
213 214
/*parameters accessors*/
#define GET_SET_STRING(object_type,attribute) \
215
	const char* object_type##_get_##attribute (const object_type##_t* obj) {\
jehan's avatar
jehan committed
216 217 218 219 220 221 222
		return obj->attribute;\
	}\
	void object_type##_set_##attribute (object_type##_t* obj,const char* value) {\
		if (obj->attribute != NULL) free((void*)obj->attribute);\
		obj->attribute=malloc(strlen(value)+1);\
		strcpy((char*)(obj->attribute),value);\
	}
jehan's avatar
jehan committed
223 224
#define GET_SET_STRING_PARAM(object_type,attribute) GET_SET_STRING_PARAM2(object_type,attribute,attribute)
#define GET_SET_STRING_PARAM2(object_type,attribute,func_name) \
225
	const char* object_type##_get_##func_name (const object_type##_t* obj) {\
jehan's avatar
jehan committed
226 227
	const char* l_value = belle_sip_parameters_get_parameter(BELLE_SIP_PARAMETERS(obj),#attribute);\
	if (l_value == NULL) { \
Simon Morlat's avatar
Simon Morlat committed
228
		/*belle_sip_warning("cannot find parameters [%s]",#attribute);*/\
jehan's avatar
jehan committed
229 230 231 232 233 234 235 236
		return NULL;\
	}\
	return l_value;\
	}\
	void object_type##_set_##func_name (object_type##_t* obj,const char* value) {\
		belle_sip_parameters_set_parameter(BELLE_SIP_PARAMETERS(obj),#attribute,value);\
	}

jehan's avatar
jehan committed
237 238
#define DESTROY_STRING(object,attribute) if (object->attribute) belle_sip_free((void*)object->attribute);

jehan's avatar
jehan committed
239 240 241
#define CLONE_STRING_GENERIC(object_type_src,object_type_dest,attribute,dest,src) \
		if ( object_type_src##_get_##attribute (src)) {\
			object_type_dest##_set_##attribute(dest,object_type_src##_get_##attribute(src));\
242
		}
jehan's avatar
jehan committed
243

jehan's avatar
jehan committed
244 245
#define CLONE_STRING(object_type,attribute,dest,src) CLONE_STRING_GENERIC(object_type,object_type,attribute,dest,src)

jehan's avatar
jehan committed
246 247 248
#define GET_SET_INT(object_type,attribute,type) GET_SET_INT_PRIVATE(object_type,attribute,type,)

#define GET_SET_INT_PRIVATE(object_type,attribute,type,set_prefix) \
249
	type  object_type##_get_##attribute (const object_type##_t* obj) {\
jehan's avatar
jehan committed
250 251 252 253 254
		return obj->attribute;\
	}\
	void set_prefix##object_type##_set_##attribute (object_type##_t* obj,type  value) {\
		obj->attribute=value;\
	}
jehan's avatar
jehan committed
255 256 257 258 259 260 261 262 263 264 265 266
#define GET_SET_INT_PARAM(object_type,attribute,type) GET_SET_INT_PARAM_PRIVATE(object_type,attribute,type,)
#define GET_SET_INT_PARAM2(object_type,attribute,type,func_name) GET_SET_INT_PARAM_PRIVATE2(object_type,attribute,type,,func_name)

#define ATO_(type,value) ATO_##type(value)
#define ATO_int(value) atoi(value)
#define ATO_float(value) strtof(value,NULL)
#define FORMAT_(type) FORMAT_##type
#define FORMAT_int    "%i"
#define FORMAT_float  "%f"

#define GET_SET_INT_PARAM_PRIVATE(object_type,attribute,type,set_prefix) GET_SET_INT_PARAM_PRIVATE2(object_type,attribute,type,set_prefix,attribute)
#define GET_SET_INT_PARAM_PRIVATE2(object_type,attribute,type,set_prefix,func_name) \
267
	type  object_type##_get_##func_name (const object_type##_t* obj) {\
jehan's avatar
jehan committed
268 269 270 271 272 273 274 275 276
		const char* l_value = belle_sip_parameters_get_parameter(BELLE_SIP_PARAMETERS(obj),#attribute);\
		if (l_value == NULL) { \
			belle_sip_error("cannot find parameters [%s]",#attribute);\
			return -1;\
		}\
		return ATO_(type,l_value);\
	}\
	void set_prefix##object_type##_set_##func_name (object_type##_t* obj,type  value) {\
		char l_str_value[16];\
277 278 279 280
		if (value == -1) { \
			belle_sip_parameters_remove_parameter(BELLE_SIP_PARAMETERS(obj),#attribute);\
			return;\
		}\
jehan's avatar
jehan committed
281 282 283
		snprintf(l_str_value,16,FORMAT_(type),value);\
		belle_sip_parameters_set_parameter(BELLE_SIP_PARAMETERS(obj),#attribute,(const char*)l_str_value);\
	}
jehan's avatar
jehan committed
284 285

#define GET_SET_BOOL(object_type,attribute,getter) \
286
	unsigned int object_type##_##getter##_##attribute (const object_type##_t* obj) {\
jehan's avatar
jehan committed
287 288 289 290 291
		return obj->attribute;\
	}\
	void object_type##_set_##attribute (object_type##_t* obj,unsigned int value) {\
		obj->attribute=value;\
	}
jehan's avatar
jehan committed
292
#define GET_SET_BOOL_PARAM2(object_type,attribute,getter,func_name) \
293
	unsigned int object_type##_##getter##_##func_name (const object_type##_t* obj) {\
jehan's avatar
jehan committed
294 295 296 297 298
		return belle_sip_parameters_is_parameter(BELLE_SIP_PARAMETERS(obj),#attribute);\
	}\
	void object_type##_set_##func_name (object_type##_t* obj,unsigned int value) {\
		belle_sip_parameters_set_parameter(BELLE_SIP_PARAMETERS(obj),#attribute,NULL);\
	}
jehan's avatar
jehan committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

#define BELLE_SIP_PARSE(object_type) \
belle_sip_##object_type##_t* belle_sip_##object_type##_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),\
			NULL);\
	lex    = belle_sip_messageLexerNew                (input);\
	tokens = antlr3CommonTokenStreamSourceNew  (ANTLR3_SIZE_HINT, TOKENSOURCE(lex));\
	parser = belle_sip_messageParserNew               (tokens);\
	belle_sip_##object_type##_t* l_parsed_object = parser->object_type(parser);\
	parser ->free(parser);\
	tokens ->free(tokens);\
	lex    ->free(lex);\
	input  ->close(input);\
jehan's avatar
jehan committed
318
	if (l_parsed_object == NULL) belle_sip_error(#object_type" parser error for [%s]",value);\
jehan's avatar
jehan committed
319 320 321
	return l_parsed_object;\
}

jehan's avatar
jehan committed
322
#define BELLE_SIP_NEW(object_type,super_type) BELLE_SIP_NEW_HEADER(object_type,super_type,NULL)
jehan's avatar
jehan committed
323 324
#define BELLE_SIP_NEW_HEADER(object_type,super_type,name) BELLE_SIP_NEW_HEADER_INIT(object_type,super_type,name,header)
#define BELLE_SIP_NEW_HEADER_INIT(object_type,super_type,name,init_type) \
325
		BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_##object_type##_t); \
jehan's avatar
jehan committed
326 327 328 329
		BELLE_SIP_INSTANCIATE_VPTR(	belle_sip_##object_type##_t\
									, belle_sip_##super_type##_t\
									, belle_sip_##object_type##_destroy\
									, belle_sip_##object_type##_clone\
Simon Morlat's avatar
Simon Morlat committed
330
									, belle_sip_##object_type##_marshal, TRUE); \
331
		belle_sip_##object_type##_t* belle_sip_##object_type##_new () { \
Simon Morlat's avatar
Simon Morlat committed
332
		belle_sip_##object_type##_t* l_object = belle_sip_object_new(belle_sip_##object_type##_t);\
333
		belle_sip_##super_type##_init((belle_sip_##super_type##_t*)l_object); \
jehan's avatar
jehan committed
334
		belle_sip_##init_type##_init((belle_sip_##init_type##_t*) l_object); \
jehan's avatar
jehan committed
335
		if (name) belle_sip_header_set_name(BELLE_SIP_HEADER(l_object),name);\
336 337
		return l_object;\
	}
jehan's avatar
jehan committed
338 339 340 341 342 343 344 345 346
typedef struct belle_sip_param_pair_t {
	int ref;
	char* name;
	char* value;
} belle_sip_param_pair_t;


belle_sip_param_pair_t* belle_sip_param_pair_new(const char* name,const char* value);

jehan's avatar
jehan committed
347
void belle_sip_param_pair_destroy(belle_sip_param_pair_t*  pair) ;
jehan's avatar
jehan committed
348 349 350 351 352 353 354 355

int belle_sip_param_pair_comp_func(const belle_sip_param_pair_t *a, const char*b) ;

belle_sip_param_pair_t* belle_sip_param_pair_ref(belle_sip_param_pair_t* obj);

void belle_sip_param_pair_unref(belle_sip_param_pair_t* obj);


jehan's avatar
jehan committed
356
void belle_sip_header_address_set_quoted_displayname(belle_sip_header_address_t* address,const char* value);
jehan's avatar
jehan committed
357

jehan's avatar
jehan committed
358 359 360
/*calss header*/
struct _belle_sip_header {
	belle_sip_object_t base;
jehan's avatar
jehan committed
361
	belle_sip_header_t* next;
jehan's avatar
jehan committed
362 363
	const char* name;
};
Simon Morlat's avatar
Simon Morlat committed
364

jehan's avatar
jehan committed
365 366 367
void belle_sip_header_set_next(belle_sip_header_t* header,belle_sip_header_t* next);
belle_sip_header_t* belle_sip_header_get_next(const belle_sip_header_t* headers);

jehan's avatar
jehan committed
368
void belle_sip_header_init(belle_sip_header_t* obj);
jehan's avatar
jehan committed
369 370
/*class parameters*/
struct _belle_sip_parameters {
jehan's avatar
jehan committed
371
	belle_sip_header_t base;
jehan's avatar
jehan committed
372 373 374 375 376 377
	belle_sip_list_t* param_list;
	belle_sip_list_t* paramnames_list;
};

void belle_sip_parameters_init(belle_sip_parameters_t *obj);

Simon Morlat's avatar
Simon Morlat committed
378
/*
379
 * Listening points
Simon Morlat's avatar
Simon Morlat committed
380 381
*/

382

383 384 385 386
BELLE_SIP_DECLARE_CUSTOM_VPTR_BEGIN(belle_sip_listening_point_t,belle_sip_object_t)
const char *transport;
belle_sip_channel_t * (*create_channel)(belle_sip_listening_point_t *,const char *dest_ip, int port);
BELLE_SIP_DECLARE_CUSTOM_VPTR_END
Simon Morlat's avatar
Simon Morlat committed
387

388 389 390

#define BELLE_SIP_LISTENING_POINT(obj) BELLE_SIP_CAST(obj,belle_sip_listening_point_t)

Simon Morlat's avatar
Simon Morlat committed
391

392 393 394 395 396 397 398

/*
 belle_sip_stack_t
*/
struct belle_sip_stack{
	belle_sip_object_t base;
	belle_sip_main_loop_t *ml;
399
	belle_sip_timer_config_t timer_config;
400 401 402 403
};

void belle_sip_stack_get_next_hop(belle_sip_stack_t *stack, belle_sip_request_t *req, belle_sip_hop_t *hop);

404
const belle_sip_timer_config_t *belle_sip_stack_get_timer_config(const belle_sip_stack_t *stack);
405 406 407 408

/*
 belle_sip_provider_t
*/
409 410 411 412 413 414

struct belle_sip_provider{
	belle_sip_object_t base;
	belle_sip_stack_t *stack;
	belle_sip_list_t *lps; /*listening points*/
	belle_sip_list_t *listeners;
Simon Morlat's avatar
Simon Morlat committed
415
	belle_sip_list_t *client_transactions;
416 417
};

418
belle_sip_provider_t *belle_sip_provider_new(belle_sip_stack_t *s, belle_sip_listening_point_t *lp);
Simon Morlat's avatar
Simon Morlat committed
419 420 421
void belle_sip_provider_add_client_transaction(belle_sip_provider_t *prov, belle_sip_client_transaction_t *t);
belle_sip_client_transaction_t *belle_sip_provider_find_matching_client_transaction(belle_sip_provider_t *prov, belle_sip_response_t *resp);
void belle_sip_provider_remove_client_transaction(belle_sip_provider_t *prov, belle_sip_client_transaction_t *t);
422
void belle_sip_provider_set_transaction_terminated(belle_sip_provider_t *p, belle_sip_transaction_t *t);
423
belle_sip_channel_t * belle_sip_provider_get_channel(belle_sip_provider_t *p, const char *name, int port, const char *transport);
424

425 426 427 428 429 430
typedef struct listener_ctx{
	belle_sip_listener_t *listener;
	void *data;
}listener_ctx_t;

#define BELLE_SIP_PROVIDER_INVOKE_LISTENERS(provider,callback,event) \
431 432
	BELLE_SIP_INVOKE_LISTENERS_ARG(((provider)->listeners),belle_sip_listener_t,callback,(event))

433 434 435 436 437

/*
 belle_sip_transaction_t
*/

438 439 440 441 442 443
struct belle_sip_transaction{
	belle_sip_object_t base;
	belle_sip_provider_t *provider; /*the provider that created this transaction */
	belle_sip_request_t *request;
	belle_sip_response_t *prov_response;
	belle_sip_response_t *final_response;
Simon Morlat's avatar
Simon Morlat committed
444
	belle_sip_channel_t *channel;
445 446 447 448 449 450
	char *branch_id;
	belle_sip_transaction_state_t state;
	uint64_t start_time;
	void *appdata;
};

Simon Morlat's avatar
Simon Morlat committed
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468

BELLE_SIP_DECLARE_CUSTOM_VPTR_BEGIN(belle_sip_transaction_t,belle_sip_object_t)
	void (*on_terminate)(belle_sip_transaction_t *obj);
BELLE_SIP_DECLARE_CUSTOM_VPTR_END

static inline const belle_sip_timer_config_t * belle_sip_transaction_get_timer_config(belle_sip_transaction_t *obj){
	return belle_sip_stack_get_timer_config(obj->provider->stack);
}

static inline void belle_sip_transaction_start_timer(belle_sip_transaction_t *obj, belle_sip_source_t *timer){
	belle_sip_main_loop_add_source(obj->provider->stack->ml,timer);
}

static inline void belle_sip_transaction_stop_timer(belle_sip_transaction_t *obj, belle_sip_source_t *timer){
	belle_sip_main_loop_remove_source(obj->provider->stack->ml,timer);
}

void belle_sip_transaction_notify_timeout(belle_sip_transaction_t *t);
Simon Morlat's avatar
Simon Morlat committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

/*
 *
 *
 *	Client transaction
 *
 *
*/

struct belle_sip_client_transaction{
	belle_sip_transaction_t base;
};

BELLE_SIP_DECLARE_CUSTOM_VPTR_BEGIN(belle_sip_client_transaction_t,belle_sip_transaction_t)
	void (*send_request)(belle_sip_client_transaction_t *);
Simon Morlat's avatar
Simon Morlat committed
484
	int (*on_response)(belle_sip_client_transaction_t *obj, belle_sip_response_t *resp);
Simon Morlat's avatar
Simon Morlat committed
485
BELLE_SIP_DECLARE_CUSTOM_VPTR_END
486

Simon Morlat's avatar
Simon Morlat committed
487
void belle_sip_client_transaction_init(belle_sip_client_transaction_t *obj, belle_sip_provider_t *prov, belle_sip_request_t *req);
488
int belle_sip_client_transaction_add_response(belle_sip_client_transaction_t *t, belle_sip_response_t *resp);
489

Simon Morlat's avatar
Simon Morlat committed
490 491 492 493 494 495 496 497 498 499 500 501 502
struct belle_sip_ict{
	belle_sip_client_transaction_t base;
};

typedef struct belle_sip_ict belle_sip_ict_t;

BELLE_SIP_DECLARE_CUSTOM_VPTR_BEGIN(belle_sip_ict_t,belle_sip_client_transaction_t)
BELLE_SIP_DECLARE_CUSTOM_VPTR_END

belle_sip_ict_t * belle_sip_ict_new(belle_sip_provider_t *prov, belle_sip_request_t *req);

struct belle_sip_nict{
	belle_sip_client_transaction_t base;
Simon Morlat's avatar
Simon Morlat committed
503 504 505
	belle_sip_source_t *timer_F;
	belle_sip_source_t *timer_E;
	belle_sip_source_t *timer_K;
Simon Morlat's avatar
Simon Morlat committed
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
};

typedef struct belle_sip_nict belle_sip_nict_t;

BELLE_SIP_DECLARE_CUSTOM_VPTR_BEGIN(belle_sip_nict_t,belle_sip_client_transaction_t)
BELLE_SIP_DECLARE_CUSTOM_VPTR_END

belle_sip_nict_t * belle_sip_nict_new(belle_sip_provider_t *prov, belle_sip_request_t *req);


/*
 *
 *
 *	Server transaction
 *
 *
*/

struct belle_sip_server_transaction{
	belle_sip_transaction_t base;
};

BELLE_SIP_DECLARE_CUSTOM_VPTR_BEGIN(belle_sip_server_transaction_t,belle_sip_transaction_t)
BELLE_SIP_DECLARE_CUSTOM_VPTR_END

struct belle_sip_ist{
	belle_sip_server_transaction_t base;
};

typedef struct belle_sip_ist belle_sip_ist_t;

BELLE_SIP_DECLARE_CUSTOM_VPTR_BEGIN(belle_sip_ist_t,belle_sip_server_transaction_t)
BELLE_SIP_DECLARE_CUSTOM_VPTR_END

belle_sip_ist_t * belle_sip_ist_new(belle_sip_provider_t *prov, belle_sip_request_t *req);

struct belle_sip_nist{
	belle_sip_server_transaction_t base;
};

typedef struct belle_sip_nist belle_sip_nist_t;

BELLE_SIP_DECLARE_CUSTOM_VPTR_BEGIN(belle_sip_nist_t,belle_sip_server_transaction_t)
BELLE_SIP_DECLARE_CUSTOM_VPTR_END

belle_sip_nist_t * belle_sip_nist_new(belle_sip_provider_t *prov, belle_sip_request_t *req);

Simon Morlat's avatar
Simon Morlat committed
553 554 555 556 557
/*
 belle_sip_response_t
*/
void belle_sip_response_get_return_hop(belle_sip_response_t *msg, belle_sip_hop_t *hop);

jehan's avatar
jehan committed
558 559 560
#define IS_TOKEN(token) \
		(INPUT->toStringTT(INPUT,LT(1),LT(strlen(#token)))->chars ?\
		strcmp(#token,(const char*)(INPUT->toStringTT(INPUT,LT(1),LT(strlen(#token)))->chars)) == 0:0)
561
char* _belle_sip_str_dup_and_unquote_string(const char* quoted_string);
562

jehan's avatar
jehan committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
/*********************************************************
 * SDP
 */
#define BELLE_SDP_PARSE(object_type) \
belle_sdp_##object_type##_t* belle_sdp_##object_type##_parse (const char* value) { \
	pANTLR3_INPUT_STREAM           input; \
	pbelle_sdpLexer               lex; \
	pANTLR3_COMMON_TOKEN_STREAM    tokens; \
	pbelle_sdpParser              parser; \
	input  = antlr3NewAsciiStringCopyStream	(\
			(pANTLR3_UINT8)value,\
			(ANTLR3_UINT32)strlen(value),\
			NULL);\
	lex    = belle_sdpLexerNew                (input);\
	tokens = antlr3CommonTokenStreamSourceNew  (ANTLR3_SIZE_HINT, TOKENSOURCE(lex));\
	parser = belle_sdpParserNew               (tokens);\
	belle_sdp_##object_type##_t* l_parsed_object = parser->object_type(parser).ret;\
	parser ->free(parser);\
	tokens ->free(tokens);\
	lex    ->free(lex);\
	input  ->close(input);\
	if (l_parsed_object == NULL) belle_sip_error(#object_type" parser error for [%s]",value);\
	return l_parsed_object;\
}
#define BELLE_SDP_NEW(object_type,super_type) \
588
		BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sdp_##object_type##_t); \
jehan's avatar
jehan committed
589 590 591 592
		BELLE_SIP_INSTANCIATE_VPTR(	belle_sdp_##object_type##_t\
									, super_type##_t\
									, belle_sdp_##object_type##_destroy\
									, belle_sdp_##object_type##_clone\
Simon Morlat's avatar
Simon Morlat committed
593
									, belle_sdp_##object_type##_marshal, TRUE); \
jehan's avatar
jehan committed
594 595 596 597 598
		belle_sdp_##object_type##_t* belle_sdp_##object_type##_new () { \
		belle_sdp_##object_type##_t* l_object = belle_sip_object_new(belle_sdp_##object_type##_t);\
		super_type##_init((super_type##_t*)l_object); \
		return l_object;\
	}
jehan's avatar
jehan committed
599
#define BELLE_SDP_NEW_WITH_CTR(object_type,super_type) \
600
		BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sdp_##object_type##_t); \
jehan's avatar
jehan committed
601 602 603 604
		BELLE_SIP_INSTANCIATE_VPTR(	belle_sdp_##object_type##_t\
									, super_type##_t\
									, belle_sdp_##object_type##_destroy\
									, belle_sdp_##object_type##_clone\
Simon Morlat's avatar
Simon Morlat committed
605
									, belle_sdp_##object_type##_marshal,TRUE); \
jehan's avatar
jehan committed
606 607 608 609 610 611
		belle_sdp_##object_type##_t* belle_sdp_##object_type##_new () { \
		belle_sdp_##object_type##_t* l_object = belle_sip_object_new(belle_sdp_##object_type##_t);\
		super_type##_init((super_type##_t*)l_object); \
		belle_sdp_##object_type##_init(l_object); \
		return l_object;\
	}
jehan's avatar
jehan committed
612 613 614



jehan's avatar
jehan committed
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
struct belle_sip_dialog_terminated_event{
	belle_sip_provider_t *source;
	belle_sip_dialog_t *dialog;
};

struct belle_sip_io_error_event{
	belle_sip_provider_t *source;
	const char *transport;
	const char *host;
	int port;
};

struct belle_sip_request_event{
	belle_sip_provider_t *source;
	belle_sip_server_transaction_t *server_transaction;
	belle_sip_dialog_t *dialog;
	belle_sip_request_t *request;
};

struct belle_sip_response_event{
	belle_sip_provider_t *source;
	belle_sip_client_transaction_t *client_transaction;
	belle_sip_dialog_t *dialog;
	belle_sip_response_t *response;
};

struct belle_sip_timeout_event{
	belle_sip_provider_t *source;
	belle_sip_transaction_t *transaction;
	int is_server_transaction;
};

struct belle_sip_transaction_terminated_event{
	belle_sip_provider_t *source;
	belle_sip_transaction_t *transaction;
	int is_server_transaction;
};

jehan's avatar
jehan committed
653 654


jehan's avatar
jehan committed
655 656 657 658
#ifdef __cplusplus
}
#endif

jehan's avatar
jehan committed
659

Simon Morlat's avatar
Simon Morlat committed
660 661
/*include private headers */
#include "belle_sip_resolver.h"
662 663

#define BELLE_SIP_SOCKET_TIMEOUT 30000
Simon Morlat's avatar
Simon Morlat committed
664

jehan's avatar
jehan committed
665
#endif