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