belle_sip_headers_impl.c 10.2 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 29
#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
30 31 32
/************************
 * header
 ***********************/
jehan's avatar
jehan committed
33

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

jehan's avatar
jehan committed
36 37 38 39
void belle_sip_header_init(belle_sip_header_t *header) {
	belle_sip_object_init_type(header,belle_sip_header_t);
	belle_sip_object_init((belle_sip_object_t*)header); /*super*/
}
40

41
 void belle_sip_header_destroy(belle_sip_header_t *header){
jehan's avatar
jehan committed
42 43
	if (header->name) belle_sip_free((void*)header->name);
}
jehan's avatar
jehan committed
44 45 46 47 48

/************************
 * header_address
 ***********************/
struct _belle_sip_header_address {
jehan's avatar
jehan committed
49
	belle_sip_parameters_t params;
jehan's avatar
jehan committed
50 51 52
	const char* displayname;
	belle_sip_uri_t* uri;
};
jehan's avatar
jehan committed
53 54 55
static void belle_sip_header_address_init(belle_sip_header_address_t* object){
	belle_sip_object_init_type(object,belle_sip_header_address_t);
	belle_sip_parameters_init((belle_sip_parameters_t*)object); /*super*/
jehan's avatar
jehan committed
56 57
}

jehan's avatar
jehan committed
58 59 60
static void belle_sip_header_address_destroy(belle_sip_header_address_t* contact) {
	if (contact->displayname) belle_sip_free((void*)(contact->displayname));
	if (contact->uri) belle_sip_object_unref(BELLE_SIP_OBJECT(contact->uri));
61
	belle_sip_header_destroy((belle_sip_header_t*)contact);
jehan's avatar
jehan committed
62 63
}

jehan's avatar
jehan committed
64
BELLE_SIP_NEW(header_address,object)
jehan's avatar
jehan committed
65 66
GET_SET_STRING(belle_sip_header_address,displayname);

jehan's avatar
jehan committed
67 68 69 70 71 72
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));
		size_t value_size = strlen(value);
		address->displayname=belle_sip_malloc0(value_size-2+1);
		strncpy((char*)(address->displayname),value+1,value_size-2);
}
jehan's avatar
jehan committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
belle_sip_uri_t* belle_sip_header_address_get_uri(belle_sip_header_address_t* address) {
	return address->uri;
}

void belle_sip_header_address_set_uri(belle_sip_header_address_t* address, belle_sip_uri_t* uri) {
	address->uri=uri;
}



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

jehan's avatar
jehan committed
91 92
void belle_sip_header_contact_destroy(belle_sip_header_contact_t* contact) {
	belle_sip_header_address_destroy(BELLE_SIP_HEADER_ADDRESS(contact));
jehan's avatar
jehan committed
93 94
}

95
BELLE_SIP_NEW_WITH_NAME(header_contact,header_address,"Contact")
jehan's avatar
jehan committed
96
BELLE_SIP_PARSE(header_contact)
jehan's avatar
jehan committed
97

jehan's avatar
jehan committed
98 99
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
100 101
GET_SET_BOOL(belle_sip_header_contact,wildcard,is);

jehan's avatar
jehan committed
102

