140cb5e5vi/*
240cb5e5vi * CDDL HEADER START
340cb5e5vi *
440cb5e5vi * The contents of this file are subject to the terms of the
540cb5e5vi * Common Development and Distribution License (the "License").
640cb5e5vi * You may not use this file except in compliance with the License.
740cb5e5vi *
840cb5e5vi * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
940cb5e5vi * or http://www.opensolaris.org/os/licensing.
1040cb5e5vi * See the License for the specific language governing permissions
1140cb5e5vi * and limitations under the License.
1240cb5e5vi *
1340cb5e5vi * When distributing Covered Code, include this CDDL HEADER in each
1440cb5e5vi * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1540cb5e5vi * If applicable, add the following below this CDDL HEADER, with the
1640cb5e5vi * fields enclosed by brackets "[]" replaced with your own identifying
1740cb5e5vi * information: Portions Copyright [yyyy] [name of copyright owner]
1840cb5e5vi *
1940cb5e5vi * CDDL HEADER END
2040cb5e5vi */
2140cb5e5vi
2240cb5e5vi/*
23943efbcgm * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
2440cb5e5vi * Use is subject to license terms.
2540cb5e5vi */
2640cb5e5vi
2740cb5e5vi#ifndef	_SIP_MISCDEFS_H
2840cb5e5vi#define	_SIP_MISCDEFS_H
2940cb5e5vi
3040cb5e5vi#pragma ident	"%Z%%M%	%I%	%E% SMI"
3140cb5e5vi
3240cb5e5vi#ifdef	__cplusplus
3340cb5e5viextern "C" {
3440cb5e5vi#endif
3540cb5e5vi
362c2c418vi#include <pthread.h>
3740cb5e5vi#include <sys/types.h>
382c2c418vi#include <sys/time.h>
39943efbcgm#include <stdio.h>
4040cb5e5vi
4140cb5e5vi#define	SIP_CR			'\r'
4240cb5e5vi#define	SIP_SP			' '
4340cb5e5vi#define	SIP_HCOLON		':'
4440cb5e5vi#define	SIP_SEMI		';'
4540cb5e5vi#define	SIP_COMMA		','
4640cb5e5vi#define	SIP_LAQUOT		'<'
4740cb5e5vi#define	SIP_RAQUOT		'>'
4840cb5e5vi#define	SIP_QUOTE		'"'
4940cb5e5vi#define	SIP_EQUAL		'='
5040cb5e5vi#define	SIP_SLASH		'/'
5140cb5e5vi#define	SIP_PERIOD		'.'
5240cb5e5vi#define	SIP_LPAR		'('
5340cb5e5vi#define	SIP_RPAR		')'
5440cb5e5vi
5540cb5e5vi#define	SIP_BRANCHID_LEN	28	/* incl. the magic cookie */
5640cb5e5vi#define	SIP_TAG_LEN		20
5740cb5e5vi#define	SIP_URI_LEN		25
5840cb5e5vi#define	SIP_DISPLAY_LEN		25
5940cb5e5vi#define	SIP_DOMAIN_LEN		25
6040cb5e5vi#define	SIP_MAX_FWDLEN		5
6140cb5e5vi#define	SIP_TRANSPORT_LEN	5
6240cb5e5vi#define	SIP_SIZE_OF_STATUS_CODE	3
6340cb5e5vi#define	SIP_SPACE_LEN		sizeof (char)
6440cb5e5vi
65943efbcgm#define	SIP_TRANSACTION_LOG		0x0001
66943efbcgm#define	SIP_DIALOG_LOG			0x0002
67943efbcgm#define	SIP_ASSERT_ERROR		0x0004
68943efbcgm
6940cb5e5vi#define	SIP_MS			1L
7040cb5e5vi#define	SIP_SECONDS		(1000 * SIP_MS)
7140cb5e5vi#define	SIP_MINUTES		(60 * SIP_SECONDS)
7240cb5e5vi#define	SIP_HOURS   		(60 * SIP_MINUTES)
7340cb5e5vi
7440cb5e5vi/* timer granularity is in msecs */
7540cb5e5vi#define	SIP_TIMER_T1		(1 * SIP_SECONDS)
7640cb5e5vi#define	SIP_TIMER_T2		(4 * SIP_SECONDS)
7740cb5e5vi#define	SIP_TIMER_T4		(5 * SIP_SECONDS)
7840cb5e5vi
7940cb5e5vi#ifdef		__linux__
8040cb5e5vi#define		SEC		1
8140cb5e5vi#define		MILLISEC	1000
8240cb5e5vi#define		MICROSEC	1000000
8340cb5e5vi#define		NANOSEC		1000000000
8440cb5e5vi
8540cb5e5vitypedef struct timespec	timestruc_t;
8640cb5e5vitypedef	long long	hrtime_t;
8740cb5e5vi#endif
8840cb5e5vi
8940cb5e5viextern int	sip_timer_T1;
9040cb5e5viextern int	sip_timer_T2;
9140cb5e5viextern int	sip_timer_T4;
9240cb5e5viextern int	sip_timer_TD;
9340cb5e5vi
9440cb5e5vi/* Structure for SIP timers */
9540cb5e5vitypedef struct sip_timer_s {
9640cb5e5vi	uint_t		sip_timerid;
9740cb5e5vi	struct timeval	sip_timeout_val;
9840cb5e5vi}sip_timer_t;
9940cb5e5vi
10040cb5e5vi/* time is in msec */
10140cb5e5vi#define	SIP_SET_TIMEOUT(timer, time) {					\
10240cb5e5vi	int	mtime = (time);						\
10340cb5e5vi									\
10440cb5e5vi	(timer).sip_timeout_val.tv_sec = mtime / MILLISEC;	\
10540cb5e5vi	mtime -= (timer).sip_timeout_val.tv_sec * MILLISEC;	\
10640cb5e5vi	(timer).sip_timeout_val.tv_usec = mtime * MILLISEC;		\
10740cb5e5vi}
10840cb5e5vi
10940cb5e5vi/* time is in msec */
11040cb5e5vi#define	SIP_INIT_TIMER(timer, time) {				\
11140cb5e5vi	SIP_SET_TIMEOUT(timer, time);				\
11240cb5e5vi	(timer).sip_timerid = 0;				\
11340cb5e5vi}
11440cb5e5vi
11540cb5e5vi#define	SIP_SCHED_TIMER(timer, obj, func) {			\
11640cb5e5vi	(timer).sip_timerid = sip_stack_timeout((void *)(obj),	\
11740cb5e5vi	    (func), &((timer).sip_timeout_val));			\
11840cb5e5vi}
11940cb5e5vi
12040cb5e5vi#define	SIP_CANCEL_TIMER(timer) {				\
12140cb5e5vi	if ((timer).sip_timerid != 0) {				\
12240cb5e5vi		sip_stack_untimeout((timer).sip_timerid);	\
12340cb5e5vi		(timer).sip_timerid = 0;			\
12440cb5e5vi	}							\
12540cb5e5vi}
12640cb5e5vi
12740cb5e5vi/* returned time is in msec */
12840cb5e5vi#define	SIP_GET_TIMEOUT(timer)					\
12940cb5e5vi	((timer).sip_timeout_val.tv_sec * MILLISEC +		\
13040cb5e5vi	(timer).sip_timeout_val.tv_usec / MILLISEC)
13140cb5e5vi
13240cb5e5vi#define	SIP_IS_TIMER_RUNNING(timer)	((timer).sip_timerid != 0)
13340cb5e5vi
134943efbcgm#define	SIP_UPDATE_COUNTERS(is_request, method, resp_code, outbound, size) {   \
135943efbcgm	(void) pthread_mutex_lock(&sip_counters.sip_counter_mutex);	       \
136943efbcgm	if (sip_counters.enabled) {					       \
137943efbcgm		(void) sip_measure_traffic((is_request), (method), (resp_code),\
138943efbcgm		    (outbound), (size));				       \
139943efbcgm	}								       \
140943efbcgm	(void) pthread_mutex_unlock(&sip_counters.sip_counter_mutex);	       \
141943efbcgm}
142943efbcgm
14340cb5e5vi/* This is the transaction list */
14440cb5e5vitypedef struct sip_conn_cache_s {
14540cb5e5vi	void			*obj;
14640cb5e5vi	struct sip_conn_cache_s	*next;
14740cb5e5vi	struct sip_conn_cache_s	*prev;
14840cb5e5vi} sip_conn_cache_t;
14940cb5e5vi
15040cb5e5vi/* TCP fragment entry */
15140cb5e5vitypedef struct sip_reass_entry_s {
15240cb5e5vi	char		*sip_reass_msg;
15340cb5e5vi	int		sip_reass_msglen;
15440cb5e5vi}sip_reass_entry_t;
15540cb5e5vi
15640cb5e5vi/* Library data in stored in connection object */
15740cb5e5vitypedef struct sip_conn_obj_pvt_s {
15840cb5e5vi	sip_reass_entry_t	*sip_conn_obj_reass;
15940cb5e5vi	pthread_mutex_t		sip_conn_obj_reass_lock;
16040cb5e5vi	sip_conn_cache_t	*sip_conn_obj_cache;
16140cb5e5vi	pthread_mutex_t		sip_conn_obj_cache_lock;
16240cb5e5vi} sip_conn_obj_pvt_t;
16340cb5e5vi
164943efbcgm/* SIP traffic counters structure */
165943efbcgm
166943efbcgmtypedef struct sip_traffic_counters_s {
167943efbcgm	boolean_t	enabled;
168943efbcgm	time_t		starttime;
169943efbcgm	time_t		stoptime;
170943efbcgm	uint64_t	sip_total_bytes_rcvd;
171943efbcgm	uint64_t	sip_total_bytes_sent;
172943efbcgm	uint64_t	sip_total_req_rcvd;
173943efbcgm	uint64_t	sip_total_req_sent;
174943efbcgm	uint64_t	sip_total_resp_rcvd;
175943efbcgm	uint64_t	sip_total_resp_sent;
176943efbcgm	uint64_t	sip_ack_req_rcvd;
177943efbcgm	uint64_t	sip_ack_req_sent;
178943efbcgm	uint64_t	sip_bye_req_rcvd;
179943efbcgm	uint64_t	sip_bye_req_sent;
180943efbcgm	uint64_t	sip_cancel_req_rcvd;
181943efbcgm	uint64_t	sip_cancel_req_sent;
182943efbcgm	uint64_t	sip_info_req_rcvd;
183943efbcgm	uint64_t	sip_info_req_sent;
184943efbcgm	uint64_t	sip_invite_req_rcvd;
185943efbcgm	uint64_t	sip_invite_req_sent;
186943efbcgm	uint64_t	sip_notify_req_rcvd;
187943efbcgm	uint64_t	sip_notify_req_sent;
188943efbcgm	uint64_t	sip_options_req_rcvd;
189943efbcgm	uint64_t	sip_options_req_sent;
190943efbcgm	uint64_t	sip_prack_req_rcvd;
191943efbcgm	uint64_t	sip_prack_req_sent;
192943efbcgm	uint64_t	sip_refer_req_rcvd;
193943efbcgm	uint64_t	sip_refer_req_sent;
194943efbcgm	uint64_t	sip_register_req_rcvd;
195943efbcgm	uint64_t	sip_register_req_sent;
196943efbcgm	uint64_t	sip_subscribe_req_rcvd;
197943efbcgm	uint64_t	sip_subscribe_req_sent;
198943efbcgm	uint64_t	sip_update_req_rcvd;
199943efbcgm	uint64_t	sip_update_req_sent;
200943efbcgm	uint64_t	sip_1xx_resp_rcvd;
201943efbcgm	uint64_t	sip_1xx_resp_sent;
202943efbcgm	uint64_t	sip_2xx_resp_rcvd;
203943efbcgm	uint64_t	sip_2xx_resp_sent;
204943efbcgm	uint64_t	sip_3xx_resp_rcvd;
205943efbcgm	uint64_t	sip_3xx_resp_sent;
206943efbcgm	uint64_t	sip_4xx_resp_rcvd;
207943efbcgm	uint64_t	sip_4xx_resp_sent;
208943efbcgm	uint64_t	sip_5xx_resp_rcvd;
209943efbcgm	uint64_t	sip_5xx_resp_sent;
210943efbcgm	uint64_t	sip_6xx_resp_rcvd;
211943efbcgm	uint64_t	sip_6xx_resp_sent;
212943efbcgm	pthread_mutex_t	sip_counter_mutex; /* Mutex should be always at end */
213943efbcgm} sip_traffic_counters_t;
214943efbcgm
215943efbcgm/* SIP logfile structure */
216943efbcgmtypedef struct sip_logfile_s {
217943efbcgm	boolean_t	sip_logging_enabled;
218943efbcgm	FILE		*sip_logfile;
219943efbcgm	pthread_mutex_t	sip_logfile_mutex;
220943efbcgm} sip_logfile_t;
221943efbcgm
222943efbcgmtypedef struct sip_msg_chain_s {
223943efbcgm	char			*sip_msg;
224943efbcgm	int			msg_seq;
225943efbcgm	time_t			msg_timestamp;
226943efbcgm	struct sip_msg_chain_s *next;
227943efbcgm}sip_msg_chain_t;
228943efbcgm
229943efbcgmtypedef struct sip_log_s {
230943efbcgm	sip_msg_chain_t	*sip_msgs;
231943efbcgm	int		sip_msgcnt;
232943efbcgm}sip_log_t;
233943efbcgm
234943efbcgmextern sip_traffic_counters_t sip_counters;
235943efbcgm
236943efbcgmextern sip_logfile_t trans_log;
237943efbcgmextern sip_logfile_t dialog_log;
238943efbcgm
23940cb5e5viextern boolean_t sip_manage_dialog;
24040cb5e5vi
24140cb5e5vi/* To salt the hash function */
24240cb5e5viextern uint64_t	sip_hash_salt;
24340cb5e5vi
24440cb5e5viextern void		sip_timeout_init();
24540cb5e5viextern uint_t		sip_timeout(void *, void (*)(void *), struct timeval *);
24640cb5e5viextern boolean_t	sip_untimeout(uint_t);
24740cb5e5viextern void		sip_md5_hash(char *, int, char *, int, char *, int,
24840cb5e5vi			    char *, int, char *, int, char *, int, uchar_t *);
249943efbcgmextern void		sip_measure_traffic(boolean_t, sip_method_t, int,
250943efbcgm			    boolean_t, int);
251943efbcgmextern void		sip_add_log(sip_log_t *, sip_msg_t, int, int);
252943efbcgmextern void		sip_write_to_log(void *, int, char *, int);
25340cb5e5vi
25440cb5e5vi#ifdef	__cplusplus
25540cb5e5vi}
25640cb5e5vi#endif
25740cb5e5vi
25840cb5e5vi#endif	/* _SIP_MISCDEFS_H */
259