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 (c) 1999 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate  * All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <syslog.h>
287c478bd9Sstevel@tonic-gate #include <slp-internal.h>
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate static SLPBoolean UnpackSrvTypesReply(slp_handle_impl_t *, char *,
317c478bd9Sstevel@tonic-gate 					SLPSrvTypeCallback, void *,
327c478bd9Sstevel@tonic-gate 					void **, int *);
337c478bd9Sstevel@tonic-gate static SLPError slp_packSrvTypeRqst(slp_handle_impl_t *, const char *);
347c478bd9Sstevel@tonic-gate static char *collate_types(char *, void **, int *, int);
357c478bd9Sstevel@tonic-gate static char *build_types_list(void *);
367c478bd9Sstevel@tonic-gate static void collect_types(void *, VISIT, int, void *);
377c478bd9Sstevel@tonic-gate 
SLPFindSrvTypes(SLPHandle hSLP,const char * pcNamingAuthority,const char * pcScopeList,SLPSrvTypeCallback callback,void * pvUser)387c478bd9Sstevel@tonic-gate SLPError SLPFindSrvTypes(SLPHandle hSLP, const char *pcNamingAuthority,
397c478bd9Sstevel@tonic-gate 				const char *pcScopeList,
407c478bd9Sstevel@tonic-gate 				SLPSrvTypeCallback callback, void *pvUser) {
417c478bd9Sstevel@tonic-gate 	SLPError err;
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate 	if (!hSLP || !pcNamingAuthority || !pcScopeList ||
447c478bd9Sstevel@tonic-gate 	    !*pcScopeList || !callback) {
457c478bd9Sstevel@tonic-gate 		return (SLP_PARAMETER_BAD);
467c478bd9Sstevel@tonic-gate 	}
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	if ((strlen(pcNamingAuthority) > SLP_MAX_STRINGLEN) ||
497c478bd9Sstevel@tonic-gate 	    (strlen(pcScopeList) > SLP_MAX_STRINGLEN)) {
507c478bd9Sstevel@tonic-gate 	    return (SLP_PARAMETER_BAD);
517c478bd9Sstevel@tonic-gate 	}
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	if ((err = slp_start_call(hSLP)) != SLP_OK)
547c478bd9Sstevel@tonic-gate 		return (err);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	/* format params into msgBuf */
577c478bd9Sstevel@tonic-gate 	err = slp_packSrvTypeRqst(hSLP, pcNamingAuthority);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	if (err == SLP_OK)
607c478bd9Sstevel@tonic-gate 		err = slp_ua_common(hSLP, pcScopeList,
61*48d1bcbbSToomas Soome 		    (SLPGenericAppCB *)(uintptr_t)callback, pvUser,
62*48d1bcbbSToomas Soome 		    (SLPMsgReplyCB *) UnpackSrvTypesReply);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	if (err != SLP_OK)
657c478bd9Sstevel@tonic-gate 		slp_end_call(hSLP);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	return (err);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
UnpackSrvTypesReply(slp_handle_impl_t * hp,char * reply,SLPSrvTypeCallback cb,void * cookie,void ** collator,int * numResults)707c478bd9Sstevel@tonic-gate static SLPBoolean UnpackSrvTypesReply(slp_handle_impl_t *hp, char *reply,
717c478bd9Sstevel@tonic-gate 					SLPSrvTypeCallback cb, void *cookie,
727c478bd9Sstevel@tonic-gate 					void **collator, int *numResults) {
737c478bd9Sstevel@tonic-gate 	char *pcSrvTypes;
747c478bd9Sstevel@tonic-gate 	SLPError errCode;
757c478bd9Sstevel@tonic-gate 	unsigned short protoErrCode;
767c478bd9Sstevel@tonic-gate 	size_t off, len;
777c478bd9Sstevel@tonic-gate 	int maxResults = slp_get_maxResults();
787c478bd9Sstevel@tonic-gate 	SLPBoolean cont = SLP_TRUE;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	if (!reply) {
817c478bd9Sstevel@tonic-gate 		/* no more results */
827c478bd9Sstevel@tonic-gate 		if (!hp->async) {
837c478bd9Sstevel@tonic-gate 		    pcSrvTypes = build_types_list(*collator);
847c478bd9Sstevel@tonic-gate 		}
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 		if (!hp->async && pcSrvTypes) {
877c478bd9Sstevel@tonic-gate 		    /* synchronous case */
887c478bd9Sstevel@tonic-gate 		    cb(hp, pcSrvTypes, SLP_OK, cookie);
897c478bd9Sstevel@tonic-gate 		    free(pcSrvTypes);
907c478bd9Sstevel@tonic-gate 		}
917c478bd9Sstevel@tonic-gate 		cb(hp, NULL, SLP_LAST_CALL, cookie);
927c478bd9Sstevel@tonic-gate 		return (SLP_FALSE);
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	/* parse reply into params */
967c478bd9Sstevel@tonic-gate 	len = slp_get_length(reply);
977c478bd9Sstevel@tonic-gate 	off = SLP_HDRLEN + slp_get_langlen(reply);
987c478bd9Sstevel@tonic-gate 	/* error code */
997c478bd9Sstevel@tonic-gate 	if (slp_get_sht(reply, len, &off, &protoErrCode) != SLP_OK)
1007c478bd9Sstevel@tonic-gate 		return (SLP_TRUE);
1017c478bd9Sstevel@tonic-gate 	/* internal errors should have been filtered out by the net code */
1027c478bd9Sstevel@tonic-gate 	if ((errCode = slp_map_err(protoErrCode)) != SLP_OK) {
1037c478bd9Sstevel@tonic-gate 		return (cb(hp, NULL, errCode, cookie));
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	/* types string */
1077c478bd9Sstevel@tonic-gate 	if (slp_get_string(reply, len, &off, &pcSrvTypes) != SLP_OK)
1087c478bd9Sstevel@tonic-gate 		return (SLP_TRUE);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/* collate the types for sync behavior */
1117c478bd9Sstevel@tonic-gate 	if (!hp->async) {
1127c478bd9Sstevel@tonic-gate 	    pcSrvTypes = collate_types(pcSrvTypes, collator,
1137c478bd9Sstevel@tonic-gate 					numResults, maxResults);
1147c478bd9Sstevel@tonic-gate 	    if (!pcSrvTypes)
1157c478bd9Sstevel@tonic-gate 		return (SLP_TRUE);
1167c478bd9Sstevel@tonic-gate 	} else {
1177c478bd9Sstevel@tonic-gate 	    /* async; invoke cb */
1187c478bd9Sstevel@tonic-gate 	    cont = cb((SLPHandle) hp, pcSrvTypes, errCode, cookie);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	/* cleanup */
1227c478bd9Sstevel@tonic-gate 	free(pcSrvTypes);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	/* check maxResults */
1257c478bd9Sstevel@tonic-gate 	if (!hp->internal_call && *numResults == maxResults) {
1267c478bd9Sstevel@tonic-gate 		return (SLP_FALSE);
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	return (cont);
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate 
slp_packSrvTypeRqst(slp_handle_impl_t * hp,const char * na)1327c478bd9Sstevel@tonic-gate static SLPError slp_packSrvTypeRqst(slp_handle_impl_t *hp, const char *na) {
1337c478bd9Sstevel@tonic-gate 	SLPError err;
1347c478bd9Sstevel@tonic-gate 	size_t len, nalen, msgLen, tmplen;
1357c478bd9Sstevel@tonic-gate 	int all_nas;
1367c478bd9Sstevel@tonic-gate 	slp_msg_t *msg = &(hp->msg);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	/*
1397c478bd9Sstevel@tonic-gate 	 * Allocate iovec for the message. A SrvTypeRqst is layed out thus:
1407c478bd9Sstevel@tonic-gate 	 *  0: header
1417c478bd9Sstevel@tonic-gate 	 *  1: prlist length
1427c478bd9Sstevel@tonic-gate 	 *  2: prlist (filled in later by networking code)
1437c478bd9Sstevel@tonic-gate 	 *  3: na
1447c478bd9Sstevel@tonic-gate 	 *  4: scopes length
1457c478bd9Sstevel@tonic-gate 	 *  5: scopes (filled in later by networking code)
1467c478bd9Sstevel@tonic-gate 	 */
1477c478bd9Sstevel@tonic-gate 	if (!(msg->iov = calloc(6, sizeof (*(msg->iov))))) {
1487c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "slp_packSrvTypeRqst", "out of memory");
1497c478bd9Sstevel@tonic-gate 		return (SLP_MEMORY_ALLOC_FAILED);
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 	msg->iovlen = 6;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/* calculate msg length */
1547c478bd9Sstevel@tonic-gate 	all_nas = strcmp(na, "*") == 0 ? 1 : 0;
1557c478bd9Sstevel@tonic-gate 	if (all_nas) {
1567c478bd9Sstevel@tonic-gate 		nalen = 0;
1577c478bd9Sstevel@tonic-gate 	} else {
1587c478bd9Sstevel@tonic-gate 		nalen = strlen(na);
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 	nalen += 2;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	msgLen = 2 +	/* prlist length */
1637c478bd9Sstevel@tonic-gate 	    nalen +	/* NA string */
1647c478bd9Sstevel@tonic-gate 	    2;		/* Scope string length */
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if (!(msg->msg = calloc(1, msgLen))) {
1677c478bd9Sstevel@tonic-gate 		free(msg->iov);
1687c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "slp_packSrvTypeRqst", "out of memory");
1697c478bd9Sstevel@tonic-gate 		return (SLP_MEMORY_ALLOC_FAILED);
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	/* set pointer to PR list and scope list length spaces */
1737c478bd9Sstevel@tonic-gate 	msg->prlistlen.iov_base = msg->msg;
1747c478bd9Sstevel@tonic-gate 	msg->prlistlen.iov_len = 2;
1757c478bd9Sstevel@tonic-gate 	msg->iov[1].iov_base = msg->msg;
1767c478bd9Sstevel@tonic-gate 	msg->iov[1].iov_len = 2;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	msg->scopeslen.iov_base = msg->msg + 2;
1797c478bd9Sstevel@tonic-gate 	msg->scopeslen.iov_len = 2;
1807c478bd9Sstevel@tonic-gate 	msg->iov[4].iov_base = msg->msg + 2;
1817c478bd9Sstevel@tonic-gate 	msg->iov[4].iov_len = 2;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/* set up the scopes and prlist pointers into iov */
1847c478bd9Sstevel@tonic-gate 	msg->prlist = &(msg->iov[2]);
1857c478bd9Sstevel@tonic-gate 	msg->scopes = &(msg->iov[5]);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	len = 4;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	/* set up NA string in iovec */
1907c478bd9Sstevel@tonic-gate 	msg->iov[3].iov_base = msg->msg + len;
1917c478bd9Sstevel@tonic-gate 	tmplen = len;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	if (all_nas) {
1947c478bd9Sstevel@tonic-gate 		err = slp_add_sht(msg->msg, msgLen, 0xffff, &len);
1957c478bd9Sstevel@tonic-gate 	} else {
1967c478bd9Sstevel@tonic-gate 		err = slp_add_string(msg->msg, msgLen, na, &len);
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 	msg->iov[3].iov_len = len - tmplen;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	hp->fid = SRVTYPERQST;
2017c478bd9Sstevel@tonic-gate 	if (err == SLP_OK) {
2027c478bd9Sstevel@tonic-gate 		return (SLP_OK);
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	/* else error */
2067c478bd9Sstevel@tonic-gate 	free(msg->iov);
2077c478bd9Sstevel@tonic-gate 	free(msg->msg);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	return (err);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate  * Using the collator, determines which types in the types list
2147c478bd9Sstevel@tonic-gate  * have already been recieved, and composes a new list of the remaining
2157c478bd9Sstevel@tonic-gate  * (unique) types. If there are no unique types, returns NULL;
2167c478bd9Sstevel@tonic-gate  * types is destructively modified.
2177c478bd9Sstevel@tonic-gate  */
collate_types(char * types,void ** collator,int * numResults,int maxResults)2187c478bd9Sstevel@tonic-gate static char *collate_types(char *types, void **collator,
2197c478bd9Sstevel@tonic-gate 				int *numResults, int maxResults) {
2207c478bd9Sstevel@tonic-gate 	char *p, *s, **res, *utypes = NULL;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/* walk through the types list */
2237c478bd9Sstevel@tonic-gate 	p = types;
2247c478bd9Sstevel@tonic-gate 	for (s = types; p && *numResults != maxResults; s = p) {
2257c478bd9Sstevel@tonic-gate 		p = slp_utf_strchr(s, ',');
2267c478bd9Sstevel@tonic-gate 		if (p)
2277c478bd9Sstevel@tonic-gate 			*p++ = 0;
2287c478bd9Sstevel@tonic-gate 		if (!(s = strdup(s))) {
2297c478bd9Sstevel@tonic-gate 		    free(types);
2307c478bd9Sstevel@tonic-gate 		    if (utypes) free(utypes);
2317c478bd9Sstevel@tonic-gate 		    slp_err(LOG_CRIT, 0, "collate_types", "out of memory");
2327c478bd9Sstevel@tonic-gate 		    return (NULL);
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 		/* search the tree for this type */
2357c478bd9Sstevel@tonic-gate 		res = slp_tsearch((void *) s, collator,
2367c478bd9Sstevel@tonic-gate 			(int (*)(const void *, const void *)) slp_strcasecmp);
2377c478bd9Sstevel@tonic-gate 		if (*res == s) {
2387c478bd9Sstevel@tonic-gate 			/* first time we've encountered this type */
2397c478bd9Sstevel@tonic-gate 			slp_add2list(s, &utypes, SLP_FALSE);
2407c478bd9Sstevel@tonic-gate 			(*numResults)++;
2417c478bd9Sstevel@tonic-gate 		} else {
2427c478bd9Sstevel@tonic-gate 			/* else  already in tree */
2437c478bd9Sstevel@tonic-gate 			free(s);
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 	free(types);
2477c478bd9Sstevel@tonic-gate 	return (utypes);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate /*
2517c478bd9Sstevel@tonic-gate  * This is used after all types have been collated into the tree.
2527c478bd9Sstevel@tonic-gate  * It walks through the tree, composing a list from all the types in
2537c478bd9Sstevel@tonic-gate  * the tree, and freeing each node of the tree as it goes.
2547c478bd9Sstevel@tonic-gate  * Returns the list, or NULL if the tree is empty.
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate /* the walk action function: */
2577c478bd9Sstevel@tonic-gate /*ARGSUSED*/
collect_types(void * node,VISIT order,int level,void * cookie)2587c478bd9Sstevel@tonic-gate static void collect_types(void *node, VISIT order, int level, void *cookie) {
2597c478bd9Sstevel@tonic-gate 	char **types = (char **)cookie;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (order == endorder || order == leaf) {
2627c478bd9Sstevel@tonic-gate 		char *t = *(char **)node;
2637c478bd9Sstevel@tonic-gate 		slp_add2list(t, types, SLP_FALSE);
2647c478bd9Sstevel@tonic-gate 		free(t);
2657c478bd9Sstevel@tonic-gate 		free(node);
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate /* the walk driver: */
build_types_list(void * collator)2707c478bd9Sstevel@tonic-gate static char *build_types_list(void *collator) {
2717c478bd9Sstevel@tonic-gate 	char *types = NULL;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (!collator)
2747c478bd9Sstevel@tonic-gate 		return (NULL);
2757c478bd9Sstevel@tonic-gate 	slp_twalk(collator, collect_types, 0, (void *) &types);
2767c478bd9Sstevel@tonic-gate 	return (types);
2777c478bd9Sstevel@tonic-gate }
278