jehan's avatar
jehan committed
103 104
int belle_sip_header_contact_set_expires(belle_sip_header_contact_t* contact, int expires) {
	if (expires < 0 ) {
105
		 belle_sip_error("bad expires value [%i] for contact",expires);
jehan's avatar
jehan committed
106 107 108 109 110 111
		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
112
	 if (qValue != -1 && qValue < 0 && qValue >1) {
113
		 belle_sip_error("bad q value [%f] for contact",qValue);
jehan's avatar
jehan committed
114 115
		 return -1;
	 }
jehan's avatar
jehan committed
116
	 _belle_sip_header_contact_set_q(contact,qValue);
jehan's avatar
jehan committed
117 118
	 return 0;
}
119
float	belle_sip_header_contact_get_qvalue(const belle_sip_header_contact_t* contact) {
jehan's avatar
jehan committed
120 121 122
	return belle_sip_header_contact_get_q(contact);
}
/**************************
jehan's avatar
jehan committed
123
* From header object inherent from header_address
jehan's avatar
jehan committed
124
****************************
jehan's avatar
jehan committed
125 126 127 128 129
*/
struct _belle_sip_header_from  {
	belle_sip_header_address_t address;
};

jehan's avatar
jehan committed
130 131 132 133
static void belle_sip_header_from_destroy(belle_sip_header_from_t* from) {
	belle_sip_header_address_destroy(BELLE_SIP_HEADER_ADDRESS(from));
}

134
BELLE_SIP_NEW_WITH_NAME(header_from,header_address,"From")
jehan's avatar
jehan committed
135
BELLE_SIP_PARSE(header_from)
jehan's avatar
jehan committed
136 137 138 139 140 141 142 143 144
GET_SET_STRING_PARAM(belle_sip_header_from,tag);

/**************************
* To header object inherent from header_address
****************************
*/
struct _belle_sip_header_to  {
	belle_sip_header_address_t address;
};
jehan's avatar
jehan committed
145

jehan's avatar
jehan committed
146 147
static void belle_sip_header_to_destroy(belle_sip_header_to_t* to) {
	belle_sip_header_address_destroy(BELLE_SIP_HEADER_ADDRESS(to));
jehan's avatar
jehan committed
148
}
jehan's avatar
jehan committed
149

150
BELLE_SIP_NEW_WITH_NAME(header_to,header_address,"To")
jehan's avatar
jehan committed
151 152
BELLE_SIP_PARSE(header_to)
GET_SET_STRING_PARAM(belle_sip_header_to,tag);
153

jehan's avatar
jehan committed
154
/**************************
155
* Viq header object inherent from parameters
jehan's avatar
jehan committed
156 157 158
****************************
*/
struct _belle_sip_header_via  {
159 160 161 162 163
	belle_sip_parameters_t params_list;
	const char* protocol;
	const char* transport;
	const char* host;
	int port;
jehan's avatar
jehan committed
164 165
};

166 167 168 169
static void belle_sip_header_via_destroy(belle_sip_header_via_t* via) {
	belle_sip_parameters_destroy(BELLE_SIP_PARAMETERS(via));
	if (via->host) belle_sip_free((void*)via->host);
	if (via->protocol) belle_sip_free((void*)via->protocol);
jehan's avatar
jehan committed
170 171
}

172
BELLE_SIP_NEW_WITH_NAME(header_via,header_address,"Via")
jehan's avatar
jehan committed
173
BELLE_SIP_PARSE(header_via)
174 175 176 177 178 179 180 181 182 183 184
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
185

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
int belle_sip_header_via_set_rport (belle_sip_header_via_t* obj,int  value) {
	if (value ==-1 || (value>0 && value<65536)) {
		_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;
	}
}
204

205 206 207 208 209 210 211 212 213
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
214

215
int belle_sip_header_via_get_listening_port(const belle_sip_header_via_t *via){
Simon Morlat's avatar
Simon Morlat committed
216 217 218 219 220
	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;
}

221
/**************************
jehan's avatar
jehan committed
222
* call_id header object inherent from object
223 224
****************************
*/
jehan's avatar
jehan committed
225
struct _belle_sip_header_call_id  {
226
	belle_sip_object_t base;
jehan's avatar
jehan committed
227
	const char* call_id;
228
};
jehan's avatar
jehan committed
229

jehan's avatar
jehan committed
230 231 232
static void belle_sip_header_call_id_destroy(belle_sip_header_call_id_t* call_id) {
	belle_sip_object_destroy(BELLE_SIP_OBJECT(call_id));
	if (call_id->call_id) belle_sip_free((void*)call_id->call_id);
jehan's avatar
jehan committed
233

234 235
}

236
BELLE_SIP_NEW_WITH_NAME(header_call_id,object,"Call-ID")
jehan's avatar
jehan committed
237 238
BELLE_SIP_PARSE(header_call_id)
GET_SET_STRING(belle_sip_header_call_id,call_id);
239 240 241 242 243 244 245 246 247
/**************************
* cseq header object inherent from object
****************************
*/
struct _belle_sip_header_cseq  {
	belle_sip_object_t base;
	const char* method;
	unsigned int seq_number;
};
jehan's avatar
jehan committed
248

249 250 251 252 253 254
static void belle_sip_header_cseq_destroy(belle_sip_header_cseq_t* cseq) {
	belle_sip_object_destroy(BELLE_SIP_OBJECT(cseq));
	if (cseq->method) belle_sip_free((void*)cseq->method);

}

255
BELLE_SIP_NEW_WITH_NAME(header_cseq,object,"Cseq")
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
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) {
	belle_sip_parameters_destroy(BELLE_SIP_PARAMETERS(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
274

275
BELLE_SIP_NEW_WITH_NAME(header_content_type,parameters,"Content-Type")
276 277 278
BELLE_SIP_PARSE(header_content_type)
GET_SET_STRING(belle_sip_header_content_type,type);
GET_SET_STRING(belle_sip_header_content_type,subtype);
jehan's avatar
jehan committed
279 280 281 282 283 284 285 286 287 288 289 290
/**************************
* 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) {
	belle_sip_header_address_destroy(BELLE_SIP_HEADER_ADDRESS(route));
}

291
BELLE_SIP_NEW_WITH_NAME(header_route,header_address,"Route")
jehan's avatar
jehan committed
292 293 294 295 296 297 298 299 300 301 302 303 304
BELLE_SIP_PARSE(header_route)
/**************************
* 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) {
	belle_sip_header_address_destroy(BELLE_SIP_HEADER_ADDRESS(record_route));
}

305
BELLE_SIP_NEW_WITH_NAME(header_record_route,header_address,"Record-Route")
jehan's avatar
jehan committed
306
BELLE_SIP_PARSE(header_record_route)
jehan's avatar
jehan committed
307 308 309 310 311 312 313 314 315 316 317
/**************************
* content length header object inherent from object
****************************
*/
struct _belle_sip_header_content_length  {
	belle_sip_object_t base;
	unsigned int content_length;
};

static void belle_sip_header_content_length_destroy(belle_sip_header_content_length_t* content_length) {
	belle_sip_object_destroy(BELLE_SIP_OBJECT(content_length));
jehan's avatar
jehan committed
318

jehan's avatar
jehan committed
319
}
320
BELLE_SIP_NEW_WITH_NAME(header_content_length,object,"Content-Length")
jehan's avatar
jehan committed
321 322
BELLE_SIP_PARSE(header_content_length)
GET_SET_INT(belle_sip_header_content_length,content_length,unsigned int)