2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <syslog.h>
2N/A#include <slp-internal.h>
2N/A
2N/Astruct surl_node {
2N/A char *surl;
2N/A unsigned short lifetime;
2N/A};
2N/A
2N/Astruct caller_bundle {
2N/A SLPSrvURLCallback *cb;
2N/A void *cookie;
2N/A SLPHandle handle;
2N/A};
2N/A
2N/Astatic int compare_surls(struct surl_node *, struct surl_node *);
2N/Astatic char *collate_surls(char *, unsigned short, void **);
2N/Astatic void traverse_surls(SLPHandle, SLPSrvURLCallback, void *, void *);
2N/Astatic void process_surl_node(void *, VISIT, int, void *);
2N/Astatic SLPBoolean unpackDAAdvert_srv(slp_handle_impl_t *, char *,
2N/A SLPSrvURLCallback, void *,
2N/A void **, int *);
2N/Astatic SLPBoolean unpackSAAdvert_srv(slp_handle_impl_t *, char *,
2N/A SLPSrvURLCallback, void *,
2N/A void **, int *);
2N/A
2N/ASLPError SLPFindSrvs(SLPHandle hSLP, const char *pcServiceType,
2N/A const char *pcScope, const char *pcSearchFilter,
2N/A SLPSrvURLCallback callback, void *pvUser) {
2N/A SLPError err;
2N/A slp_handle_impl_t *hp = (slp_handle_impl_t *)hSLP;
2N/A int wantSAAdvert =
2N/A strcasecmp(pcServiceType, "service:service-agent") == 0;
2N/A int wantDAAdvert =
2N/A strcasecmp(pcServiceType, "service:directory-agent") == 0;
2N/A int isSpecial = wantSAAdvert || wantDAAdvert;
2N/A SLPMsgReplyCB *unpack_cb;
2N/A
2N/A if (!hSLP || !pcServiceType || !pcScope || (!*pcScope && !isSpecial) ||
2N/A !pcSearchFilter || !callback) {
2N/A return (SLP_PARAMETER_BAD);
2N/A }
2N/A
2N/A if ((strlen(pcServiceType) > SLP_MAX_STRINGLEN) ||
2N/A (strlen(pcScope) > SLP_MAX_STRINGLEN) ||
2N/A (strlen(pcSearchFilter) > SLP_MAX_STRINGLEN)) {
2N/A return (SLP_PARAMETER_BAD);
2N/A }
2N/A
2N/A if ((err = slp_start_call(hSLP)) != SLP_OK)
2N/A return (err);
2N/A
2N/A /* Special unpacker for DA and SA solicitations */
2N/A if (wantDAAdvert) {
2N/A unpack_cb = (SLPMsgReplyCB *)unpackDAAdvert_srv;
2N/A hp->force_multicast = SLP_TRUE;
2N/A } else if (wantSAAdvert) {
2N/A unpack_cb = (SLPMsgReplyCB *)unpackSAAdvert_srv;
2N/A hp->force_multicast = SLP_TRUE;
2N/A } else {
2N/A /* normal service request */
2N/A unpack_cb = (SLPMsgReplyCB *)slp_unpackSrvReply;
2N/A }
2N/A
2N/A err = slp_packSrvRqst(pcServiceType, pcSearchFilter, hp);
2N/A
2N/A if (err == SLP_OK)
2N/A err = slp_ua_common(hSLP, pcScope,
2N/A (SLPGenericAppCB *) callback, pvUser,
2N/A unpack_cb);
2N/A if (err != SLP_OK)
2N/A slp_end_call(hSLP);
2N/A
2N/A return (err);
2N/A}
2N/A
2N/ASLPBoolean slp_unpackSrvReply(slp_handle_impl_t *hp, char *reply,
2N/A SLPSrvURLCallback cb, void *cookie,
2N/A void **collator, int *numResults) {
2N/A SLPError errCode;
2N/A unsigned short urlCount, protoErrCode;
2N/A size_t len, off;
2N/A int i;
2N/A int maxResults = slp_get_maxResults();
2N/A SLPBoolean cont = SLP_TRUE;
2N/A
2N/A if (!reply) {
2N/A /* no more results */
2N/A /* traverse_surls:invoke cb for sync case,and free resources */
2N/A if (!hp->async) {
2N/A traverse_surls(hp, cb, cookie, *collator);
2N/A }
2N/A cb(hp, NULL, 0, SLP_LAST_CALL, cookie);
2N/A return (SLP_FALSE);
2N/A }
2N/A
2N/A len = slp_get_length(reply);
2N/A off = SLP_HDRLEN + slp_get_langlen(reply);
2N/A /* err code */
2N/A if (slp_get_sht(reply, len, &off, &protoErrCode) != SLP_OK)
2N/A return (SLP_TRUE);
2N/A /* internal errors should have been filtered out by the net code */
2N/A if ((errCode = slp_map_err(protoErrCode)) != SLP_OK) {
2N/A return (cb(hp, NULL, 0, errCode, cookie));
2N/A }
2N/A
2N/A /* url entry count */
2N/A if (slp_get_sht(reply, len, &off, &urlCount) != SLP_OK)
2N/A return (SLP_TRUE);
2N/A
2N/A /* for each srvRply, unpack and pass to CB */
2N/A for (i = 0; i < urlCount && !hp->cancel; i++) {
2N/A char *pcSrvURL;
2N/A unsigned short sLifetime;
2N/A int nURLAuthBlocks;
2N/A size_t tbv_len;
2N/A char *url_tbv;
2N/A
2N/A /* parse URL entry into params */
2N/A off++; /* skip reserved byte */
2N/A /* lifetime */
2N/A if (slp_get_sht(reply, len, &off, &sLifetime) != SLP_OK)
2N/A return (SLP_TRUE);
2N/A /* URL itself; keep track of it in case we need to verify */
2N/A url_tbv = reply + off;
2N/A tbv_len = off;
2N/A if (slp_get_string(reply, len, &off, &pcSrvURL) != SLP_OK)
2N/A return (SLP_TRUE);
2N/A tbv_len = off - tbv_len;
2N/A
2N/A /* number of url auths */
2N/A if (slp_get_byte(reply, len, &off, &nURLAuthBlocks) != SLP_OK)
2N/A goto cleanup;
2N/A
2N/A /* get and verify auth blocks */
2N/A if ((!hp->internal_call && slp_get_security_on()) ||
2N/A nURLAuthBlocks > 0) {
2N/A struct iovec iov[1];
2N/A size_t abLen = 0;
2N/A
2N/A iov[0].iov_base = url_tbv;
2N/A iov[0].iov_len = tbv_len;
2N/A
2N/A if (slp_verify(iov, 1,
2N/A reply + off,
2N/A len - off,
2N/A nURLAuthBlocks,
2N/A &abLen) != SLP_OK) {
2N/A goto cleanup;
2N/A }
2N/A off += abLen;
2N/A }
2N/A
2N/A /* collate the srv urls for sync behavior */
2N/A if (!hp->async) {
2N/A pcSrvURL = collate_surls(pcSrvURL, sLifetime, collator);
2N/A
2N/A if (!pcSrvURL)
2N/A continue;
2N/A }
2N/A
2N/A (*numResults)++;
2N/A /* invoke cb */
2N/A if (hp->async)
2N/A cont = cb(
2N/A (SLPHandle) hp,
2N/A pcSrvURL,
2N/A sLifetime,
2N/A errCode,
2N/A cookie);
2N/A
2N/A /* cleanup */
2N/Acleanup:
2N/A free(pcSrvURL);
2N/A
2N/A /* check maxResults */
2N/A if (!hp->internal_call && *numResults == maxResults) {
2N/A cont = SLP_FALSE;
2N/A }
2N/A
2N/A if (!cont) break;
2N/A }
2N/A
2N/A return (cont);
2N/A}
2N/A
2N/A/*
2N/A * unpackDAAdvert_srv follows the same same logic flow as slp_unpackSrvReply
2N/A * with two differences: the message in reply is a DAAdvert, and
2N/A * this function is not used internally, so hp is never NULL. Although
2N/A * all info from a DAAdvert is returned by slp_unpackDAAdvert, here
2N/A * the recipient (the user-supplied SLPSrvURLCallback) is interested
2N/A * only in the DA service URL.
2N/A */
2N/Astatic SLPBoolean unpackDAAdvert_srv(slp_handle_impl_t *hp, char *reply,
2N/A SLPSrvURLCallback cb, void *cookie,
2N/A void **collator, int *numResults) {
2N/A char *surl, *scopes, *attrs, *spis;
2N/A SLPBoolean cont = SLP_TRUE;
2N/A SLPError errCode;
2N/A int maxResults = slp_get_maxResults();
2N/A
2N/A if (!reply) {
2N/A /* no more results */
2N/A /* traverse_surls:invoke cb for sync case,and free resources */
2N/A if (!hp->async) {
2N/A traverse_surls(hp, cb, cookie, *collator);
2N/A }
2N/A cb(hp, NULL, 0, SLP_LAST_CALL, cookie);
2N/A return (SLP_FALSE);
2N/A }
2N/A
2N/A if (slp_unpackDAAdvert(reply, &surl, &scopes, &attrs, &spis, &errCode)
2N/A != SLP_OK) {
2N/A return (SLP_TRUE);
2N/A }
2N/A if (errCode != SLP_OK) {
2N/A return (cb(hp, NULL, 0, errCode, cookie));
2N/A }
2N/A
2N/A /* collate the urls */
2N/A surl = collate_surls(surl, 0, collator);
2N/A if (!surl) {
2N/A return (SLP_TRUE);
2N/A }
2N/A
2N/A (*numResults)++;
2N/A if (hp->async) {
2N/A cont = cb((SLPHandle)hp, surl, 0, errCode, cookie);
2N/A }
2N/A
2N/A /* cleanup */
2N/A free(surl);
2N/A free(scopes);
2N/A free(attrs);
2N/A free(spis);
2N/A
2N/A /* check maxResults */
2N/A if (!hp->internal_call && *numResults == maxResults) {
2N/A return (SLP_FALSE);
2N/A }
2N/A
2N/A return (cont);
2N/A}
2N/A/*
2N/A * unpackSAAdvert_srv follows the same same logic flow as slp_unpackSrvReply
2N/A * with two differences: the message in reply is a SAAdvert, and
2N/A * this function is not used internally, so hp is never NULL. Although
2N/A * all info from an SAAdvert is returned by slp_unpackSAAdvert, here
2N/A * the recipient (the user-supplied SLPSrvURLCallback) is interested
2N/A * only in the SA service URL.
2N/A */
2N/Astatic SLPBoolean unpackSAAdvert_srv(slp_handle_impl_t *hp, char *reply,
2N/A SLPSrvURLCallback cb, void *cookie,
2N/A void **collator, int *numResults) {
2N/A char *surl, *scopes, *attrs;
2N/A SLPBoolean cont = SLP_TRUE;
2N/A int maxResults = slp_get_maxResults();
2N/A
2N/A if (!reply) {
2N/A /* no more results */
2N/A /* traverse_surls:invoke cb for sync case,and free resources */
2N/A if (!hp->async) {
2N/A /* sync case */
2N/A traverse_surls(hp, cb, cookie, *collator);
2N/A }
2N/A cb(hp, NULL, 0, SLP_LAST_CALL, cookie);
2N/A return (SLP_FALSE);
2N/A }
2N/A
2N/A if (slp_unpackSAAdvert(reply, &surl, &scopes, &attrs) != SLP_OK) {
2N/A return (SLP_TRUE);
2N/A }
2N/A
2N/A /* collate the urls */
2N/A surl = collate_surls(surl, 0, collator);
2N/A if (!surl) {
2N/A return (SLP_TRUE);
2N/A }
2N/A
2N/A (*numResults)++;
2N/A if (hp->async) {
2N/A cont = cb((SLPHandle)hp, surl, 0, SLP_OK, cookie);
2N/A }
2N/A
2N/A /* cleanup */
2N/A free(surl);
2N/A free(scopes);
2N/A free(attrs);
2N/A
2N/A /* check maxResults */
2N/A if (!hp->internal_call && *numResults == maxResults) {
2N/A return (SLP_FALSE);
2N/A }
2N/A
2N/A return (cont);
2N/A}
2N/A
2N/ASLPError slp_packSrvRqst(const char *type,
2N/A const char *filter,
2N/A slp_handle_impl_t *hp) {
2N/A SLPError err;
2N/A size_t len, msgLen, tmplen;
2N/A slp_msg_t *msg = &(hp->msg);
2N/A char *spi = NULL;
2N/A
2N/A if (slp_get_security_on()) {
2N/A spi = (char *)SLPGetProperty(SLP_CONFIG_SPI);
2N/A }
2N/A
2N/A if (!spi || !*spi) {
2N/A spi = "";
2N/A }
2N/A
2N/A /*
2N/A * Allocate iovec for the messge. A SrvRqst is layed out thus:
2N/A * 0: header
2N/A * 1: prlist length
2N/A * 2: prlist (filled in later by networking code)
2N/A * 3: service type string
2N/A * 4: scopes length
2N/A * 5: scopes (filled in later by networking code)
2N/A * 6: predicate string and SPI string
2N/A */
2N/A if (!(msg->iov = calloc(7, sizeof (*(msg->iov))))) {
2N/A slp_err(LOG_CRIT, 0, "slp_packSrvRqst", "out of memory");
2N/A return (SLP_MEMORY_ALLOC_FAILED);
2N/A }
2N/A msg->iovlen = 7;
2N/A
2N/A /* calculate msg length */
2N/A msgLen = 2 + /* prlist length */
2N/A 2 + strlen(type) + /* service type */
2N/A 2 + /* scope list length */
2N/A 2 + strlen(filter) + /* predicate string */
2N/A 2 + strlen(spi); /* SPI string */
2N/A
2N/A if (!(msg->msg = calloc(1, msgLen))) {
2N/A free(msg->iov);
2N/A slp_err(LOG_CRIT, 0, "slp_packSrvRqst", "out of memory");
2N/A return (SLP_MEMORY_ALLOC_FAILED);
2N/A }
2N/A
2N/A /* set pointer to PR list and scope list length spaces */
2N/A msg->prlistlen.iov_base = msg->msg;
2N/A msg->prlistlen.iov_len = 2;
2N/A msg->iov[1].iov_base = msg->msg;
2N/A msg->iov[1].iov_len = 2;
2N/A
2N/A msg->scopeslen.iov_base = msg->msg + 2;
2N/A msg->scopeslen.iov_len = 2;
2N/A msg->iov[4].iov_base = msg->msg + 2;
2N/A msg->iov[4].iov_len = 2;
2N/A
2N/A /* set up the scopes and prlist pointers into iov */
2N/A msg->prlist = &(msg->iov[2]);
2N/A msg->scopes = &(msg->iov[5]);
2N/A
2N/A len = 4;
2N/A
2N/A /* Add type string */
2N/A msg->iov[3].iov_base = msg->msg + len;
2N/A tmplen = len;
2N/A
2N/A err = slp_add_string(msg->msg, msgLen, type, &len);
2N/A msg->iov[3].iov_len = len - tmplen;
2N/A
2N/A if (err != SLP_OK)
2N/A goto error;
2N/A
2N/A /* Add search filter */
2N/A msg->iov[6].iov_base = msg->msg + len;
2N/A tmplen = len;
2N/A
2N/A err = slp_add_string(msg->msg, msgLen, filter, &len);
2N/A if (err != SLP_OK)
2N/A goto error;
2N/A
2N/A err = slp_add_string(msg->msg, msgLen, spi, &len);
2N/A
2N/A msg->iov[6].iov_len = len - tmplen;
2N/A
2N/A hp->fid = SRVRQST;
2N/A
2N/A if (err == SLP_OK) {
2N/A return (err);
2N/A }
2N/A
2N/A /* else error */
2N/Aerror:
2N/A free(msg->iov);
2N/A free(msg->msg);
2N/A
2N/A return (err);
2N/A}
2N/A
2N/A/*
2N/A * Caller must free msg
2N/A */
2N/ASLPError slp_packSrvRqst_single(const char *type,
2N/A const char *scopes,
2N/A const char *filter,
2N/A char **msg,
2N/A const char *lang) {
2N/A SLPError err;
2N/A size_t len, msgLen;
2N/A
2N/A msgLen =
2N/A SLP_HDRLEN + strlen(lang) + 2 +
2N/A 2 + strlen(type) +
2N/A 2 + strlen(scopes) +
2N/A 2 + strlen(filter) +
2N/A 2; /* No SPI string for internal calls */
2N/A
2N/A if (!(*msg = calloc(msgLen, 1))) {
2N/A slp_err(LOG_CRIT, 0, "slp_packSrvRqst_single",
2N/A "out of memory");
2N/A return (SLP_MEMORY_ALLOC_FAILED);
2N/A }
2N/A
2N/A len = 0;
2N/A err = slp_add_header(lang, *msg, msgLen, SRVRQST, msgLen, &len);
2N/A
2N/A len += 2; /* empty PR list */
2N/A
2N/A if (err == SLP_OK)
2N/A err = slp_add_string(*msg, msgLen, type, &len);
2N/A if (err == SLP_OK)
2N/A err = slp_add_string(*msg, msgLen, scopes, &len);
2N/A if (err == SLP_OK)
2N/A err = slp_add_string(*msg, msgLen, filter, &len);
2N/A if (err == SLP_OK)
2N/A /* empty SPI string */
2N/A err = slp_add_string(*msg, msgLen, "", &len);
2N/A
2N/A return (err);
2N/A}
2N/A
2N/A
2N/Astatic int compare_surls(struct surl_node *s1, struct surl_node *s2) {
2N/A if (s1->lifetime != s2->lifetime)
2N/A return (s1->lifetime - s2->lifetime);
2N/A return (slp_strcasecmp(s1->surl, s2->surl));
2N/A}
2N/A
2N/A/*
2N/A * Using the collator, determine if this URL has already been processed.
2N/A * If so, free surl and return NULL, else return the URL.
2N/A */
2N/Astatic char *collate_surls(char *surl, unsigned short life, void **collator) {
2N/A struct surl_node *n, **res;
2N/A
2N/A if (!(n = malloc(sizeof (*n)))) {
2N/A slp_err(LOG_CRIT, 0, "collate_surls", "out of memory");
2N/A return (NULL);
2N/A }
2N/A if (!(n->surl = strdup(surl))) {
2N/A free(n);
2N/A slp_err(LOG_CRIT, 0, "collate_surls", "out of memory");
2N/A return (NULL);
2N/A }
2N/A n->lifetime = life;
2N/A res = slp_tsearch((void *) n, collator,
2N/A (int (*)(const void *, const void *)) compare_surls);
2N/A if (*res == n) {
2N/A /* first time we've encountered this url */
2N/A return (surl);
2N/A }
2N/A /* else already in tree */
2N/A free(n->surl);
2N/A free(n);
2N/A free(surl);
2N/A return (NULL);
2N/A}
2N/A
2N/Astatic void traverse_surls(SLPHandle h, SLPSrvURLCallback cb,
2N/A void *cookie, void *collator) {
2N/A struct caller_bundle caller[1];
2N/A
2N/A if (!collator)
2N/A return;
2N/A caller->cb = cb;
2N/A caller->cookie = cookie;
2N/A caller->handle = h;
2N/A slp_twalk(collator, process_surl_node, 0, caller);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic void process_surl_node(void *node, VISIT order, int level, void *c) {
2N/A struct surl_node *n;
2N/A SLPSrvURLCallback *cb;
2N/A slp_handle_impl_t *h;
2N/A struct caller_bundle *caller = (struct caller_bundle *)c;
2N/A
2N/A if (order == endorder || order == leaf) {
2N/A SLPBoolean cont = SLP_TRUE;
2N/A
2N/A cb = caller->cb;
2N/A h = (slp_handle_impl_t *)caller->handle;
2N/A n = *(struct surl_node **)node;
2N/A /* invoke cb */
2N/A if (cont && (!h || !h->async))
2N/A cont = cb(
2N/A h, n->surl,
2N/A n->lifetime,
2N/A SLP_OK,
2N/A caller->cookie);
2N/A
2N/A free(n->surl);
2N/A free(n);
2N/A free(node);
2N/A }
2N/A}