xref: /titanic_51/usr/src/lib/libsip/common/sip_msg.h (revision 14ea4bb737263733ad80a36b4f73f681c30a6b45)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_SIP_MSG_H
28 #define	_SIP_MSG_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef	__cplusplus
33 extern "C" {
34 #endif
35 
36 #include <sip.h>
37 
38 #ifdef	__solaris__
39 extern int mutex_held();
40 #endif
41 
42 extern sip_header_function_t *sip_header_function_table_external;
43 
44 /* Compare Cseq numbers */
45 #define	SIP_CSEQ_LT(a, b)	((int32_t)((a)-(b)) < 0)
46 #define	SIP_CSEQ_GT(a, b)	((int32_t)((a)-(b)) > 0)
47 #define	SIP_CSEQ_GEQ(a, b)	((int32_t)((a)-(b)) >= 0)
48 
49 #define	SIP_HEADER_ACTIVE	0x0
50 #define	SIP_HEADER_DELETED	0x1
51 #define	SIP_HEADER_DELETED_VAL	0x2
52 
53 /* List of registered sent-by values */
54 typedef struct sent_by_list_s {
55 	struct sent_by_list_s	*sb_next;
56 	struct sent_by_list_s	*sb_prev;
57 	char			*sb_val;
58 } sent_by_list_t;
59 
60 extern sent_by_list_t	*sip_sent_by;
61 extern int		sip_sent_by_count;
62 extern pthread_mutex_t	sip_sent_by_lock;
63 
64 typedef struct sip_header {
65 	sip_hdr_general_t	sip_hdr_general;
66 	/* active/deleted or has deleted val */
67 	int			sip_header_state;
68 	struct sip_header	*sip_hdr_next;
69 	struct sip_header	*sip_hdr_prev;
70 	struct sip_message	*sip_hdr_sipmsg;
71 	/* True if header was allocated */
72 	boolean_t		sip_hdr_allocated;
73 	sip_header_function_t	*sip_header_functions;
74 }_sip_header_t;
75 
76 /* Structure for the SIP message body */
77 typedef struct sip_content {
78 	char			*sip_content_start;
79 	char			*sip_content_end;
80 	char			*sip_content_current;
81 	struct sip_content	*sip_content_next;
82 	boolean_t		sip_content_allocated;
83 }sip_content_t;
84 
85 
86 /* General definitions */
87 
88 /* Two string values */
89 typedef struct sip_2strs {
90 	sip_str_t	s1;
91 	sip_str_t	s2;
92 }sip_2strs_t;
93 
94 /* An integer and a string value */
95 typedef struct sip_intstr {
96 	int		i;
97 	sip_str_t	s;
98 } sip_intstr_t;
99 
100 /* Warn value */
101 typedef struct sip_warn {
102 	int		code;
103 	sip_str_t	agt;
104 	sip_str_t	text;
105 } sip_warn_t;
106 
107 /* Date value */
108 typedef struct sip_date {
109 	sip_str_t	t;
110 	int		d;
111 	sip_str_t	m;
112 	int		y;
113 	sip_str_t	tz;
114 	sip_str_t	wd;
115 } sip_date_t;
116 
117 /* Authorization and authentication value */
118 typedef struct sip_auth {
119 	sip_str_t	scheme;
120 	sip_param_t	*param;
121 } sip_auth_t;
122 
123 /* RACK value */
124 typedef struct sip_rack {
125 	int		rack_resp_num;
126 	int		rack_cseq_num;
127 	sip_method_t	rack_method;
128 }sip_rack_t;
129 
130 /* Cseq value */
131 typedef struct sip_cseq {
132 	int		num;
133 	sip_method_t	method;
134 } sip_cseq_value_t;
135 
136 /* Value for Contact, From and To header */
137 typedef struct cftr_value {
138 	sip_str_t	*display_name;
139 	sip_str_t	uri;
140 } sip_cftr_value_t;
141 
142 /* SIP name/version/transport value in Via */
143 typedef struct sip_proto_version_s {
144 	sip_str_t	name;
145 	sip_str_t	version;
146 	sip_str_t	transport;
147 }sip_proto_version_t;
148 
149 /* Via value */
150 typedef struct via_value {
151 	sip_proto_version_t 	sent_protocol;
152 	sip_str_t		sent_by_host;
153 	int			sent_by_port;
154 }sip_via_value_t;
155 
156 typedef struct sip_hdr_value {
157 	sip_value_t	sip_value;
158 	union {
159 		int			i;
160 		sip_str_t		str;
161 		sip_2strs_t		strs;
162 		sip_intstr_t		intstr;
163 		sip_warn_t		warn;
164 		sip_date_t		date;
165 		sip_auth_t		auth;
166 		sip_rack_t		rack;
167 		sip_cseq_value_t	cseq;
168 		sip_cftr_value_t	cftr;
169 		sip_via_value_t		via;
170 	} hdr_value;
171 } sip_hdr_value_t;
172 
173 /*
174  * NOTE: ALL value structs MUST have sip_value_t as the first field.
175  */
176 #define	sip_value_version	sip_value.sip_value_version
177 #define	sip_next_value		sip_value.next
178 #define	sip_param_list		sip_value.param_list
179 #define	sip_value_state 	sip_value.value_state
180 #define	sip_value_header 	sip_value.parsed_header
181 #define	sip_value_start		sip_value.value_start
182 #define	sip_value_end		sip_value.value_end
183 #define	sip_value_parsed_uri 	sip_value.sip_value_parse_uri
184 
185 #define	auth_val		hdr_value.auth
186 #define	auth_scheme_ptr		hdr_value.auth.scheme.sip_str_ptr
187 #define	auth_scheme_len		hdr_value.auth.scheme.sip_str_len
188 #define	auth_param		hdr_value.auth.param
189 #define	int_val			hdr_value.i
190 #define	str_val			hdr_value.str
191 #define	str_val_ptr		hdr_value.str.sip_str_ptr
192 #define	str_val_len		hdr_value.str.sip_str_len
193 #define	strs_val		hdr_value.strs
194 #define	strs_s1			hdr_value.strs.s1
195 #define	strs_s2			hdr_value.strs.s2
196 #define	strs1_val_ptr		hdr_value.strs.s1.sip_str_ptr
197 #define	strs1_val_len		hdr_value.strs.s1.sip_str_len
198 #define	strs2_val_ptr		hdr_value.strs.s2.sip_str_ptr
199 #define	strs2_val_len		hdr_value.strs.s2.sip_str_len
200 #define	intstr_val		hdr_value.intstr
201 #define	intstr_int		hdr_value.intstr.i
202 #define	intstr_str		hdr_value.intstr.s
203 #define	intstr_str_ptr		hdr_value.intstr.s.sip_str_ptr
204 #define	intstr_str_len		hdr_value.intstr.s.sip_str_len
205 #define	warn_code		hdr_value.warn.code
206 #define	warn_agt		hdr_value.warn.agt
207 #define	warn_text		hdr_value.warn.text
208 #define	warn_agt_ptr		warn_agt.sip_str_ptr
209 #define	warn_agt_len		warn_agt.sip_str_len
210 #define	warn_text_ptr		warn_text.sip_str_ptr
211 #define	warn_text_len		warn_text.sip_str_len
212 #define	date_t			hdr_value.date.t
213 #define	date_d			hdr_value.date.d
214 #define	date_m			hdr_value.date.m
215 #define	date_y			hdr_value.date.y
216 #define	date_tz			hdr_value.date.tz
217 #define	date_wd			hdr_value.date.wd
218 #define	date_t_ptr		date_t.sip_str_ptr
219 #define	date_t_len		date_t.sip_str_len
220 #define	date_m_ptr		date_m.sip_str_ptr
221 #define	date_m_len		date_m.sip_str_len
222 #define	date_tz_ptr		date_tz.sip_str_ptr
223 #define	date_tz_len		date_tz.sip_str_len
224 #define	date_wd_ptr		date_wd.sip_str_ptr
225 #define	date_wd_len		date_wd.sip_str_len
226 #define	rack_resp		hdr_value.rack.rack_resp_num
227 #define	rack_cseq		hdr_value.rack.rack_cseq_num
228 #define	rack_method		hdr_value.rack.rack_method
229 #define	cftr_name		hdr_value.cftr.display_name
230 #define	cftr_uri		hdr_value.cftr.uri
231 #define	cseq_num		hdr_value.cseq.num
232 #define	cseq_method		hdr_value.cseq.method
233 #define	via_protocol		hdr_value.via.sent_protocol
234 #define	via_protocol_name	hdr_value.via.sent_protocol.name
235 #define	via_protocol_vers	hdr_value.via.sent_protocol.version
236 #define	via_protocol_transport	hdr_value.via.sent_protocol.transport
237 #define	via_sent_by_host	hdr_value.via.sent_by_host
238 #define	via_sent_by_port	hdr_value.via.sent_by_port
239 
240 #define	SIP_INT_VAL		0x01
241 #define	SIP_STR_VAL		0x02
242 #define	SIP_STRS_VAL		0x03
243 #define	SIP_INTSTR_VAL		0x04
244 #define	SIP_AUTH_VAL		0x05
245 
246 /* hdr value contains two string */
247 typedef sip_hdr_value_t sip_acpt_value_t;
248 typedef sip_hdr_value_t sip_content_type_value_t;
249 
250 /* hdr value contains one string only */
251 typedef sip_hdr_value_t sip_acpt_lang_value_t;
252 typedef sip_hdr_value_t sip_acpt_encode_value_t;
253 typedef sip_hdr_value_t sip_alert_value_t;
254 typedef sip_hdr_value_t sip_cl_info_value_t;
255 typedef sip_hdr_value_t sip_ct_disp_value_t;
256 typedef sip_hdr_value_t sip_ct_encode_value_t;
257 typedef sip_hdr_value_t sip_ct_lang_value_t;
258 typedef sip_hdr_value_t sip_irt_value_t;
259 typedef sip_hdr_value_t sip_mime_ver_value_t;
260 typedef sip_hdr_value_t sip_org_value_t;
261 typedef sip_hdr_value_t sip_prio_value_t;
262 typedef sip_hdr_value_t sip_reply_value_t;
263 typedef sip_hdr_value_t sip_privacy_value_t;
264 typedef sip_hdr_value_t sip_ppassertedid_value_t;
265 typedef sip_hdr_value_t sip_ppreferredid_value_t;
266 typedef sip_hdr_value_t sip_pxy_req_value_t;
267 typedef sip_hdr_value_t sip_req_value_t;
268 typedef sip_hdr_value_t sip_subject_value_t;
269 typedef sip_hdr_value_t sip_svr_value_t;
270 typedef sip_hdr_value_t sip_support_value_t;
271 typedef sip_hdr_value_t sip_unsupport_value_t;
272 typedef sip_hdr_value_t sip_usr_agt_value_t;
273 typedef sip_hdr_value_t sip_err_info_value_t;
274 typedef sip_hdr_value_t sip_date_value_t;
275 typedef sip_hdr_value_t sip_allert_value_t;
276 typedef sip_hdr_value_t	sip_callid_value_t;
277 
278 /* hdr value contain one int only */
279 typedef sip_hdr_value_t sip_expr_value_t;
280 typedef sip_hdr_value_t sip_min_expr_value_t;
281 typedef sip_hdr_value_t sip_retry_value_t;
282 typedef sip_hdr_value_t sip_timestamp_value_t;
283 typedef sip_hdr_value_t sip_rseq_value_t;
284 typedef sip_hdr_value_t sip_content_len_value_t;
285 typedef sip_hdr_value_t sip_max_forwards_value_t;
286 typedef sip_hdr_value_t sip_allow_value_t;
287 
288 /* hdr value contain one int, two strings */
289 typedef sip_hdr_value_t sip_warn_value_t;
290 
291 /* hdr field value is a list of param=param_val */
292 typedef sip_hdr_value_t	sip_authen_value_t;
293 typedef sip_hdr_value_t	sip_authen_info_value_t;
294 typedef sip_hdr_value_t	sip_pxy_authen_value_t;
295 typedef sip_hdr_value_t	sip_pxy_author_value_t;
296 typedef sip_hdr_value_t	sip_3w_authen_value_t;
297 
298 /* SIP request line structure */
299 typedef struct sip_request {
300 	sip_method_t	sip_request_method;
301 	sip_str_t	sip_request_uri;
302 	sip_uri_t	sip_parse_uri;
303 } sip_request_t;
304 
305 /* SIP response line structure */
306 typedef struct sip_response {
307 	int		sip_response_code;
308 	sip_str_t	sip_response_phrase;
309 } sip_response_t;
310 
311 /* SIP message type - request or response */
312 typedef struct sip_message_type {
313 	boolean_t		is_request;
314 	sip_proto_version_t	sip_proto_version;
315 	union {
316 	sip_request_t		sip_request;
317 	sip_response_t		sip_response;
318 	} U;
319 	/* This is to save old value when we use a recvd message. */
320 	struct sip_message_type	*sip_next;
321 } sip_message_type_t;
322 
323 /* Increment reference count on SIP message */
324 #define	SIP_MSG_REFCNT_INCR(sip_msg) {				\
325 	(void) pthread_mutex_lock(&(sip_msg)->sip_msg_mutex);	\
326 	(sip_msg)->sip_msg_ref_cnt++;				\
327 	(void) pthread_mutex_unlock(&(sip_msg)->sip_msg_mutex);	\
328 }
329 
330 /* Decrement reference count on SIP message */
331 #define	SIP_MSG_REFCNT_DECR(sip_msg) {					\
332 	(void) pthread_mutex_lock(&(sip_msg)->sip_msg_mutex);		\
333 	assert((sip_msg)->sip_msg_ref_cnt > 0);				\
334 	if (--(sip_msg)->sip_msg_ref_cnt == 0) {			\
335 		sip_destroy_msg(sip_msg);				\
336 	} else {							\
337 		(void) pthread_mutex_unlock(&(sip_msg)->sip_msg_mutex);	\
338 	}								\
339 }
340 
341 /* SIP message structure */
342 typedef struct sip_message {
343 	char			*sip_msg_buf;	/* Message */
344 	char			*sip_msg_old_buf;
345 	boolean_t		sip_msg_modified;
346 	boolean_t		sip_msg_cannot_be_modified;
347 	int			sip_msg_len;
348 	size_t			sip_msg_content_len;	/* content length */
349 	sip_content_t		*sip_msg_content;
350 	/* All fields synchronizes on this */
351 	pthread_mutex_t		sip_msg_mutex;
352 	/* doubly linked list of headers */
353 	_sip_header_t		*sip_msg_headers_start;
354 	_sip_header_t		*sip_msg_headers_end;
355 	_sip_header_t		*sip_msg_start_line;
356 	sip_message_type_t	*sip_msg_req_res;
357 	int			sip_msg_ref_cnt;
358 }_sip_msg_t;
359 
360 extern char		*sip_get_tcp_msg(sip_conn_object_t, char *, size_t *);
361 extern char		*sip_msg_to_msgbuf(_sip_msg_t *msg, int *error);
362 extern char		*_sip_startline_to_str(_sip_msg_t *sip_msg, int *error);
363 extern int		sip_adjust_msgbuf(_sip_msg_t *msg);
364 extern void		sip_delete_all_headers(_sip_msg_t *sip_msg);
365 extern _sip_header_t	*sip_dup_header(_sip_header_t *from);
366 extern int		_sip_copy_header(_sip_msg_t *, _sip_header_t *, char *,
367 			    boolean_t);
368 extern int		_sip_find_and_copy_header(_sip_msg_t *, _sip_msg_t *,
369 			    char *, char *, boolean_t);
370 extern int		_sip_find_and_copy_all_header(_sip_msg_t *,
371 			    _sip_msg_t *, char *header_name);
372 extern _sip_header_t	*sip_search_for_header(_sip_msg_t *, char *,
373 			    _sip_header_t *);
374 extern void		_sip_add_header(_sip_msg_t *, _sip_header_t *,
375 			    boolean_t, boolean_t, char *);
376 extern _sip_header_t	*sip_new_header(int);
377 extern int		sip_create_nonOKack(sip_msg_t, sip_msg_t, sip_msg_t);
378 extern void		sip_destroy_msg(_sip_msg_t *);
379 extern void		sip_free_header(_sip_header_t *sip_header);
380 extern void		sip_free_phdr(sip_parsed_header_t *);
381 extern void		sip_free_cftr_header(sip_parsed_header_t *);
382 
383 extern int		sip_parse_allow_events_header(_sip_header_t *,
384 			    sip_parsed_header_t **);
385 extern int		sip_parse_event_header(_sip_header_t *,
386 			    sip_parsed_header_t **);
387 extern int		sip_parse_substate_header(_sip_header_t *,
388 			    sip_parsed_header_t **);
389 extern int		sip_parse_acpt_header(_sip_header_t *,
390 			    sip_parsed_header_t **);
391 extern int		sip_parse_acpt_encode_header(_sip_header_t *,
392 			    sip_parsed_header_t **);
393 extern int		sip_parse_acpt_lang_header(_sip_header_t *,
394 			    sip_parsed_header_t **);
395 extern int		sip_parse_alert_header(_sip_header_t *,
396 			    sip_parsed_header_t **);
397 extern int		sip_parse_allow_header(_sip_header_t *,
398 			    sip_parsed_header_t **);
399 extern int		sip_parse_useragt_header(_sip_header_t *,
400 			    sip_parsed_header_t **);
401 extern int		sip_parse_usupport_header(_sip_header_t *,
402 			    sip_parsed_header_t **);
403 extern int		sip_parse_timestamp_header(_sip_header_t *,
404 			    sip_parsed_header_t **);
405 extern int		sip_parse_support_header(_sip_header_t *,
406 			    sip_parsed_header_t **);
407 extern int		sip_parse_subject_header(_sip_header_t *,
408 			    sip_parsed_header_t **);
409 extern int		sip_parse_server_header(_sip_header_t *,
410 			    sip_parsed_header_t **);
411 extern int		sip_parse_retryaft_header(_sip_header_t *,
412 			    sip_parsed_header_t **);
413 extern int		sip_parse_require_header(_sip_header_t *,
414 			    sip_parsed_header_t **);
415 extern int		sip_parse_replyto_header(_sip_header_t *,
416 			    sip_parsed_header_t **);
417 extern int		sip_parse_passertedid_header(_sip_header_t *,
418 			    sip_parsed_header_t **);
419 extern int		sip_parse_ppreferredid_header(_sip_header_t *,
420 			    sip_parsed_header_t **);
421 extern int		sip_parse_priority_header(_sip_header_t *,
422 			    sip_parsed_header_t **);
423 extern int		sip_parse_org_header(_sip_header_t *,
424 			    sip_parsed_header_t **);
425 extern int		sip_parse_mimeversion_header(_sip_header_t *,
426 			    sip_parsed_header_t **);
427 extern int		sip_parse_minexpire_header(_sip_header_t *,
428 			    sip_parsed_header_t **);
429 extern int		sip_parse_rseq_header(_sip_header_t *,
430 			    sip_parsed_header_t **);
431 extern int		sip_parse_inreplyto_header(_sip_header_t *,
432 			    sip_parsed_header_t **);
433 extern int		sip_parse_privacy_header(_sip_header_t *,
434 			    sip_parsed_header_t **);
435 extern int		sip_parse_expire_header(_sip_header_t *,
436 			    sip_parsed_header_t **);
437 extern int		sip_parse_errorinfo_header(_sip_header_t *,
438 			    sip_parsed_header_t **);
439 extern int		sip_parse_contentlang_header(_sip_header_t *,
440 			    sip_parsed_header_t **);
441 extern int		sip_parse_contentencode_header(_sip_header_t *,
442 			    sip_parsed_header_t **);
443 extern int		sip_parse_contentdis_header(_sip_header_t *,
444 			    sip_parsed_header_t **);
445 extern int		sip_parse_callinfo_header(_sip_header_t *,
446 			    sip_parsed_header_t **);
447 extern int		sip_parse_date_header(_sip_header_t *,
448 			    sip_parsed_header_t **);
449 extern int		sip_parse_warn_header(_sip_header_t *,
450 			    sip_parsed_header_t **);
451 extern int		sip_parse_cftr_header(_sip_header_t *,
452 			    sip_parsed_header_t **);
453 extern int		sip_parse_cseq_header(_sip_header_t *,
454 			    sip_parsed_header_t **);
455 extern int		sip_parse_cid_header(_sip_header_t *,
456 			    sip_parsed_header_t **);
457 extern int		sip_parse_via_header(_sip_header_t *,
458 			    sip_parsed_header_t **);
459 extern int		sip_parse_clen_header(_sip_header_t *,
460 			    sip_parsed_header_t **);
461 extern int		sip_parse_maxf_header(_sip_header_t *,
462 			    sip_parsed_header_t **);
463 extern int		sip_parse_ctype_header(_sip_header_t *,
464 			    sip_parsed_header_t **);
465 extern int		sip_parse_unknown_header(_sip_header_t *,
466 			    sip_parsed_header_t **);
467 extern int		sip_parse_ainfo_header(_sip_header_t *,
468 			    sip_parsed_header_t **);
469 extern int		sip_parse_preq_header(_sip_header_t *,
470 			    sip_parsed_header_t **);
471 extern int		sip_parse_author_header(_sip_header_t *,
472 			    sip_parsed_header_t **);
473 extern int		sip_parse_pauthor_header(_sip_header_t *,
474 			    sip_parsed_header_t **);
475 extern int		sip_parse_pauthen_header(_sip_header_t *,
476 			    sip_parsed_header_t **);
477 extern int		sip_parse_wauthen_header(_sip_header_t *,
478 			    sip_parsed_header_t **);
479 extern int		sip_parse_rseq(_sip_header_t *, sip_parsed_header_t **);
480 extern int		sip_parse_rack(_sip_header_t *, sip_parsed_header_t **);
481 extern int		sip_parse_passertedid(_sip_header_t *,
482 			    sip_parsed_header_t **);
483 extern int		sip_parse_ppreferredid(_sip_header_t *,
484 			    sip_parsed_header_t **);
485 extern int		sip_parse_privacy_header(_sip_header_t *,
486 			    sip_parsed_header_t **);
487 
488 extern sip_param_t	*sip_get_param_from_list(sip_param_t *, char *);
489 extern int		sip_copy_values(char *, _sip_header_t *);
490 extern int		sip_add_content_length(_sip_msg_t *, int);
491 extern int		sip_delete_start_line_locked(_sip_msg_t *);
492 
493 /* Useful access macros */
494 #define	sip_resp_phrase_len	U.sip_response.sip_response_phrase.sip_str_len
495 #define	sip_resp_phrase_ptr	U.sip_response.sip_response_phrase.sip_str_ptr
496 
497 #define	sip_resp_code		U.sip_response.sip_response_code
498 #define	sip_resp_phrase		U.sip_response.sip_response_phrase
499 
500 #define	sip_req_method		U.sip_request.sip_request_method
501 #define	sip_req_uri		U.sip_request.sip_request_uri
502 #define	sip_req_uri_ptr		U.sip_request.sip_request_uri.sip_str_ptr
503 #define	sip_req_uri_len		U.sip_request.sip_request_uri.sip_str_ptr
504 #define	sip_req_parse_uri	U.sip_request.sip_parse_uri
505 
506 #define	sip_header_parse	sip_header_functions->header_parse_func
507 #define	sip_header_name		sip_header_functions->header_name
508 
509 #define	sip_hdr_start		sip_hdr_general.sip_hdr_start
510 #define	sip_hdr_end		sip_hdr_general.sip_hdr_end
511 #define	sip_hdr_current		sip_hdr_general.sip_hdr_current
512 #define	sip_hdr_parsed		sip_hdr_general.sip_hdr_parsed
513 
514 #ifdef	__cplusplus
515 }
516 #endif
517 
518 #endif	/* _SIP_MSG_H */
519