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