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