17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <syslog.h>
307c478bd9Sstevel@tonic-gate #include <slp-internal.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate struct surl_node {
337c478bd9Sstevel@tonic-gate 	char *surl;
347c478bd9Sstevel@tonic-gate 	unsigned short lifetime;
357c478bd9Sstevel@tonic-gate };
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate struct caller_bundle {
387c478bd9Sstevel@tonic-gate 	SLPSrvURLCallback *cb;
397c478bd9Sstevel@tonic-gate 	void *cookie;
407c478bd9Sstevel@tonic-gate 	SLPHandle handle;
417c478bd9Sstevel@tonic-gate };
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static int compare_surls(struct surl_node *, struct surl_node *);
447c478bd9Sstevel@tonic-gate static char *collate_surls(char *, unsigned short, void **);
457c478bd9Sstevel@tonic-gate static void traverse_surls(SLPHandle, SLPSrvURLCallback, void *, void *);
467c478bd9Sstevel@tonic-gate static void process_surl_node(void *, VISIT, int, void *);
477c478bd9Sstevel@tonic-gate static SLPBoolean unpackDAAdvert_srv(slp_handle_impl_t *, char *,
487c478bd9Sstevel@tonic-gate 					SLPSrvURLCallback, void *,
497c478bd9Sstevel@tonic-gate 					void **, int *);
507c478bd9Sstevel@tonic-gate static SLPBoolean unpackSAAdvert_srv(slp_handle_impl_t *, char *,
517c478bd9Sstevel@tonic-gate 					SLPSrvURLCallback, void *,
527c478bd9Sstevel@tonic-gate 					void **, int *);
537c478bd9Sstevel@tonic-gate 
SLPFindSrvs(SLPHandle hSLP,const char * pcServiceType,const char * pcScope,const char * pcSearchFilter,SLPSrvURLCallback callback,void * pvUser)547c478bd9Sstevel@tonic-gate SLPError SLPFindSrvs(SLPHandle hSLP, const char *pcServiceType,
557c478bd9Sstevel@tonic-gate 			const char *pcScope, const char *pcSearchFilter,
567c478bd9Sstevel@tonic-gate 			SLPSrvURLCallback callback, void *pvUser) {
577c478bd9Sstevel@tonic-gate 	SLPError err;
587c478bd9Sstevel@tonic-gate 	slp_handle_impl_t *hp = (slp_handle_impl_t *)hSLP;
597c478bd9Sstevel@tonic-gate 	int wantSAAdvert =
607c478bd9Sstevel@tonic-gate 		strcasecmp(pcServiceType, "service:service-agent") == 0;
617c478bd9Sstevel@tonic-gate 	int wantDAAdvert =
627c478bd9Sstevel@tonic-gate 		strcasecmp(pcServiceType, "service:directory-agent") == 0;
637c478bd9Sstevel@tonic-gate 	int isSpecial = wantSAAdvert || wantDAAdvert;
647c478bd9Sstevel@tonic-gate 	SLPMsgReplyCB *unpack_cb;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	if (!hSLP || !pcServiceType || !pcScope || (!*pcScope && !isSpecial) ||
677c478bd9Sstevel@tonic-gate 	    !pcSearchFilter || !callback) {
687c478bd9Sstevel@tonic-gate 		return (SLP_PARAMETER_BAD);
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	if ((strlen(pcServiceType) > SLP_MAX_STRINGLEN) ||
727c478bd9Sstevel@tonic-gate 	    (strlen(pcScope) > SLP_MAX_STRINGLEN) ||
737c478bd9Sstevel@tonic-gate 	    (strlen(pcSearchFilter) > SLP_MAX_STRINGLEN)) {
747c478bd9Sstevel@tonic-gate 	    return (SLP_PARAMETER_BAD);
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	if ((err = slp_start_call(hSLP)) != SLP_OK)
787c478bd9Sstevel@tonic-gate 		return (err);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	/* Special unpacker for DA and SA solicitations */
817c478bd9Sstevel@tonic-gate 	if (wantDAAdvert) {
827c478bd9Sstevel@tonic-gate 		unpack_cb = (SLPMsgReplyCB *)unpackDAAdvert_srv;
837c478bd9Sstevel@tonic-gate 		hp->force_multicast = SLP_TRUE;
847c478bd9Sstevel@tonic-gate 	} else if (wantSAAdvert) {
857c478bd9Sstevel@tonic-gate 		unpack_cb = (SLPMsgReplyCB *)unpackSAAdvert_srv;
867c478bd9Sstevel@tonic-gate 		hp->force_multicast = SLP_TRUE;
877c478bd9Sstevel@tonic-gate 	} else {
887c478bd9Sstevel@tonic-gate 		/* normal service request */
897c478bd9Sstevel@tonic-gate 		unpack_cb = (SLPMsgReplyCB *)slp_unpackSrvReply;
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	err = slp_packSrvRqst(pcServiceType, pcSearchFilter, hp);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	if (err == SLP_OK)
957c478bd9Sstevel@tonic-gate 		err = slp_ua_common(hSLP, pcScope,
96*48d1bcbbSToomas Soome 		    (SLPGenericAppCB *)(uintptr_t)callback, pvUser, unpack_cb);
977c478bd9Sstevel@tonic-gate 	if (err != SLP_OK)
987c478bd9Sstevel@tonic-gate 		slp_end_call(hSLP);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	return (err);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
slp_unpackSrvReply(slp_handle_impl_t * hp,char * reply,SLPSrvURLCallback cb,void * cookie,void ** collator,int * numResults)1037c478bd9Sstevel@tonic-gate SLPBoolean slp_unpackSrvReply(slp_handle_impl_t *hp, char *reply,
1047c478bd9Sstevel@tonic-gate 				SLPSrvURLCallback cb, void *cookie,
1057c478bd9Sstevel@tonic-gate 				void **collator, int *numResults) {
1067c478bd9Sstevel@tonic-gate 	SLPError errCode;
1077c478bd9Sstevel@tonic-gate 	unsigned short urlCount, protoErrCode;
1087c478bd9Sstevel@tonic-gate 	size_t len, off;
1097c478bd9Sstevel@tonic-gate 	int i;
1107c478bd9Sstevel@tonic-gate 	int maxResults = slp_get_maxResults();
1117c478bd9Sstevel@tonic-gate 	SLPBoolean cont = SLP_TRUE;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if (!reply) {
1147c478bd9Sstevel@tonic-gate 		/* no more results */
1157c478bd9Sstevel@tonic-gate 		/* traverse_surls:invoke cb for sync case,and free resources */
1167c478bd9Sstevel@tonic-gate 		if (!hp->async) {
1177c478bd9Sstevel@tonic-gate 		    traverse_surls(hp, cb, cookie, *collator);
1187c478bd9Sstevel@tonic-gate 		}
1197c478bd9Sstevel@tonic-gate 		cb(hp, NULL, 0, SLP_LAST_CALL, cookie);
1207c478bd9Sstevel@tonic-gate 		return (SLP_FALSE);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	len = slp_get_length(reply);
1247c478bd9Sstevel@tonic-gate 	off = SLP_HDRLEN + slp_get_langlen(reply);
1257c478bd9Sstevel@tonic-gate 	/* err code */
1267c478bd9Sstevel@tonic-gate 	if (slp_get_sht(reply, len, &off, &protoErrCode) != SLP_OK)
1277c478bd9Sstevel@tonic-gate 		return (SLP_TRUE);
1287c478bd9Sstevel@tonic-gate 	/* internal errors should have been filtered out by the net code */
1297c478bd9Sstevel@tonic-gate 	if ((errCode = slp_map_err(protoErrCode)) != SLP_OK) {
1307c478bd9Sstevel@tonic-gate 		return (cb(hp, NULL, 0, errCode, cookie));
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	/* url entry count */
1347c478bd9Sstevel@tonic-gate 	if (slp_get_sht(reply, len, &off, &urlCount) != SLP_OK)
1357c478bd9Sstevel@tonic-gate 		return (SLP_TRUE);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	/* for each srvRply, unpack and pass to CB */
1387c478bd9Sstevel@tonic-gate 	for (i = 0; i < urlCount && !hp->cancel; i++) {
1397c478bd9Sstevel@tonic-gate 		char *pcSrvURL;
1407c478bd9Sstevel@tonic-gate 		unsigned short sLifetime;
1417c478bd9Sstevel@tonic-gate 		int nURLAuthBlocks;
1427c478bd9Sstevel@tonic-gate 		size_t tbv_len;
1437c478bd9Sstevel@tonic-gate 		char *url_tbv;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		/* parse URL entry into params */
1467c478bd9Sstevel@tonic-gate 		off++;	/* skip reserved byte */
1477c478bd9Sstevel@tonic-gate 		/* lifetime */
1487c478bd9Sstevel@tonic-gate 		if (slp_get_sht(reply, len, &off, &sLifetime) != SLP_OK)
1497c478bd9Sstevel@tonic-gate 			return (SLP_TRUE);
1507c478bd9Sstevel@tonic-gate 		/* URL itself; keep track of it in case we need to verify */
1517c478bd9Sstevel@tonic-gate 		url_tbv = reply + off;
1527c478bd9Sstevel@tonic-gate 		tbv_len = off;
1537c478bd9Sstevel@tonic-gate 		if (slp_get_string(reply, len, &off, &pcSrvURL) != SLP_OK)
1547c478bd9Sstevel@tonic-gate 			return (SLP_TRUE);
1557c478bd9Sstevel@tonic-gate 		tbv_len = off - tbv_len;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		/* number of url auths */
1587c478bd9Sstevel@tonic-gate 		if (slp_get_byte(reply, len, &off, &nURLAuthBlocks) != SLP_OK)
1597c478bd9Sstevel@tonic-gate 			goto cleanup;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 		/* get and verify auth blocks */
1627c478bd9Sstevel@tonic-gate 		if ((!hp->internal_call && slp_get_security_on()) ||
1637c478bd9Sstevel@tonic-gate 		    nURLAuthBlocks > 0) {
1647c478bd9Sstevel@tonic-gate 			struct iovec iov[1];
1657c478bd9Sstevel@tonic-gate 			size_t abLen = 0;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 			iov[0].iov_base = url_tbv;
1687c478bd9Sstevel@tonic-gate 			iov[0].iov_len = tbv_len;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 			if (slp_verify(iov, 1,
1717c478bd9Sstevel@tonic-gate 					reply + off,
1727c478bd9Sstevel@tonic-gate 					len - off,
1737c478bd9Sstevel@tonic-gate 					nURLAuthBlocks,
1747c478bd9Sstevel@tonic-gate 					&abLen) != SLP_OK) {
1757c478bd9Sstevel@tonic-gate 			    goto cleanup;
1767c478bd9Sstevel@tonic-gate 			}
1777c478bd9Sstevel@tonic-gate 			off += abLen;
1787c478bd9Sstevel@tonic-gate 		}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		/* collate the srv urls for sync behavior */
1817c478bd9Sstevel@tonic-gate 		if (!hp->async) {
1827c478bd9Sstevel@tonic-gate 		    pcSrvURL = collate_surls(pcSrvURL, sLifetime, collator);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		    if (!pcSrvURL)
1857c478bd9Sstevel@tonic-gate 			continue;
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		(*numResults)++;
1897c478bd9Sstevel@tonic-gate 		/* invoke cb */
1907c478bd9Sstevel@tonic-gate 		if (hp->async)
1917c478bd9Sstevel@tonic-gate 			cont = cb(
1927c478bd9Sstevel@tonic-gate 				(SLPHandle) hp,
1937c478bd9Sstevel@tonic-gate 				pcSrvURL,
1947c478bd9Sstevel@tonic-gate 				sLifetime,
1957c478bd9Sstevel@tonic-gate 				errCode,
1967c478bd9Sstevel@tonic-gate 				cookie);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 		/* cleanup */
1997c478bd9Sstevel@tonic-gate cleanup:
2007c478bd9Sstevel@tonic-gate 		free(pcSrvURL);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 		/* check maxResults */
2037c478bd9Sstevel@tonic-gate 		if (!hp->internal_call && *numResults == maxResults) {
2047c478bd9Sstevel@tonic-gate 			cont = SLP_FALSE;
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		if (!cont) break;
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	return (cont);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate  * unpackDAAdvert_srv follows the same same logic flow as slp_unpackSrvReply
2157c478bd9Sstevel@tonic-gate  * with two differences: the message in reply is a DAAdvert, and
2167c478bd9Sstevel@tonic-gate  * this function is not used internally, so hp is never NULL. Although
2177c478bd9Sstevel@tonic-gate  * all info from a DAAdvert is returned by slp_unpackDAAdvert, here
2187c478bd9Sstevel@tonic-gate  * the recipient (the user-supplied SLPSrvURLCallback) is interested
2197c478bd9Sstevel@tonic-gate  * only in the DA service URL.
2207c478bd9Sstevel@tonic-gate  */
unpackDAAdvert_srv(slp_handle_impl_t * hp,char * reply,SLPSrvURLCallback cb,void * cookie,void ** collator,int * numResults)2217c478bd9Sstevel@tonic-gate static SLPBoolean unpackDAAdvert_srv(slp_handle_impl_t *hp, char *reply,
2227c478bd9Sstevel@tonic-gate 					SLPSrvURLCallback cb, void *cookie,
2237c478bd9Sstevel@tonic-gate 					void **collator, int *numResults) {
2247c478bd9Sstevel@tonic-gate 	char *surl, *scopes, *attrs, *spis;
2257c478bd9Sstevel@tonic-gate 	SLPBoolean cont = SLP_TRUE;
2267c478bd9Sstevel@tonic-gate 	SLPError errCode;
2277c478bd9Sstevel@tonic-gate 	int maxResults = slp_get_maxResults();
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	if (!reply) {
2307c478bd9Sstevel@tonic-gate 		/* no more results */
2317c478bd9Sstevel@tonic-gate 		/* traverse_surls:invoke cb for sync case,and free resources */
2327c478bd9Sstevel@tonic-gate 		if (!hp->async) {
2337c478bd9Sstevel@tonic-gate 			traverse_surls(hp, cb, cookie, *collator);
2347c478bd9Sstevel@tonic-gate 		}
2357c478bd9Sstevel@tonic-gate 		cb(hp, NULL, 0, SLP_LAST_CALL, cookie);
2367c478bd9Sstevel@tonic-gate 		return (SLP_FALSE);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	if (slp_unpackDAAdvert(reply, &surl, &scopes, &attrs, &spis, &errCode)
2407c478bd9Sstevel@tonic-gate 	    != SLP_OK) {
2417c478bd9Sstevel@tonic-gate 		return (SLP_TRUE);
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 	if (errCode != SLP_OK) {
2447c478bd9Sstevel@tonic-gate 		return (cb(hp, NULL, 0, errCode, cookie));
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/* collate the urls */
2487c478bd9Sstevel@tonic-gate 	surl = collate_surls(surl, 0, collator);
2497c478bd9Sstevel@tonic-gate 	if (!surl) {
2507c478bd9Sstevel@tonic-gate 		return (SLP_TRUE);
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	(*numResults)++;
2547c478bd9Sstevel@tonic-gate 	if (hp->async) {
2557c478bd9Sstevel@tonic-gate 		cont = cb((SLPHandle)hp, surl, 0, errCode, cookie);
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	/* cleanup */
2597c478bd9Sstevel@tonic-gate 	free(surl);
2607c478bd9Sstevel@tonic-gate 	free(scopes);
2617c478bd9Sstevel@tonic-gate 	free(attrs);
2627c478bd9Sstevel@tonic-gate 	free(spis);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/* check maxResults */
2657c478bd9Sstevel@tonic-gate 	if (!hp->internal_call && *numResults == maxResults) {
2667c478bd9Sstevel@tonic-gate 		return (SLP_FALSE);
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	return (cont);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate /*
2727c478bd9Sstevel@tonic-gate  * unpackSAAdvert_srv follows the same same logic flow as slp_unpackSrvReply
2737c478bd9Sstevel@tonic-gate  * with two differences: the message in reply is a SAAdvert, and
2747c478bd9Sstevel@tonic-gate  * this function is not used internally, so hp is never NULL. Although
2757c478bd9Sstevel@tonic-gate  * all info from an SAAdvert is returned by slp_unpackSAAdvert, here
2767c478bd9Sstevel@tonic-gate  * the recipient (the user-supplied SLPSrvURLCallback) is interested
2777c478bd9Sstevel@tonic-gate  * only in the SA service URL.
2787c478bd9Sstevel@tonic-gate  */
unpackSAAdvert_srv(slp_handle_impl_t * hp,char * reply,SLPSrvURLCallback cb,void * cookie,void ** collator,int * numResults)2797c478bd9Sstevel@tonic-gate static SLPBoolean unpackSAAdvert_srv(slp_handle_impl_t *hp, char *reply,
2807c478bd9Sstevel@tonic-gate 					SLPSrvURLCallback cb, void *cookie,
2817c478bd9Sstevel@tonic-gate 					void **collator, int *numResults) {
2827c478bd9Sstevel@tonic-gate 	char *surl, *scopes, *attrs;
2837c478bd9Sstevel@tonic-gate 	SLPBoolean cont = SLP_TRUE;
2847c478bd9Sstevel@tonic-gate 	int maxResults = slp_get_maxResults();
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	if (!reply) {
2877c478bd9Sstevel@tonic-gate 		/* no more results */
2887c478bd9Sstevel@tonic-gate 		/* traverse_surls:invoke cb for sync case,and free resources */
2897c478bd9Sstevel@tonic-gate 		if (!hp->async) {
2907c478bd9Sstevel@tonic-gate 			/* sync case */
2917c478bd9Sstevel@tonic-gate 			traverse_surls(hp, cb, cookie, *collator);
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 		cb(hp, NULL, 0, SLP_LAST_CALL, cookie);
2947c478bd9Sstevel@tonic-gate 		return (SLP_FALSE);
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if (slp_unpackSAAdvert(reply, &surl, &scopes, &attrs) != SLP_OK) {
2987c478bd9Sstevel@tonic-gate 		return (SLP_TRUE);
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	/* collate the urls */
3027c478bd9Sstevel@tonic-gate 	surl = collate_surls(surl, 0, collator);
3037c478bd9Sstevel@tonic-gate 	if (!surl) {
3047c478bd9Sstevel@tonic-gate 		return (SLP_TRUE);
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	(*numResults)++;
3087c478bd9Sstevel@tonic-gate 	if (hp->async) {
3097c478bd9Sstevel@tonic-gate 		cont = cb((SLPHandle)hp, surl, 0, SLP_OK, cookie);
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	/* cleanup */
3137c478bd9Sstevel@tonic-gate 	free(surl);
3147c478bd9Sstevel@tonic-gate 	free(scopes);
3157c478bd9Sstevel@tonic-gate 	free(attrs);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	/* check maxResults */
3187c478bd9Sstevel@tonic-gate 	if (!hp->internal_call && *numResults == maxResults) {
3197c478bd9Sstevel@tonic-gate 		return (SLP_FALSE);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	return (cont);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
slp_packSrvRqst(const char * type,const char * filter,slp_handle_impl_t * hp)3257c478bd9Sstevel@tonic-gate SLPError slp_packSrvRqst(const char *type,
3267c478bd9Sstevel@tonic-gate 				const char *filter,
3277c478bd9Sstevel@tonic-gate 				slp_handle_impl_t *hp) {
3287c478bd9Sstevel@tonic-gate 	SLPError err;
3297c478bd9Sstevel@tonic-gate 	size_t len, msgLen, tmplen;
3307c478bd9Sstevel@tonic-gate 	slp_msg_t *msg = &(hp->msg);
3317c478bd9Sstevel@tonic-gate 	char *spi = NULL;
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	if (slp_get_security_on()) {
3347c478bd9Sstevel@tonic-gate 	    spi = (char *)SLPGetProperty(SLP_CONFIG_SPI);
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if (!spi || !*spi) {
3387c478bd9Sstevel@tonic-gate 		spi = "";
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/*
3427c478bd9Sstevel@tonic-gate 	 * Allocate iovec for the messge. A SrvRqst is layed out thus:
3437c478bd9Sstevel@tonic-gate 	 *  0: header
3447c478bd9Sstevel@tonic-gate 	 *  1: prlist length
3457c478bd9Sstevel@tonic-gate 	 *  2: prlist (filled in later by networking code)
3467c478bd9Sstevel@tonic-gate 	 *  3: service type string
3477c478bd9Sstevel@tonic-gate 	 *  4: scopes length
3487c478bd9Sstevel@tonic-gate 	 *  5: scopes (filled in later by networking code)
3497c478bd9Sstevel@tonic-gate 	 *  6: predicate string and SPI string
3507c478bd9Sstevel@tonic-gate 	 */
3517c478bd9Sstevel@tonic-gate 	if (!(msg->iov = calloc(7, sizeof (*(msg->iov))))) {
3527c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "slp_packSrvRqst", "out of memory");
3537c478bd9Sstevel@tonic-gate 		return (SLP_MEMORY_ALLOC_FAILED);
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 	msg->iovlen = 7;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	/* calculate msg length */
3587c478bd9Sstevel@tonic-gate 	msgLen = 2 +		/* prlist length */
3597c478bd9Sstevel@tonic-gate 	    2 + strlen(type) +	/* service type */
3607c478bd9Sstevel@tonic-gate 	    2 +			/* scope list length */
3617c478bd9Sstevel@tonic-gate 	    2 + strlen(filter) + /* predicate string */
3627c478bd9Sstevel@tonic-gate 	    2 + strlen(spi);	/* SPI string */
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	if (!(msg->msg = calloc(1, msgLen))) {
3657c478bd9Sstevel@tonic-gate 		free(msg->iov);
3667c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "slp_packSrvRqst", "out of memory");
3677c478bd9Sstevel@tonic-gate 		return (SLP_MEMORY_ALLOC_FAILED);
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	/* set pointer to PR list and scope list length spaces */
3717c478bd9Sstevel@tonic-gate 	msg->prlistlen.iov_base = msg->msg;
3727c478bd9Sstevel@tonic-gate 	msg->prlistlen.iov_len = 2;
3737c478bd9Sstevel@tonic-gate 	msg->iov[1].iov_base = msg->msg;
3747c478bd9Sstevel@tonic-gate 	msg->iov[1].iov_len = 2;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	msg->scopeslen.iov_base = msg->msg + 2;
3777c478bd9Sstevel@tonic-gate 	msg->scopeslen.iov_len = 2;
3787c478bd9Sstevel@tonic-gate 	msg->iov[4].iov_base = msg->msg + 2;
3797c478bd9Sstevel@tonic-gate 	msg->iov[4].iov_len = 2;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	/* set up the scopes and prlist pointers into iov */
3827c478bd9Sstevel@tonic-gate 	msg->prlist = &(msg->iov[2]);
3837c478bd9Sstevel@tonic-gate 	msg->scopes = &(msg->iov[5]);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	len = 4;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/* Add type string */
3887c478bd9Sstevel@tonic-gate 	msg->iov[3].iov_base = msg->msg + len;
3897c478bd9Sstevel@tonic-gate 	tmplen = len;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	err = slp_add_string(msg->msg, msgLen, type, &len);
3927c478bd9Sstevel@tonic-gate 	msg->iov[3].iov_len = len - tmplen;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	if (err != SLP_OK)
3957c478bd9Sstevel@tonic-gate 		goto error;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	/* Add search filter */
3987c478bd9Sstevel@tonic-gate 	msg->iov[6].iov_base = msg->msg + len;
3997c478bd9Sstevel@tonic-gate 	tmplen = len;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	err = slp_add_string(msg->msg, msgLen, filter, &len);
4027c478bd9Sstevel@tonic-gate 	if (err != SLP_OK)
4037c478bd9Sstevel@tonic-gate 		goto error;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	err = slp_add_string(msg->msg, msgLen, spi, &len);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	msg->iov[6].iov_len = len - tmplen;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	hp->fid = SRVRQST;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	if (err == SLP_OK) {
4127c478bd9Sstevel@tonic-gate 		return (err);
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	/* else error */
4167c478bd9Sstevel@tonic-gate error:
4177c478bd9Sstevel@tonic-gate 	free(msg->iov);
4187c478bd9Sstevel@tonic-gate 	free(msg->msg);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	return (err);
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate /*
4247c478bd9Sstevel@tonic-gate  * Caller must free msg
4257c478bd9Sstevel@tonic-gate  */
slp_packSrvRqst_single(const char * type,const char * scopes,const char * filter,char ** msg,const char * lang)4267c478bd9Sstevel@tonic-gate SLPError slp_packSrvRqst_single(const char *type,
4277c478bd9Sstevel@tonic-gate 				const char *scopes,
4287c478bd9Sstevel@tonic-gate 				const char *filter,
4297c478bd9Sstevel@tonic-gate 				char **msg,
4307c478bd9Sstevel@tonic-gate 				const char *lang) {
4317c478bd9Sstevel@tonic-gate 	SLPError err;
4327c478bd9Sstevel@tonic-gate 	size_t len, msgLen;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	msgLen =
4357c478bd9Sstevel@tonic-gate 		SLP_HDRLEN + strlen(lang) + 2 +
4367c478bd9Sstevel@tonic-gate 		2 + strlen(type) +
4377c478bd9Sstevel@tonic-gate 		2 + strlen(scopes) +
4387c478bd9Sstevel@tonic-gate 		2 + strlen(filter) +
4397c478bd9Sstevel@tonic-gate 		2; /* No SPI string for internal calls */
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	if (!(*msg = calloc(msgLen, 1))) {
4427c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "slp_packSrvRqst_single",
4437c478bd9Sstevel@tonic-gate 			"out of memory");
4447c478bd9Sstevel@tonic-gate 		return (SLP_MEMORY_ALLOC_FAILED);
4457c478bd9Sstevel@tonic-gate 	}
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	len = 0;
4487c478bd9Sstevel@tonic-gate 	err = slp_add_header(lang, *msg, msgLen, SRVRQST, msgLen, &len);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	len += 2;	/* empty PR list */
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	if (err == SLP_OK)
4537c478bd9Sstevel@tonic-gate 		err = slp_add_string(*msg, msgLen, type, &len);
4547c478bd9Sstevel@tonic-gate 	if (err == SLP_OK)
4557c478bd9Sstevel@tonic-gate 		err = slp_add_string(*msg, msgLen, scopes, &len);
4567c478bd9Sstevel@tonic-gate 	if (err == SLP_OK)
4577c478bd9Sstevel@tonic-gate 		err = slp_add_string(*msg, msgLen, filter, &len);
4587c478bd9Sstevel@tonic-gate 	if (err == SLP_OK)
4597c478bd9Sstevel@tonic-gate 		/* empty SPI string */
4607c478bd9Sstevel@tonic-gate 		err = slp_add_string(*msg, msgLen, "", &len);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	return (err);
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 
compare_surls(struct surl_node * s1,struct surl_node * s2)4667c478bd9Sstevel@tonic-gate static int compare_surls(struct surl_node *s1, struct surl_node *s2) {
4677c478bd9Sstevel@tonic-gate 	if (s1->lifetime != s2->lifetime)
4687c478bd9Sstevel@tonic-gate 		return (s1->lifetime - s2->lifetime);
4697c478bd9Sstevel@tonic-gate 	return (slp_strcasecmp(s1->surl, s2->surl));
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate /*
4737c478bd9Sstevel@tonic-gate  * Using the collator, determine if this URL has already been processed.
4747c478bd9Sstevel@tonic-gate  * If so, free surl and return NULL, else return the URL.
4757c478bd9Sstevel@tonic-gate  */
collate_surls(char * surl,unsigned short life,void ** collator)4767c478bd9Sstevel@tonic-gate static char *collate_surls(char *surl, unsigned short life, void **collator) {
4777c478bd9Sstevel@tonic-gate 	struct surl_node *n, **res;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	if (!(n = malloc(sizeof (*n)))) {
4807c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "collate_surls", "out of memory");
4817c478bd9Sstevel@tonic-gate 		return (NULL);
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 	if (!(n->surl = strdup(surl))) {
4847c478bd9Sstevel@tonic-gate 		free(n);
4857c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "collate_surls", "out of memory");
4867c478bd9Sstevel@tonic-gate 		return (NULL);
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 	n->lifetime = life;
4897c478bd9Sstevel@tonic-gate 	res = slp_tsearch((void *) n, collator,
4907c478bd9Sstevel@tonic-gate 			(int (*)(const void *, const void *)) compare_surls);
4917c478bd9Sstevel@tonic-gate 	if (*res == n) {
4927c478bd9Sstevel@tonic-gate 		/* first time we've encountered this url */
4937c478bd9Sstevel@tonic-gate 		return (surl);
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 	/* else  already in tree */
4967c478bd9Sstevel@tonic-gate 	free(n->surl);
4977c478bd9Sstevel@tonic-gate 	free(n);
4987c478bd9Sstevel@tonic-gate 	free(surl);
4997c478bd9Sstevel@tonic-gate 	return (NULL);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
traverse_surls(SLPHandle h,SLPSrvURLCallback cb,void * cookie,void * collator)5027c478bd9Sstevel@tonic-gate static void traverse_surls(SLPHandle h, SLPSrvURLCallback cb,
5037c478bd9Sstevel@tonic-gate 				void *cookie, void *collator) {
5047c478bd9Sstevel@tonic-gate 	struct caller_bundle caller[1];
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	if (!collator)
5077c478bd9Sstevel@tonic-gate 		return;
5087c478bd9Sstevel@tonic-gate 	caller->cb = cb;
5097c478bd9Sstevel@tonic-gate 	caller->cookie = cookie;
5107c478bd9Sstevel@tonic-gate 	caller->handle = h;
5117c478bd9Sstevel@tonic-gate 	slp_twalk(collator, process_surl_node, 0, caller);
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate /*ARGSUSED*/
process_surl_node(void * node,VISIT order,int level,void * c)5157c478bd9Sstevel@tonic-gate static void process_surl_node(void *node, VISIT order, int level, void *c) {
5167c478bd9Sstevel@tonic-gate 	struct surl_node *n;
5177c478bd9Sstevel@tonic-gate 	SLPSrvURLCallback *cb;
5187c478bd9Sstevel@tonic-gate 	slp_handle_impl_t *h;
5197c478bd9Sstevel@tonic-gate 	struct caller_bundle *caller = (struct caller_bundle *)c;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	if (order == endorder || order == leaf) {
5227c478bd9Sstevel@tonic-gate 		SLPBoolean cont = SLP_TRUE;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 		cb = caller->cb;
5257c478bd9Sstevel@tonic-gate 		h = (slp_handle_impl_t *)caller->handle;
5267c478bd9Sstevel@tonic-gate 		n = *(struct surl_node **)node;
5277c478bd9Sstevel@tonic-gate 		/* invoke cb */
5287c478bd9Sstevel@tonic-gate 		if (cont && (!h || !h->async))
5297c478bd9Sstevel@tonic-gate 			cont = cb(
5307c478bd9Sstevel@tonic-gate 				h, n->surl,
5317c478bd9Sstevel@tonic-gate 				n->lifetime,
5327c478bd9Sstevel@tonic-gate 				SLP_OK,
5337c478bd9Sstevel@tonic-gate 				caller->cookie);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 		free(n->surl);
5367c478bd9Sstevel@tonic-gate 		free(n);
5377c478bd9Sstevel@tonic-gate 		free(node);
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate }
540