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/*
2N/A * Target Lists
2N/A * ============
2N/A * All UA functions use target lists to select and manage their
2N/A * network targets. There are two types of network targets: unicast (uc)
2N/A * and multicast (mc) -- multicast will also work for broadcast. This
2N/A * module organizes unicast targets into an efficient ordering. The
2N/A * targeting structure can be though of as a 2-dimensional matrix, with
2N/A * the following axes:
2N/A *
2N/A * unicast failovers --->
2N/A * targets
2N/A * |
2N/A * |
2N/A * \ /
2N/A *
2N/A * Callers walk down the unicast targets, unicasting to each. If any
2N/A * unicast target fails, callers then walk to the right, through failover
2N/A * targets until they either find one that works, or there are no more
2N/A * failover targets.
2N/A *
2N/A * The targeting heuristic orders the unicast targets so that those
2N/A * DAs which support the greatest number of requested scopes are called
2N/A * first, thus minimizing the number of unicasts which need to be done.
2N/A * Within groups of DAs supporting the same scope coverage, the DAs are
2N/A * sorted according to network proximity relative to the local host:
2N/A * DAs on the local host come first, then those on a same subnet, then
2N/A * all other (remote) DAs.
2N/A *
2N/A * A given DA is called no more than once, and failed DAs are skipped
2N/A * after they have been marked 'failed'.
2N/A *
2N/A * All access to a target list is done through the following functions
2N/A * and types:
2N/A * There are two opaque types:
2N/A * slp_target_list_t: A handle to a target list
2N/A * slp_target_t: A handle to an individual target. slp_get_target_sin
2N/A * will extract an inet address for this target.
2N/A *
2N/A * There are the following accessor functions:
2N/A * slp_new_target_list: creates a new target list for the given scopes,
2N/A * and populates with all known DAs for these scopes.
2N/A * slp_get_uc_scopes: returns a list of all scopes for which there are
2N/A * DAs (and which can thus be used for unicasts)
2N/A * slp_get_mc_scopes: returns a list of all scopes for which there are
2N/A * no DAs (and which must thus be used for multicasts).
2N/A * slp_next_uc_target: Returns a slp_target_t handle for the next unicast
2N/A * target, or NULL for none.
2N/A * slp_next_failover: Returns the next failover DA for a given target, or
2N/A * NULL for none.
2N/A * slp_get_target_sin: extracts a sockaddr_in for a given slp_target_t;
2N/A * slp_mark_target_used: callers should mark a slp_target_t used after
2N/A * successfully communicating with that target.
2N/A * slp_mark_target_failed: callers should mark a slp_target_t failed after
2N/A * trying and failing to communicate with a target.
2N/A * slp_destroy_target_list: destroys and frees a target list and all its
2N/A * associated resources.
2N/A * slp_fabricate_target: Creates a slp_target_t from a given sockaddr_in.
2N/A * This is useful for situations such as when a
2N/A * multicast routine needs to hand off to a TCP
2N/A * routine (due to overflow), and there is no target
2N/A * list available. Fabricated targets should be free'd
2N/A * with slp_free_target; the input sin will duplicated
2N/A * in the target, so the caller can free it after
2N/A * calling slp_fabricate_target.
2N/A * slp_free_target: Frees an slp_target_t created by slp_fabricate_target.
2N/A * This should not be used to free any other target.
2N/A *
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <syslog.h>
2N/A#include <arpa/inet.h>
2N/A#include <slp-internal.h>
2N/A#include <slp_net_utils.h>
2N/A
2N/Atypedef enum {
2N/A SLP_REMOTE_PROX = 0, /* remote to local host */
2N/A SLP_SUBNET_PROX = 1, /* on same subnet as local host */
2N/A SLP_LOCAL_PROX = 2 /* on local host */
2N/A} slp_net_prox;
2N/A
2N/Astruct da_node {
2N/A struct sockaddr_in sin;
2N/A char *scopes;
2N/A SLPBoolean used, failed;
2N/A int coverage;
2N/A slp_net_prox proximity;
2N/A struct da_node *next, *prev;
2N/A};
2N/A
2N/Astruct scope_targets {
2N/A struct da_node *da;
2N/A struct scope_targets *next;
2N/A};
2N/A
2N/Astruct target_list {
2N/A struct scope_targets **scopes;
2N/A struct scope_targets **state;
2N/A char *uc_scopes;
2N/A char *mc_scopes;
2N/A char *all_scopes;
2N/A struct da_node *DAs;
2N/A};
2N/A
2N/Astatic void add2scopes_list(struct da_node *, struct target_list *);
2N/Astatic void add_da_entry(struct da_node **, struct sockaddr_in *,
2N/A char *, slp_net_prox, int);
2N/Astatic SLPSrvURLCallback collect_DAs;
2N/Astatic void format_query(char *, const char *);
2N/A
2N/ASLPError slp_new_target_list(slp_handle_impl_t *hp, const char *scopes,
2N/A slp_target_list_t **handle) {
2N/A struct target_list *tl;
2N/A int scope_cnt;
2N/A char *p;
2N/A struct da_node *te;
2N/A char *query, *reply;
2N/A SLPError err;
2N/A void *collator = NULL;
2N/A
2N/A /* count the number of scopes in the list */
2N/A scope_cnt = 0;
2N/A for (p = (char *)scopes; p; p++) {
2N/A p = slp_utf_strchr(p, ',');
2N/A scope_cnt++;
2N/A if (!p)
2N/A break;
2N/A }
2N/A
2N/A /* create a new target list */
2N/A if (!(tl = calloc(1, sizeof (*tl)))) {
2N/A slp_err(LOG_CRIT, 0, "slp_new_target_list", "out of memory");
2N/A return (SLP_MEMORY_ALLOC_FAILED);
2N/A }
2N/A tl->DAs = NULL;
2N/A
2N/A if (!(tl->scopes = calloc(scope_cnt + 1, sizeof (*(tl->scopes))))) {
2N/A slp_err(LOG_CRIT, 0, "slp_new_target_list", "out of memory");
2N/A free(tl);
2N/A return (SLP_MEMORY_ALLOC_FAILED);
2N/A }
2N/A tl->uc_scopes = NULL;
2N/A tl->state = tl->scopes;
2N/A if (!(tl->all_scopes = strdup(scopes))) {
2N/A slp_err(LOG_CRIT, 0, "slp_new_target_list", "out of memory");
2N/A free(tl->scopes); free(tl);
2N/A return (SLP_MEMORY_ALLOC_FAILED);
2N/A }
2N/A /* As scopes are added to uc list, they are removed from the mc list */
2N/A if (!(tl->mc_scopes = strdup(scopes))) {
2N/A slp_err(LOG_CRIT, 0, "slp_new_target_list", "out of memory");
2N/A free(tl->scopes); free(tl->all_scopes); free(tl);
2N/A return (SLP_MEMORY_ALLOC_FAILED);
2N/A }
2N/A
2N/A if (hp->force_multicast) {
2N/A /* all scopes remain multicast scopes; useful for SAAdverts */
2N/A *handle = tl;
2N/A return (SLP_OK);
2N/A }
2N/A
2N/A /* DAs from active and passive discovery */
2N/A if (!(query = malloc(strlen(scopes) -
2N/A (scope_cnt - 1) + /* exclude commas */
2N/A strlen(SLP_SUN_VERSION_TAG) +
2N/A strlen("(&(=2)(|))") + 1 +
2N/A (scope_cnt *
2N/A (strlen(SLP_SUN_SCOPES_TAG) +
2N/A strlen("(=)")))))) { /* (scopes=) */
2N/A slp_err(LOG_CRIT, 0, "slp_new_target_list", "out of memory");
2N/A free(tl->scopes);
2N/A free(tl->all_scopes);
2N/A free(tl->mc_scopes);
2N/A free(tl);
2N/A return (SLP_MEMORY_ALLOC_FAILED);
2N/A }
2N/A format_query(query, scopes);
2N/A
2N/A if ((err = slp_find_das(query, &reply)) != SLP_OK &&
2N/A err != SLP_NETWORK_ERROR) {
2N/A free(tl->scopes);
2N/A free(tl->all_scopes);
2N/A free(tl->mc_scopes);
2N/A free(tl);
2N/A free(query);
2N/A return (err);
2N/A }
2N/A free(query);
2N/A
2N/A /* Unpack the reply */
2N/A if (reply) {
2N/A int numResults = 0; /* placeholder; not actually used */
2N/A /* tag call as internal */
2N/A hp->internal_call = SLP_TRUE;
2N/A
2N/A (void) slp_unpackSrvReply(hp, reply, collect_DAs,
2N/A tl, &collator, &numResults);
2N/A free(reply);
2N/A /* invoke last call */
2N/A (void) slp_unpackSrvReply(hp, NULL, collect_DAs,
2N/A tl, &collator, &numResults);
2N/A
2N/A /* revert internal call tag */
2N/A hp->internal_call = SLP_FALSE;
2N/A }
2N/A
2N/A /*
2N/A * tl->DAs now points to a list of DAs sorted by the number of
2N/A * relevant scopes they serve. Using this ordering, populate the
2N/A * scope array lists.
2N/A */
2N/A for (te = tl->DAs; te; te = te->next)
2N/A add2scopes_list(te, tl);
2N/A
2N/A *handle = tl;
2N/A return (SLP_OK);
2N/A}
2N/A
2N/Aconst char *slp_get_uc_scopes(slp_target_list_t *h) {
2N/A struct target_list *tl = (struct target_list *)h;
2N/A return (tl->uc_scopes);
2N/A}
2N/A
2N/Aconst char *slp_get_mc_scopes(slp_target_list_t *h) {
2N/A struct target_list *tl = (struct target_list *)h;
2N/A return (tl->mc_scopes);
2N/A}
2N/A
2N/Aslp_target_t *slp_next_uc_target(slp_target_list_t *h) {
2N/A struct scope_targets *p;
2N/A struct target_list *tl = (struct target_list *)h;
2N/A
2N/A if (!(*tl->state))
2N/A return (NULL);
2N/A /* find the next unused target */
2N/A for (; *tl->state; tl->state++) {
2N/A if (!(*tl->state)->da->used && !(*tl->state)->da->failed)
2N/A return (*tl->state++);
2N/A if ((*tl->state)->da->failed) {
2N/A /* get next failover */
2N/A if (p = slp_next_failover(*tl->state)) {
2N/A tl->state++;
2N/A return (p);
2N/A }
2N/A /* else nothing more we can do */
2N/A }
2N/A }
2N/A return (NULL);
2N/A}
2N/A
2N/Aslp_target_t *slp_next_failover(slp_target_t *h) {
2N/A struct scope_targets *p = (struct scope_targets *)h;
2N/A for (p = p->next; p; p = p->next) {
2N/A if (p->da->used)
2N/A return (NULL); /* already did this scope */
2N/A if (!p->da->used && !p->da->failed)
2N/A return (p);
2N/A }
2N/A return (NULL);
2N/A}
2N/A
2N/Avoid *slp_get_target_sin(slp_target_t *h) {
2N/A struct scope_targets *p = (struct scope_targets *)h;
2N/A return (void *)(p ? &(p->da->sin) : NULL);
2N/A}
2N/A
2N/Avoid slp_mark_target_used(slp_target_t *h) {
2N/A struct scope_targets *p = (struct scope_targets *)h;
2N/A p->da->used = SLP_TRUE;
2N/A}
2N/A
2N/Avoid slp_mark_target_failed(slp_target_t *h) {
2N/A struct scope_targets *p = (struct scope_targets *)h;
2N/A p->da->failed = SLP_TRUE;
2N/A}
2N/A
2N/Aslp_target_t *slp_fabricate_target(void *s) {
2N/A struct da_node *dn;
2N/A struct scope_targets *st;
2N/A struct sockaddr_in *sin = (struct sockaddr_in *)s;
2N/A
2N/A if (!(st = malloc(sizeof (*st)))) {
2N/A slp_err(LOG_CRIT, 0, "slp_fabricate_target", "out of memory");
2N/A return (NULL);
2N/A }
2N/A if (!(dn = malloc(sizeof (*dn)))) {
2N/A free(st);
2N/A slp_err(LOG_CRIT, 0, "slp_fabricate_target", "out of memory");
2N/A return (NULL);
2N/A }
2N/A (void) memcpy(&(dn->sin), sin, sizeof (dn->sin));
2N/A dn->used = dn->failed = SLP_FALSE;
2N/A dn->coverage = 0;
2N/A dn->proximity = SLP_REMOTE_PROX;
2N/A dn->next = dn->prev = NULL;
2N/A
2N/A st->da = dn;
2N/A st->next = NULL;
2N/A
2N/A return (st);
2N/A}
2N/A
2N/Avoid slp_free_target(slp_target_t *target) {
2N/A struct scope_targets *t = (struct scope_targets *)target;
2N/A if (!t)
2N/A return;
2N/A free(t->da);
2N/A free(t);
2N/A}
2N/A
2N/Avoid slp_destroy_target_list(slp_target_list_t *h) {
2N/A struct da_node *das, *dap;
2N/A int i;
2N/A struct target_list *tl = (struct target_list *)h;
2N/A
2N/A /* free da node list */
2N/A for (das = tl->DAs; das; das = dap) {
2N/A dap = das->next;
2N/A free(das->scopes);
2N/A free(das);
2N/A }
2N/A
2N/A /* free scope target linked lists */
2N/A for (i = 0; tl->scopes[i]; i++) {
2N/A struct scope_targets *sts, *stp;
2N/A for (sts = tl->scopes[i]; sts; sts = stp) {
2N/A stp = sts->next;
2N/A free(sts);
2N/A }
2N/A }
2N/A
2N/A /* free scope array */
2N/A free(tl->scopes);
2N/A
2N/A /* free any char * lists in use */
2N/A if (tl->uc_scopes)
2N/A free(tl->uc_scopes);
2N/A if (tl->mc_scopes)
2N/A free(tl->mc_scopes);
2N/A free(tl->all_scopes);
2N/A
2N/A /* free the target list struct */
2N/A free(tl);
2N/A}
2N/A
2N/Astatic void add2scopes_list(struct da_node *te, struct target_list *tl) {
2N/A struct scope_targets **scopes = tl->scopes;
2N/A char *p, *s;
2N/A int i;
2N/A
2N/A /*
2N/A * for each scope in tl->uc_scopes:
2N/A * add this DA if it serves the scope.
2N/A */
2N/A i = 0;
2N/A for (s = tl->uc_scopes; s; s = p) {
2N/A p = slp_utf_strchr(s, ',');
2N/A if (p)
2N/A *p = 0;
2N/A if (slp_onlist(s, te->scopes)) {
2N/A struct scope_targets *st, *stp;
2N/A /* add this DA node to this scope's target list */
2N/A if (!(st = malloc(sizeof (*st)))) {
2N/A slp_err(LOG_CRIT, 0, "add2scopes_list",
2N/A "out of memory");
2N/A return;
2N/A }
2N/A st->da = te;
2N/A st->next = NULL;
2N/A /* find the end of the target list */
2N/A for (stp = scopes[i]; stp && stp->next; ) {
2N/A stp = stp->next;
2N/A }
2N/A if (stp)
2N/A stp->next = st;
2N/A else
2N/A scopes[i] = st;
2N/A }
2N/A if (p)
2N/A *p++ = ',';
2N/A i++;
2N/A }
2N/A}
2N/A
2N/Astatic void add_da_entry(struct da_node **tel, struct sockaddr_in *sin,
2N/A char *scopes, slp_net_prox proximity, int c) {
2N/A struct da_node *te, *p;
2N/A
2N/A if (!(te = malloc(sizeof (*te)))) {
2N/A slp_err(LOG_CRIT, 0, "add_da_entry", "out of memory");
2N/A return;
2N/A }
2N/A te->scopes = scopes;
2N/A te->coverage = c;
2N/A te->proximity = proximity;
2N/A (void) memcpy(&(te->sin), sin, sizeof (te->sin));
2N/A te->used = SLP_FALSE;
2N/A te->failed = SLP_FALSE;
2N/A te->prev = NULL;
2N/A te->next = NULL;
2N/A
2N/A /* find its place in the list */
2N/A if (!(*tel)) {
2N/A *tel = te;
2N/A return;
2N/A }
2N/A for (p = *tel; p; p = p->next)
2N/A if (c >= p->coverage) {
2N/A /* found a coverage grouping; now sort by proximity */
2N/A for (; p && proximity < p->proximity; )
2N/A p = p->next;
2N/A
2N/A if (!p) {
2N/A break;
2N/A }
2N/A
2N/A /* add it here */
2N/A te->next = p;
2N/A te->prev = p->prev;
2N/A if (p->prev)
2N/A p->prev->next = te;
2N/A else
2N/A /* we're at the head */
2N/A (*tel) = te;
2N/A p->prev = te;
2N/A return;
2N/A }
2N/A
2N/A /* didn't find a place in the list, so add it at the end */
2N/A for (p = *tel; p->next; )
2N/A p = p->next;
2N/A
2N/A p->next = te;
2N/A te->prev = p;
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic SLPBoolean collect_DAs(SLPHandle h, const char *u,
2N/A unsigned short lifetime,
2N/A SLPError errCode, void *cookie) {
2N/A SLPSrvURL *surl = NULL;
2N/A char *s, *p, *sscopes, *sscopes_end, *url;
2N/A int coverage, proximity;
2N/A struct sockaddr_in sin[1];
2N/A struct target_list *tl = (struct target_list *)cookie;
2N/A
2N/A if (errCode != SLP_OK)
2N/A return (SLP_TRUE);
2N/A
2N/A /* dup url so as not to corrupt da cache */
2N/A if (!(url = strdup(u))) {
2N/A slp_err(LOG_CRIT, 0, "collect_DAs", "out of memory");
2N/A return (SLP_FALSE);
2N/A }
2N/A
2N/A /* parse url into a SLPSrvURL struct */
2N/A if (SLPParseSrvURL(url, &surl) != SLP_OK) {
2N/A return (SLP_TRUE); /* bad URL; skip it */
2N/A }
2N/A
2N/A /* determine proximity */
2N/A if (slp_surl2sin(surl, sin) != SLP_OK) {
2N/A goto cleanup;
2N/A }
2N/A if (slp_on_localhost(h, sin->sin_addr)) {
2N/A proximity = SLP_LOCAL_PROX;
2N/A } else if (slp_on_subnet(h, sin->sin_addr)) {
2N/A proximity = SLP_SUBNET_PROX;
2N/A } else {
2N/A proximity = SLP_REMOTE_PROX;
2N/A }
2N/A
2N/A /*
2N/A * sort the DAs into the entry list, ranked by the number of
2N/A * relevant scopes they serve (coverage).
2N/A */
2N/A coverage = 0;
2N/A if (!(sscopes = slp_utf_strchr(surl->s_pcSrvPart, '='))) {
2N/A /* URL part should be of the form 'scopes=...' */
2N/A goto cleanup;
2N/A }
2N/A sscopes++;
2N/A
2N/A /* cut off host scope at end */
2N/A if (sscopes_end = slp_utf_strchr(sscopes, '=')) {
2N/A /* skip the =[hostname] at the end */
2N/A *sscopes_end = 0;
2N/A }
2N/A
2N/A /* copy out the scopes part, since url will be freed after this call */
2N/A if (!(sscopes = strdup(sscopes))) {
2N/A slp_err(LOG_CRIT, 0, "collect_DAs", "out of memory");
2N/A free(surl);
2N/A return (SLP_FALSE);
2N/A }
2N/A
2N/A for (s = tl->all_scopes; s; s = p) {
2N/A p = slp_utf_strchr(s, ',');
2N/A if (p)
2N/A *p = 0;
2N/A if (slp_onlist(s, sscopes)) {
2N/A /* add to uc list; remove from mc list */
2N/A slp_add2list(s, &(tl->uc_scopes), SLP_TRUE);
2N/A slp_list_subtract(s, &(tl->mc_scopes));
2N/A coverage++;
2N/A }
2N/A if (p)
2N/A *p++ = ',';
2N/A }
2N/A if (coverage)
2N/A add_da_entry(&(tl->DAs), sin, sscopes, proximity, coverage);
2N/A
2N/Acleanup:
2N/A free(url);
2N/A if (surl) free(surl);
2N/A
2N/A return (SLP_TRUE);
2N/A}
2N/A
2N/A/*
2N/A * Takes a scopes list of the form 's1,s2,s3,...' and formats it into
2N/A * an LDAP search filter of the form '(|(SCOPETAG=s1)(SCOPETAG=s2)...)'.
2N/A * 'scopes' contains the scopes list; 'q' is a buffer allocated
2N/A * by the caller into which the result will be placed.
2N/A */
2N/Astatic void format_query(char *q, const char *scopes) {
2N/A char *p, *s;
2N/A int more_than_one = slp_utf_strchr(scopes, ',') ? 1 : 0;
2N/A
2N/A *q++ = '('; *q++ = '&';
2N/A if (more_than_one) {
2N/A *q++ = '('; *q++ = '|';
2N/A }
2N/A
2N/A for (p = s = (char *)scopes; p; s = p) {
2N/A *q++ = '(';
2N/A (void) strcpy(q, SLP_SUN_SCOPES_TAG);
2N/A q += strlen(SLP_SUN_SCOPES_TAG);
2N/A *q++ = '=';
2N/A
2N/A p = slp_utf_strchr(s, ',');
2N/A if (p) {
2N/A (void) memcpy(q, s, p - s);
2N/A q += (p - s);
2N/A p++;
2N/A } else {
2N/A (void) strcpy(q, s);
2N/A q += strlen(s);
2N/A }
2N/A *q++ = ')';
2N/A }
2N/A
2N/A if (more_than_one) {
2N/A *q++ = ')';
2N/A }
2N/A *q++ = '(';
2N/A (void) strcpy(q, SLP_SUN_VERSION_TAG);
2N/A q += strlen(SLP_SUN_VERSION_TAG);
2N/A *q++ = '=';
2N/A *q++ = '2';
2N/A *q++ = ')';
2N/A *q++ = ')';
2N/A *q = 0;
2N/A}