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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_SIP_MISCDEFS_H
28 #define	_SIP_MISCDEFS_H
29 
30 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 #include <pthread.h>
35 #include <sys/types.h>
36 #include <sys/time.h>
37 #include <stdio.h>
38 
39 #define	SIP_CR			'\r'
40 #define	SIP_SP			' '
41 #define	SIP_HCOLON		':'
42 #define	SIP_SEMI		';'
43 #define	SIP_COMMA		','
44 #define	SIP_LAQUOT		'<'
45 #define	SIP_RAQUOT		'>'
46 #define	SIP_QUOTE		'"'
47 #define	SIP_EQUAL		'='
48 #define	SIP_SLASH		'/'
49 #define	SIP_PERIOD		'.'
50 #define	SIP_LPAR		'('
51 #define	SIP_RPAR		')'
52 
53 #define	SIP_BRANCHID_LEN	28	/* incl. the magic cookie */
54 #define	SIP_TAG_LEN		20
55 #define	SIP_URI_LEN		25
56 #define	SIP_DISPLAY_LEN		25
57 #define	SIP_DOMAIN_LEN		25
58 #define	SIP_MAX_FWDLEN		5
59 #define	SIP_TRANSPORT_LEN	5
60 #define	SIP_SIZE_OF_STATUS_CODE	3
61 #define	SIP_SPACE_LEN		sizeof (char)
62 
63 #define	SIP_TRANSACTION_LOG		0x0001
64 #define	SIP_DIALOG_LOG			0x0002
65 #define	SIP_ASSERT_ERROR		0x0004
66 
67 #define	SIP_MS			1L
68 #define	SIP_SECONDS		(1000 * SIP_MS)
69 #define	SIP_MINUTES		(60 * SIP_SECONDS)
70 #define	SIP_HOURS   		(60 * SIP_MINUTES)
71 
72 /* timer granularity is in msecs */
73 #define	SIP_TIMER_T1		(1 * SIP_SECONDS)
74 #define	SIP_TIMER_T2		(4 * SIP_SECONDS)
75 #define	SIP_TIMER_T4		(5 * SIP_SECONDS)
76 
77 #ifdef		__linux__
78 #define		SEC		1
79 #define		MILLISEC	1000
80 #define		MICROSEC	1000000
81 #define		NANOSEC		1000000000
82 
83 typedef struct timespec	timestruc_t;
84 typedef	long long	hrtime_t;
85 #endif
86 
87 extern int	sip_timer_T1;
88 extern int	sip_timer_T2;
89 extern int	sip_timer_T4;
90 extern int	sip_timer_TD;
91 
92 /* Structure for SIP timers */
93 typedef struct sip_timer_s {
94 	uint_t		sip_timerid;
95 	struct timeval	sip_timeout_val;
96 }sip_timer_t;
97 
98 /* time is in msec */
99 #define	SIP_SET_TIMEOUT(timer, time) {					\
100 	int	mtime = (time);						\
101 									\
102 	(timer).sip_timeout_val.tv_sec = mtime / MILLISEC;	\
103 	mtime -= (timer).sip_timeout_val.tv_sec * MILLISEC;	\
104 	(timer).sip_timeout_val.tv_usec = mtime * MILLISEC;		\
105 }
106 
107 /* time is in msec */
108 #define	SIP_INIT_TIMER(timer, time) {				\
109 	SIP_SET_TIMEOUT(timer, time);				\
110 	(timer).sip_timerid = 0;				\
111 }
112 
113 #define	SIP_SCHED_TIMER(timer, obj, func) {			\
114 	(timer).sip_timerid = sip_stack_timeout((void *)(obj),	\
115 	    (func), &((timer).sip_timeout_val));			\
116 }
117 
118 #define	SIP_CANCEL_TIMER(timer) {				\
119 	if ((timer).sip_timerid != 0) {				\
120 		sip_stack_untimeout((timer).sip_timerid);	\
121 		(timer).sip_timerid = 0;			\
122 	}							\
123 }
124 
125 /* returned time is in msec */
126 #define	SIP_GET_TIMEOUT(timer)					\
127 	((timer).sip_timeout_val.tv_sec * MILLISEC +		\
128 	(timer).sip_timeout_val.tv_usec / MILLISEC)
129 
130 #define	SIP_IS_TIMER_RUNNING(timer)	((timer).sip_timerid != 0)
131 
132 #define	SIP_UPDATE_COUNTERS(is_request, method, resp_code, outbound, size) {   \
133 	(void) pthread_mutex_lock(&sip_counters.sip_counter_mutex);	       \
134 	if (sip_counters.enabled) {					       \
135 		(void) sip_measure_traffic((is_request), (method), (resp_code),\
136 		    (outbound), (size));				       \
137 	}								       \
138 	(void) pthread_mutex_unlock(&sip_counters.sip_counter_mutex);	       \
139 }
140 
141 /* This is the transaction list */
142 typedef struct sip_conn_cache_s {
143 	void			*obj;
144 	struct sip_conn_cache_s	*next;
145 	struct sip_conn_cache_s	*prev;
146 } sip_conn_cache_t;
147 
148 /* TCP fragment entry */
149 typedef struct sip_reass_entry_s {
150 	char		*sip_reass_msg;
151 	int		sip_reass_msglen;
152 }sip_reass_entry_t;
153 
154 /* Library data in stored in connection object */
155 typedef struct sip_conn_obj_pvt_s {
156 	sip_reass_entry_t	*sip_conn_obj_reass;
157 	pthread_mutex_t		sip_conn_obj_reass_lock;
158 	sip_conn_cache_t	*sip_conn_obj_cache;
159 	pthread_mutex_t		sip_conn_obj_cache_lock;
160 } sip_conn_obj_pvt_t;
161 
162 /* SIP traffic counters structure */
163 
164 typedef struct sip_traffic_counters_s {
165 	boolean_t	enabled;
166 	time_t		starttime;
167 	time_t		stoptime;
168 	uint64_t	sip_total_bytes_rcvd;
169 	uint64_t	sip_total_bytes_sent;
170 	uint64_t	sip_total_req_rcvd;
171 	uint64_t	sip_total_req_sent;
172 	uint64_t	sip_total_resp_rcvd;
173 	uint64_t	sip_total_resp_sent;
174 	uint64_t	sip_ack_req_rcvd;
175 	uint64_t	sip_ack_req_sent;
176 	uint64_t	sip_bye_req_rcvd;
177 	uint64_t	sip_bye_req_sent;
178 	uint64_t	sip_cancel_req_rcvd;
179 	uint64_t	sip_cancel_req_sent;
180 	uint64_t	sip_info_req_rcvd;
181 	uint64_t	sip_info_req_sent;
182 	uint64_t	sip_invite_req_rcvd;
183 	uint64_t	sip_invite_req_sent;
184 	uint64_t	sip_notify_req_rcvd;
185 	uint64_t	sip_notify_req_sent;
186 	uint64_t	sip_options_req_rcvd;
187 	uint64_t	sip_options_req_sent;
188 	uint64_t	sip_prack_req_rcvd;
189 	uint64_t	sip_prack_req_sent;
190 	uint64_t	sip_refer_req_rcvd;
191 	uint64_t	sip_refer_req_sent;
192 	uint64_t	sip_register_req_rcvd;
193 	uint64_t	sip_register_req_sent;
194 	uint64_t	sip_subscribe_req_rcvd;
195 	uint64_t	sip_subscribe_req_sent;
196 	uint64_t	sip_update_req_rcvd;
197 	uint64_t	sip_update_req_sent;
198 	uint64_t	sip_1xx_resp_rcvd;
199 	uint64_t	sip_1xx_resp_sent;
200 	uint64_t	sip_2xx_resp_rcvd;
201 	uint64_t	sip_2xx_resp_sent;
202 	uint64_t	sip_3xx_resp_rcvd;
203 	uint64_t	sip_3xx_resp_sent;
204 	uint64_t	sip_4xx_resp_rcvd;
205 	uint64_t	sip_4xx_resp_sent;
206 	uint64_t	sip_5xx_resp_rcvd;
207 	uint64_t	sip_5xx_resp_sent;
208 	uint64_t	sip_6xx_resp_rcvd;
209 	uint64_t	sip_6xx_resp_sent;
210 	pthread_mutex_t	sip_counter_mutex; /* Mutex should be always at end */
211 } sip_traffic_counters_t;
212 
213 /* SIP logfile structure */
214 typedef struct sip_logfile_s {
215 	boolean_t	sip_logging_enabled;
216 	FILE		*sip_logfile;
217 	pthread_mutex_t	sip_logfile_mutex;
218 } sip_logfile_t;
219 
220 typedef struct sip_msg_chain_s {
221 	char			*sip_msg;
222 	int			msg_seq;
223 	time_t			msg_timestamp;
224 	struct sip_msg_chain_s *next;
225 }sip_msg_chain_t;
226 
227 typedef struct sip_log_s {
228 	sip_msg_chain_t	*sip_msgs;
229 	int		sip_msgcnt;
230 }sip_log_t;
231 
232 extern sip_traffic_counters_t sip_counters;
233 
234 extern sip_logfile_t trans_log;
235 extern sip_logfile_t dialog_log;
236 
237 extern boolean_t sip_manage_dialog;
238 
239 /* To salt the hash function */
240 extern uint64_t	sip_hash_salt;
241 
242 extern void		sip_timeout_init();
243 extern uint_t		sip_timeout(void *, void (*)(void *), struct timeval *);
244 extern boolean_t	sip_untimeout(uint_t);
245 extern void		sip_md5_hash(char *, int, char *, int, char *, int,
246 			    char *, int, char *, int, char *, int, uchar_t *);
247 extern void		sip_measure_traffic(boolean_t, sip_method_t, int,
248 			    boolean_t, int);
249 extern void		sip_add_log(sip_log_t *, sip_msg_t, int, int);
250 extern void		sip_write_to_log(void *, int, char *, int);
251 
252 #ifdef	__cplusplus
253 }
254 #endif
255 
256 #endif	/* _SIP_MISCDEFS_H */
257