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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _SLP_INTERNAL_H
28 #define	_SLP_INTERNAL_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include <thread.h>
37 #include <synch.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <limits.h>
42 #include <sys/types.h>
43 #include <sys/uio.h>
44 #include <slp.h>
45 
46 /* SLPv2 function numbers */
47 #define	SRVRQST		1
48 #define	SRVRPLY		2
49 #define	SRVREG		3
50 #define	SRVDEREG	4
51 #define	SRVACK		5
52 #define	ATTRRQST	6
53 #define	ATTRRPLY	7
54 #define	DAADVERT	8
55 #define	SRVTYPERQST	9
56 #define	SRVTYPERPLY	10
57 #define	SAADVERT	11
58 
59 /* SLPv2 protocol error functions, hidden under the API */
60 typedef enum {
61 	SLP_MSG_PARSE_ERROR		= 256,	/* used internally */
62 	SLP_VER_NOT_SUPPORTED		= 9,
63 	SLP_SICK_DA			= 10,
64 	SLP_DA_BUSY_NOW			= 11,
65 	SLP_OPTION_NOT_UNDERSTOOD	= 12,
66 	SLP_RQST_NOT_SUPPORTED		= 13
67 } slp_proto_err;
68 
69 /* Defaults and properties */
70 #define	SLP_VERSION	2
71 #define	SLP_DEFAULT_SENDMTU		1400
72 #define	SLP_PORT	427
73 #define	SLP_DEFAULT_MAXWAIT	15000
74 #define	SLP_DEFAULT_MAXRESULTS	-1
75 #define	SLP_MULTICAST_ADDRESS	inet_addr("239.255.255.253")
76 #define	SLP_MAX_STRINGLEN	USHRT_MAX
77 #define	SLP_MAX_MSGLEN		16777216	/* max message length 2^24 */
78 #define	SLP_SUN_SCOPES_TAG	"424242SUN-TABLE-SCOPES424242"
79 #define	SLP_SUN_VERSION_TAG	"424242SUN-TABLE-VERSION424242"
80 
81 /* Property names */
82 #define	SLP_CONFIG_USESCOPES		"net.slp.useScopes"
83 #define	SLP_CONFIG_ISBROADCASTONLY	"net.slp.isBroadcastOnly"
84 #define	SLP_CONFIG_MULTICASTTTL		"net.slp.multicastTTL"
85 #define	SLP_CONFIG_MULTICASTMAXWAIT	"net.slp.multicastMaximumWait"
86 #define	SLP_CONFIG_DATAGRAMTIMEOUTS	"net.slp.datagramTimeouts"
87 #define	SLP_CONFIG_MULTICASTTIMEOUTS	"net.slp.multicastTimeouts"
88 #define	SLP_CONFIG_MTU			"net.slp.mtu"
89 #define	SLP_CONFIG_INTERFACES		"net.slp.interfaces"
90 #define	SLP_CONFIG_LOCALE		"net.slp.locale"
91 #define	SLP_CONFIG_MAXRESULTS		"net.slp.maxResults"
92 #define	SLP_CONFIG_USEGETXXXBYYYY	"sun.net.slp.usegetxxxbyyyy"
93 #define	SLP_CONFIG_TYPEHINT		"net.slp.typeHint"
94 #define	SLP_CONFIG_SECURITY_ON		"net.slp.securityEnabled"
95 #define	SLP_CONFIG_SPI			"sun.net.slp.SPIs"
96 #define	SLP_CONFIG_SIGN_AS		"sun.net.slp.signAs"
97 #define	SLP_CONFIG_BYPASS_AUTH		"sun.net.slp.bypassAuth"
98 #define	SLP_CONFIG_AUTH_BACKEND		"sun.net.slp.authBackend"
99 #define	SLP_SUN_DA_TYPE			"service:directory-agent.sun"
100 
101 #define	SLP_DEFAULT_CONFIG_FILE		"/etc/inet/slp.conf"
102 
103 extern void slp_readConfig(void);
104 
105 /* Synchronized queue structures and functions */
106 
107 typedef void slp_queue_t;
108 
109 extern slp_queue_t *slp_new_queue(SLPError *);
110 extern SLPError slp_enqueue(slp_queue_t *, void *);
111 extern SLPError slp_enqueue_at_head(slp_queue_t *, void *);
112 extern void *slp_dequeue_timed(slp_queue_t *, timestruc_t *, SLPBoolean *);
113 extern void *slp_dequeue(slp_queue_t *);
114 extern void slp_flush_queue(slp_queue_t *, void (*)(void *));
115 extern void slp_destroy_queue(slp_queue_t *);
116 
117 typedef struct {
118 	struct iovec	*iov;
119 	int		iovlen;
120 	char	*msg;
121 	struct iovec	prlistlen;
122 	struct iovec	*prlist;
123 	struct iovec	scopeslen;
124 	struct iovec	*scopes;
125 } slp_msg_t;
126 
127 /* Implementation of SLPHandle */
128 typedef struct handle_impl {
129 	const char	*locale;	/* language tag */
130 	int		fid;		/* SLP function ID */
131 	slp_msg_t	msg;		/* The SLP message */
132 	mutex_t		*tcp_lock;	/* TCP thread wait lock */
133 	int		tcp_ref_cnt;	/* TCP thread reference count */
134 	cond_t		*tcp_wait;	/* TCP thread wait condition var */
135 	SLPBoolean	async;		/* asynchronous flag */
136 	slp_queue_t	*q;		/* message queue for this handle */
137 	thread_t	producer_tid;	/* thr ID of message producer */
138 	thread_t	consumer_tid;	/* thr ID of message consumer */
139 	int		cancel;		/* cancellation flag */
140 	void		*ifinfo;	/* interface info */
141 	SLPBoolean	force_multicast; /* for SAAdvert solicitations */
142 	SLPBoolean	internal_call;	/* current call is an internal op */
143 	SLPBoolean	pending_outcall; /* is handle in use? */
144 	mutex_t		outcall_lock;	/* protects pending_outcall */
145 	cond_t		outcall_cv;	/* outcall cond var */
146 	SLPBoolean	close_on_end;	/* cleanup on slp_end_call */
147 } slp_handle_impl_t;
148 
149 extern SLPError slp_start_call(slp_handle_impl_t *);
150 extern void slp_end_call(slp_handle_impl_t *);
151 extern void slp_cleanup_handle(slp_handle_impl_t *);
152 
153 /* UA common functionality */
154 typedef void SLPGenericAppCB();
155 typedef SLPBoolean SLPMsgReplyCB(slp_handle_impl_t *, char *, void (*)(),
156 					void *, void **, int *);
157 
158 extern SLPError slp_ua_common(SLPHandle, const char *, SLPGenericAppCB, void *,
159 				SLPMsgReplyCB);
160 
161 extern SLPError slp_packSrvRqst(const char *, const char *,
162 				slp_handle_impl_t *);
163 extern SLPError slp_packSrvRqst_single(const char *, const char *,
164 					const char *, char **,
165 					const char *);
166 extern SLPBoolean slp_unpackSrvReply(slp_handle_impl_t *, char *,
167 					SLPSrvURLCallback, void *,
168 					void **, int *);
169 extern SLPError slp_packAttrRqst_single(const char *,
170 					const char *,
171 					const char *,
172 					char **,
173 					const char *);
174 extern SLPBoolean slp_UnpackAttrReply(slp_handle_impl_t *, char *,
175 					SLPAttrCallback, void *,
176 					void **, int *);
177 extern SLPError slp_getDAbyScope(const char *, slp_queue_t *);
178 extern SLPError slp_SAAdvert(slp_handle_impl_t *, void *);
179 extern SLPError slp_unpackDAAdvert(char *, char **, char **, char **,
180 					char **, SLPError *);
181 extern SLPError slp_unpackSAAdvert(char *, char **, char **, char **);
182 
183 /* target selection routines */
184 typedef void slp_target_list_t;
185 typedef void slp_target_t;
186 extern SLPError slp_new_target_list(slp_handle_impl_t *hp, const char *,
187 					slp_target_list_t **);
188 extern const char *slp_get_uc_scopes(slp_target_list_t *);
189 extern const char *slp_get_mc_scopes(slp_target_list_t *);
190 extern slp_target_t *slp_next_uc_target(slp_target_list_t *);
191 extern slp_target_t *slp_next_failover(slp_target_t *);
192 extern void *slp_get_target_sin(slp_target_t *);
193 extern void slp_mark_target_used(slp_target_t *);
194 extern void slp_mark_target_failed(slp_target_t *);
195 extern slp_target_t *slp_fabricate_target(void *);
196 extern void slp_free_target(slp_target_t *);
197 extern void slp_destroy_target_list(slp_target_list_t *);
198 
199 /* short-lived DA cache */
200 extern char *slp_find_das_cached(const char *);
201 extern void slp_put_das_cached(const char *, const char *, unsigned int);
202 
203 /* networking */
204 extern void slp_uc_tcp_send(slp_handle_impl_t *, slp_target_t *,
205 				const char *, SLPBoolean, unsigned short);
206 extern void slp_uc_udp_send(slp_handle_impl_t *, slp_target_t *,
207 				const char *);
208 extern void slp_mc_send(slp_handle_impl_t *, const char *);
209 extern void slp_tcp_wait(slp_handle_impl_t *);
210 extern SLPError slp_tcp_read(int, char **);
211 extern char *slp_ntop(char *, int, const void *);
212 extern int slp_pton(const char *, void *);
213 
214 /* IPC */
215 extern SLPError slp_send2slpd(const char *, char **);
216 extern SLPError slp_send2slpd_iov(struct iovec *, int, char **);
217 
218 /* SLP-style list management */
219 extern int slp_onlist(const char *, const char *);
220 extern void slp_add2list(const char *, char **, SLPBoolean);
221 extern void slp_list_subtract(const char *, char **);
222 
223 /* searching and storing */
224 typedef enum { preorder, postorder, endorder, leaf } VISIT;
225 extern void slp_twalk(void *, void (*)(void *, VISIT, int, void *),
226 			int, void *);
227 extern void *slp_tsearch(const void *, void **, int (*)());
228 extern void *slp_tfind(const void *, void *const *,
229 		int (*)(const void *, const void *));
230 
231 /* DA and scope discovery routines */
232 extern SLPError slp_find_das(const char *, char **);
233 extern SLPError slp_administrative_scopes(char **, SLPBoolean);
234 
235 /* UTF8 routines */
236 extern char *slp_utf_strchr(const char *, char);
237 extern int slp_strcasecmp(const char *, const char *);
238 
239 /* Error reporting */
240 extern void slp_err(int, int, char *, char *, ...);
241 
242 /* Mapping from protocol to API error codes */
243 extern SLPError slp_map_err(unsigned short);
244 
245 /* Security: signing and verifying */
246 extern SLPError slp_sign(struct iovec *, int, time_t, struct iovec *, int);
247 extern SLPError slp_verify(struct iovec *, int, const char *,
248 			    size_t, int, size_t *);
249 
250 /* Config convenience wrappers */
251 extern size_t slp_get_mtu();
252 extern int slp_get_next_onlist(char **);
253 extern int slp_get_maxResults();
254 #define	slp_get_mcmaxwait() atoi(SLPGetProperty(SLP_CONFIG_MULTICASTMAXWAIT))
255 #define	slp_get_maxresults() atoi(SLPGetProperty(SLP_CONFIG_MAXRESULTS))
256 #define	slp_get_multicastTTL() atoi(SLPGetProperty(SLP_CONFIG_MULTICASTTTL))
257 #define	slp_get_usebroadcast() \
258 	(!strcasecmp(SLPGetProperty(SLP_CONFIG_ISBROADCASTONLY), "true"))
259 #define	slp_get_security_on() \
260 	(!strcasecmp(SLPGetProperty(SLP_CONFIG_SECURITY_ON), "true"))
261 #define	slp_get_bypass_auth() \
262 	(!strcasecmp(SLPGetProperty(SLP_CONFIG_BYPASS_AUTH), "true"))
263 
264 /* Primitive encoding routines */
265 extern SLPError slp_add_byte(char *, size_t, int, size_t *);
266 extern SLPError slp_add_sht(char *, size_t, unsigned short, size_t *);
267 extern SLPError slp_add_int32(char *, size_t, unsigned int, size_t *);
268 extern SLPError slp_add_string(char *, size_t, const char *, size_t *);
269 extern SLPError slp_get_byte(const char *, size_t, size_t *, int *);
270 extern SLPError slp_get_sht(const char *, size_t, size_t *, unsigned short *);
271 extern SLPError slp_get_int32(const char *, size_t, size_t *, unsigned int *);
272 extern SLPError slp_get_string(const char *, size_t, size_t *, char **);
273 
274 /* Header generation and handling */
275 
276 /* OFFSETS to fields in the header */
277 #define	SLP_VER		0
278 #define	SLP_FUN		1
279 #define	SLP_LEN		2
280 #define	SLP_FLAGS	5
281 #define	SLP_NEXTOP	7
282 #define	SLP_XID		10
283 #define	SLP_LANGLEN	12
284 #define	SLP_HDRLEN	14
285 
286 /* Flags */
287 #define	SLP_OVERFLOW	(char)0x80
288 #define	SLP_FRESH	(char)0x40
289 #define	SLP_MCAST	(char)0x20
290 
291 /* One byte macros (not needing byte order conversion) */
292 #define	slp_get_version(h)	(h)[SLP_VER]
293 #define	slp_set_version(h, v)	(h)[SLP_VER] = (v);
294 #define	slp_get_function(h)	(h)[SLP_FUN]
295 #define	slp_set_function(h, f)	(h)[SLP_FUN] = (f)
296 #define	slp_get_overflow(h)	((h)[SLP_FLAGS] & SLP_OVERFLOW)
297 #define	slp_set_overflow(h)	(h)[SLP_FLAGS] |= SLP_OVERFLOW
298 #define	slp_set_fresh(h)	(h)[SLP_FLAGS] |= SLP_FRESH
299 #define	slp_set_mcast(h)	(h)[SLP_FLAGS] |= SLP_MCAST
300 
301 /* Routines requiring byte order conversions */
302 extern unsigned short slp_header_get_sht(const char *, size_t);
303 extern void slp_header_set_sht(char *, unsigned short, size_t);
304 extern unsigned int slp_header_get_int24(const char *, size_t);
305 extern void slp_header_set_int24(char *, unsigned int, size_t);
306 extern slp_proto_err slp_get_errcode(char *);
307 #define	slp_get_length(h)	slp_header_get_int24((h), SLP_LEN)
308 #define	slp_set_length(h, x)	slp_header_set_int24((h), (int)(x), SLP_LEN)
309 #define	slp_get_langlen(h)	slp_header_get_sht((h), SLP_LANGLEN)
310 #define	slp_set_langlen(h, x)	slp_header_set_sht((h), (x), SLP_LANGLEN)
311 #define	slp_get_option(h)	slp_header_get_int24((h), SLP_NEXTOP)
312 #define	slp_set_option(h, x)	slp_header_set_int24((h), (x), SLP_NEXTOP)
313 #define	slp_get_xid(h)		slp_header_get_sht((h), SLP_XID)
314 #define	slp_set_xid(h, x)	slp_header_set_sht((h), (x), SLP_XID)
315 
316 extern SLPError slp_add_header(const char *, char *, size_t, int,
317 				size_t, size_t *);
318 #define	slp_hdrlang_length(h)	\
319 		(SLP_HDRLEN + strlen(((slp_handle_impl_t *)(h))->locale))
320 
321 #ifdef __cplusplus
322 }
323 #endif
324 
325 #endif	/* _SLP_INTERNAL_H */
326