1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate * Target Lists
29*7c478bd9Sstevel@tonic-gate * ============
30*7c478bd9Sstevel@tonic-gate * All UA functions use target lists to select and manage their
31*7c478bd9Sstevel@tonic-gate * network targets. There are two types of network targets: unicast (uc)
32*7c478bd9Sstevel@tonic-gate * and multicast (mc) -- multicast will also work for broadcast. This
33*7c478bd9Sstevel@tonic-gate * module organizes unicast targets into an efficient ordering. The
34*7c478bd9Sstevel@tonic-gate * targeting structure can be though of as a 2-dimensional matrix, with
35*7c478bd9Sstevel@tonic-gate * the following axes:
36*7c478bd9Sstevel@tonic-gate *
37*7c478bd9Sstevel@tonic-gate * unicast failovers --->
38*7c478bd9Sstevel@tonic-gate * targets
39*7c478bd9Sstevel@tonic-gate * |
40*7c478bd9Sstevel@tonic-gate * |
41*7c478bd9Sstevel@tonic-gate * \ /
42*7c478bd9Sstevel@tonic-gate *
43*7c478bd9Sstevel@tonic-gate * Callers walk down the unicast targets, unicasting to each. If any
44*7c478bd9Sstevel@tonic-gate * unicast target fails, callers then walk to the right, through failover
45*7c478bd9Sstevel@tonic-gate * targets until they either find one that works, or there are no more
46*7c478bd9Sstevel@tonic-gate * failover targets.
47*7c478bd9Sstevel@tonic-gate *
48*7c478bd9Sstevel@tonic-gate * The targeting heuristic orders the unicast targets so that those
49*7c478bd9Sstevel@tonic-gate * DAs which support the greatest number of requested scopes are called
50*7c478bd9Sstevel@tonic-gate * first, thus minimizing the number of unicasts which need to be done.
51*7c478bd9Sstevel@tonic-gate * Within groups of DAs supporting the same scope coverage, the DAs are
52*7c478bd9Sstevel@tonic-gate * sorted according to network proximity relative to the local host:
53*7c478bd9Sstevel@tonic-gate * DAs on the local host come first, then those on a same subnet, then
54*7c478bd9Sstevel@tonic-gate * all other (remote) DAs.
55*7c478bd9Sstevel@tonic-gate *
56*7c478bd9Sstevel@tonic-gate * A given DA is called no more than once, and failed DAs are skipped
57*7c478bd9Sstevel@tonic-gate * after they have been marked 'failed'.
58*7c478bd9Sstevel@tonic-gate *
59*7c478bd9Sstevel@tonic-gate * All access to a target list is done through the following functions
60*7c478bd9Sstevel@tonic-gate * and types:
61*7c478bd9Sstevel@tonic-gate * There are two opaque types:
62*7c478bd9Sstevel@tonic-gate * slp_target_list_t: A handle to a target list
63*7c478bd9Sstevel@tonic-gate * slp_target_t: A handle to an individual target. slp_get_target_sin
64*7c478bd9Sstevel@tonic-gate * will extract an inet address for this target.
65*7c478bd9Sstevel@tonic-gate *
66*7c478bd9Sstevel@tonic-gate * There are the following accessor functions:
67*7c478bd9Sstevel@tonic-gate * slp_new_target_list: creates a new target list for the given scopes,
68*7c478bd9Sstevel@tonic-gate * and populates with all known DAs for these scopes.
69*7c478bd9Sstevel@tonic-gate * slp_get_uc_scopes: returns a list of all scopes for which there are
70*7c478bd9Sstevel@tonic-gate * DAs (and which can thus be used for unicasts)
71*7c478bd9Sstevel@tonic-gate * slp_get_mc_scopes: returns a list of all scopes for which there are
72*7c478bd9Sstevel@tonic-gate * no DAs (and which must thus be used for multicasts).
73*7c478bd9Sstevel@tonic-gate * slp_next_uc_target: Returns a slp_target_t handle for the next unicast
74*7c478bd9Sstevel@tonic-gate * target, or NULL for none.
75*7c478bd9Sstevel@tonic-gate * slp_next_failover: Returns the next failover DA for a given target, or
76*7c478bd9Sstevel@tonic-gate * NULL for none.
77*7c478bd9Sstevel@tonic-gate * slp_get_target_sin: extracts a sockaddr_in for a given slp_target_t;
78*7c478bd9Sstevel@tonic-gate * slp_mark_target_used: callers should mark a slp_target_t used after
79*7c478bd9Sstevel@tonic-gate * successfully communicating with that target.
80*7c478bd9Sstevel@tonic-gate * slp_mark_target_failed: callers should mark a slp_target_t failed after
81*7c478bd9Sstevel@tonic-gate * trying and failing to communicate with a target.
82*7c478bd9Sstevel@tonic-gate * slp_destroy_target_list: destroys and frees a target list and all its
83*7c478bd9Sstevel@tonic-gate * associated resources.
84*7c478bd9Sstevel@tonic-gate * slp_fabricate_target: Creates a slp_target_t from a given sockaddr_in.
85*7c478bd9Sstevel@tonic-gate * This is useful for situations such as when a
86*7c478bd9Sstevel@tonic-gate * multicast routine needs to hand off to a TCP
87*7c478bd9Sstevel@tonic-gate * routine (due to overflow), and there is no target
88*7c478bd9Sstevel@tonic-gate * list available. Fabricated targets should be free'd
89*7c478bd9Sstevel@tonic-gate * with slp_free_target; the input sin will duplicated
90*7c478bd9Sstevel@tonic-gate * in the target, so the caller can free it after
91*7c478bd9Sstevel@tonic-gate * calling slp_fabricate_target.
92*7c478bd9Sstevel@tonic-gate * slp_free_target: Frees an slp_target_t created by slp_fabricate_target.
93*7c478bd9Sstevel@tonic-gate * This should not be used to free any other target.
94*7c478bd9Sstevel@tonic-gate *
95*7c478bd9Sstevel@tonic-gate */
96*7c478bd9Sstevel@tonic-gate
97*7c478bd9Sstevel@tonic-gate #include <stdio.h>
98*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
99*7c478bd9Sstevel@tonic-gate #include <string.h>
100*7c478bd9Sstevel@tonic-gate #include <syslog.h>
101*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
102*7c478bd9Sstevel@tonic-gate #include <slp-internal.h>
103*7c478bd9Sstevel@tonic-gate #include <slp_net_utils.h>
104*7c478bd9Sstevel@tonic-gate
105*7c478bd9Sstevel@tonic-gate typedef enum {
106*7c478bd9Sstevel@tonic-gate SLP_REMOTE_PROX = 0, /* remote to local host */
107*7c478bd9Sstevel@tonic-gate SLP_SUBNET_PROX = 1, /* on same subnet as local host */
108*7c478bd9Sstevel@tonic-gate SLP_LOCAL_PROX = 2 /* on local host */
109*7c478bd9Sstevel@tonic-gate } slp_net_prox;
110*7c478bd9Sstevel@tonic-gate
111*7c478bd9Sstevel@tonic-gate struct da_node {
112*7c478bd9Sstevel@tonic-gate struct sockaddr_in sin;
113*7c478bd9Sstevel@tonic-gate char *scopes;
114*7c478bd9Sstevel@tonic-gate SLPBoolean used, failed;
115*7c478bd9Sstevel@tonic-gate int coverage;
116*7c478bd9Sstevel@tonic-gate slp_net_prox proximity;
117*7c478bd9Sstevel@tonic-gate struct da_node *next, *prev;
118*7c478bd9Sstevel@tonic-gate };
119*7c478bd9Sstevel@tonic-gate
120*7c478bd9Sstevel@tonic-gate struct scope_targets {
121*7c478bd9Sstevel@tonic-gate struct da_node *da;
122*7c478bd9Sstevel@tonic-gate struct scope_targets *next;
123*7c478bd9Sstevel@tonic-gate };
124*7c478bd9Sstevel@tonic-gate
125*7c478bd9Sstevel@tonic-gate struct target_list {
126*7c478bd9Sstevel@tonic-gate struct scope_targets **scopes;
127*7c478bd9Sstevel@tonic-gate struct scope_targets **state;
128*7c478bd9Sstevel@tonic-gate char *uc_scopes;
129*7c478bd9Sstevel@tonic-gate char *mc_scopes;
130*7c478bd9Sstevel@tonic-gate char *all_scopes;
131*7c478bd9Sstevel@tonic-gate struct da_node *DAs;
132*7c478bd9Sstevel@tonic-gate };
133*7c478bd9Sstevel@tonic-gate
134*7c478bd9Sstevel@tonic-gate static void add2scopes_list(struct da_node *, struct target_list *);
135*7c478bd9Sstevel@tonic-gate static void add_da_entry(struct da_node **, struct sockaddr_in *,
136*7c478bd9Sstevel@tonic-gate char *, slp_net_prox, int);
137*7c478bd9Sstevel@tonic-gate static SLPSrvURLCallback collect_DAs;
138*7c478bd9Sstevel@tonic-gate static void format_query(char *, const char *);
139*7c478bd9Sstevel@tonic-gate
slp_new_target_list(slp_handle_impl_t * hp,const char * scopes,slp_target_list_t ** handle)140*7c478bd9Sstevel@tonic-gate SLPError slp_new_target_list(slp_handle_impl_t *hp, const char *scopes,
141*7c478bd9Sstevel@tonic-gate slp_target_list_t **handle) {
142*7c478bd9Sstevel@tonic-gate struct target_list *tl;
143*7c478bd9Sstevel@tonic-gate int scope_cnt;
144*7c478bd9Sstevel@tonic-gate char *p;
145*7c478bd9Sstevel@tonic-gate struct da_node *te;
146*7c478bd9Sstevel@tonic-gate char *query, *reply;
147*7c478bd9Sstevel@tonic-gate SLPError err;
148*7c478bd9Sstevel@tonic-gate void *collator = NULL;
149*7c478bd9Sstevel@tonic-gate
150*7c478bd9Sstevel@tonic-gate /* count the number of scopes in the list */
151*7c478bd9Sstevel@tonic-gate scope_cnt = 0;
152*7c478bd9Sstevel@tonic-gate for (p = (char *)scopes; p; p++) {
153*7c478bd9Sstevel@tonic-gate p = slp_utf_strchr(p, ',');
154*7c478bd9Sstevel@tonic-gate scope_cnt++;
155*7c478bd9Sstevel@tonic-gate if (!p)
156*7c478bd9Sstevel@tonic-gate break;
157*7c478bd9Sstevel@tonic-gate }
158*7c478bd9Sstevel@tonic-gate
159*7c478bd9Sstevel@tonic-gate /* create a new target list */
160*7c478bd9Sstevel@tonic-gate if (!(tl = calloc(1, sizeof (*tl)))) {
161*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_new_target_list", "out of memory");
162*7c478bd9Sstevel@tonic-gate return (SLP_MEMORY_ALLOC_FAILED);
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate tl->DAs = NULL;
165*7c478bd9Sstevel@tonic-gate
166*7c478bd9Sstevel@tonic-gate if (!(tl->scopes = calloc(scope_cnt + 1, sizeof (*(tl->scopes))))) {
167*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_new_target_list", "out of memory");
168*7c478bd9Sstevel@tonic-gate free(tl);
169*7c478bd9Sstevel@tonic-gate return (SLP_MEMORY_ALLOC_FAILED);
170*7c478bd9Sstevel@tonic-gate }
171*7c478bd9Sstevel@tonic-gate tl->uc_scopes = NULL;
172*7c478bd9Sstevel@tonic-gate tl->state = tl->scopes;
173*7c478bd9Sstevel@tonic-gate if (!(tl->all_scopes = strdup(scopes))) {
174*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_new_target_list", "out of memory");
175*7c478bd9Sstevel@tonic-gate free(tl->scopes); free(tl);
176*7c478bd9Sstevel@tonic-gate return (SLP_MEMORY_ALLOC_FAILED);
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate /* As scopes are added to uc list, they are removed from the mc list */
179*7c478bd9Sstevel@tonic-gate if (!(tl->mc_scopes = strdup(scopes))) {
180*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_new_target_list", "out of memory");
181*7c478bd9Sstevel@tonic-gate free(tl->scopes); free(tl->all_scopes); free(tl);
182*7c478bd9Sstevel@tonic-gate return (SLP_MEMORY_ALLOC_FAILED);
183*7c478bd9Sstevel@tonic-gate }
184*7c478bd9Sstevel@tonic-gate
185*7c478bd9Sstevel@tonic-gate if (hp->force_multicast) {
186*7c478bd9Sstevel@tonic-gate /* all scopes remain multicast scopes; useful for SAAdverts */
187*7c478bd9Sstevel@tonic-gate *handle = tl;
188*7c478bd9Sstevel@tonic-gate return (SLP_OK);
189*7c478bd9Sstevel@tonic-gate }
190*7c478bd9Sstevel@tonic-gate
191*7c478bd9Sstevel@tonic-gate /* DAs from active and passive discovery */
192*7c478bd9Sstevel@tonic-gate if (!(query = malloc(strlen(scopes) -
193*7c478bd9Sstevel@tonic-gate (scope_cnt - 1) + /* exclude commas */
194*7c478bd9Sstevel@tonic-gate strlen(SLP_SUN_VERSION_TAG) +
195*7c478bd9Sstevel@tonic-gate strlen("(&(=2)(|))") + 1 +
196*7c478bd9Sstevel@tonic-gate (scope_cnt *
197*7c478bd9Sstevel@tonic-gate (strlen(SLP_SUN_SCOPES_TAG) +
198*7c478bd9Sstevel@tonic-gate strlen("(=)")))))) { /* (scopes=) */
199*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_new_target_list", "out of memory");
200*7c478bd9Sstevel@tonic-gate free(tl->scopes);
201*7c478bd9Sstevel@tonic-gate free(tl->all_scopes);
202*7c478bd9Sstevel@tonic-gate free(tl->mc_scopes);
203*7c478bd9Sstevel@tonic-gate free(tl);
204*7c478bd9Sstevel@tonic-gate return (SLP_MEMORY_ALLOC_FAILED);
205*7c478bd9Sstevel@tonic-gate }
206*7c478bd9Sstevel@tonic-gate format_query(query, scopes);
207*7c478bd9Sstevel@tonic-gate
208*7c478bd9Sstevel@tonic-gate if ((err = slp_find_das(query, &reply)) != SLP_OK &&
209*7c478bd9Sstevel@tonic-gate err != SLP_NETWORK_ERROR) {
210*7c478bd9Sstevel@tonic-gate free(tl->scopes);
211*7c478bd9Sstevel@tonic-gate free(tl->all_scopes);
212*7c478bd9Sstevel@tonic-gate free(tl->mc_scopes);
213*7c478bd9Sstevel@tonic-gate free(tl);
214*7c478bd9Sstevel@tonic-gate free(query);
215*7c478bd9Sstevel@tonic-gate return (err);
216*7c478bd9Sstevel@tonic-gate }
217*7c478bd9Sstevel@tonic-gate free(query);
218*7c478bd9Sstevel@tonic-gate
219*7c478bd9Sstevel@tonic-gate /* Unpack the reply */
220*7c478bd9Sstevel@tonic-gate if (reply) {
221*7c478bd9Sstevel@tonic-gate int numResults = 0; /* placeholder; not actually used */
222*7c478bd9Sstevel@tonic-gate /* tag call as internal */
223*7c478bd9Sstevel@tonic-gate hp->internal_call = SLP_TRUE;
224*7c478bd9Sstevel@tonic-gate
225*7c478bd9Sstevel@tonic-gate (void) slp_unpackSrvReply(hp, reply, collect_DAs,
226*7c478bd9Sstevel@tonic-gate tl, &collator, &numResults);
227*7c478bd9Sstevel@tonic-gate free(reply);
228*7c478bd9Sstevel@tonic-gate /* invoke last call */
229*7c478bd9Sstevel@tonic-gate (void) slp_unpackSrvReply(hp, NULL, collect_DAs,
230*7c478bd9Sstevel@tonic-gate tl, &collator, &numResults);
231*7c478bd9Sstevel@tonic-gate
232*7c478bd9Sstevel@tonic-gate /* revert internal call tag */
233*7c478bd9Sstevel@tonic-gate hp->internal_call = SLP_FALSE;
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate
236*7c478bd9Sstevel@tonic-gate /*
237*7c478bd9Sstevel@tonic-gate * tl->DAs now points to a list of DAs sorted by the number of
238*7c478bd9Sstevel@tonic-gate * relevant scopes they serve. Using this ordering, populate the
239*7c478bd9Sstevel@tonic-gate * scope array lists.
240*7c478bd9Sstevel@tonic-gate */
241*7c478bd9Sstevel@tonic-gate for (te = tl->DAs; te; te = te->next)
242*7c478bd9Sstevel@tonic-gate add2scopes_list(te, tl);
243*7c478bd9Sstevel@tonic-gate
244*7c478bd9Sstevel@tonic-gate *handle = tl;
245*7c478bd9Sstevel@tonic-gate return (SLP_OK);
246*7c478bd9Sstevel@tonic-gate }
247*7c478bd9Sstevel@tonic-gate
slp_get_uc_scopes(slp_target_list_t * h)248*7c478bd9Sstevel@tonic-gate const char *slp_get_uc_scopes(slp_target_list_t *h) {
249*7c478bd9Sstevel@tonic-gate struct target_list *tl = (struct target_list *)h;
250*7c478bd9Sstevel@tonic-gate return (tl->uc_scopes);
251*7c478bd9Sstevel@tonic-gate }
252*7c478bd9Sstevel@tonic-gate
slp_get_mc_scopes(slp_target_list_t * h)253*7c478bd9Sstevel@tonic-gate const char *slp_get_mc_scopes(slp_target_list_t *h) {
254*7c478bd9Sstevel@tonic-gate struct target_list *tl = (struct target_list *)h;
255*7c478bd9Sstevel@tonic-gate return (tl->mc_scopes);
256*7c478bd9Sstevel@tonic-gate }
257*7c478bd9Sstevel@tonic-gate
slp_next_uc_target(slp_target_list_t * h)258*7c478bd9Sstevel@tonic-gate slp_target_t *slp_next_uc_target(slp_target_list_t *h) {
259*7c478bd9Sstevel@tonic-gate struct scope_targets *p;
260*7c478bd9Sstevel@tonic-gate struct target_list *tl = (struct target_list *)h;
261*7c478bd9Sstevel@tonic-gate
262*7c478bd9Sstevel@tonic-gate if (!(*tl->state))
263*7c478bd9Sstevel@tonic-gate return (NULL);
264*7c478bd9Sstevel@tonic-gate /* find the next unused target */
265*7c478bd9Sstevel@tonic-gate for (; *tl->state; tl->state++) {
266*7c478bd9Sstevel@tonic-gate if (!(*tl->state)->da->used && !(*tl->state)->da->failed)
267*7c478bd9Sstevel@tonic-gate return (*tl->state++);
268*7c478bd9Sstevel@tonic-gate if ((*tl->state)->da->failed) {
269*7c478bd9Sstevel@tonic-gate /* get next failover */
270*7c478bd9Sstevel@tonic-gate if (p = slp_next_failover(*tl->state)) {
271*7c478bd9Sstevel@tonic-gate tl->state++;
272*7c478bd9Sstevel@tonic-gate return (p);
273*7c478bd9Sstevel@tonic-gate }
274*7c478bd9Sstevel@tonic-gate /* else nothing more we can do */
275*7c478bd9Sstevel@tonic-gate }
276*7c478bd9Sstevel@tonic-gate }
277*7c478bd9Sstevel@tonic-gate return (NULL);
278*7c478bd9Sstevel@tonic-gate }
279*7c478bd9Sstevel@tonic-gate
slp_next_failover(slp_target_t * h)280*7c478bd9Sstevel@tonic-gate slp_target_t *slp_next_failover(slp_target_t *h) {
281*7c478bd9Sstevel@tonic-gate struct scope_targets *p = (struct scope_targets *)h;
282*7c478bd9Sstevel@tonic-gate for (p = p->next; p; p = p->next) {
283*7c478bd9Sstevel@tonic-gate if (p->da->used)
284*7c478bd9Sstevel@tonic-gate return (NULL); /* already did this scope */
285*7c478bd9Sstevel@tonic-gate if (!p->da->used && !p->da->failed)
286*7c478bd9Sstevel@tonic-gate return (p);
287*7c478bd9Sstevel@tonic-gate }
288*7c478bd9Sstevel@tonic-gate return (NULL);
289*7c478bd9Sstevel@tonic-gate }
290*7c478bd9Sstevel@tonic-gate
slp_get_target_sin(slp_target_t * h)291*7c478bd9Sstevel@tonic-gate void *slp_get_target_sin(slp_target_t *h) {
292*7c478bd9Sstevel@tonic-gate struct scope_targets *p = (struct scope_targets *)h;
293*7c478bd9Sstevel@tonic-gate return (void *)(p ? &(p->da->sin) : NULL);
294*7c478bd9Sstevel@tonic-gate }
295*7c478bd9Sstevel@tonic-gate
slp_mark_target_used(slp_target_t * h)296*7c478bd9Sstevel@tonic-gate void slp_mark_target_used(slp_target_t *h) {
297*7c478bd9Sstevel@tonic-gate struct scope_targets *p = (struct scope_targets *)h;
298*7c478bd9Sstevel@tonic-gate p->da->used = SLP_TRUE;
299*7c478bd9Sstevel@tonic-gate }
300*7c478bd9Sstevel@tonic-gate
slp_mark_target_failed(slp_target_t * h)301*7c478bd9Sstevel@tonic-gate void slp_mark_target_failed(slp_target_t *h) {
302*7c478bd9Sstevel@tonic-gate struct scope_targets *p = (struct scope_targets *)h;
303*7c478bd9Sstevel@tonic-gate p->da->failed = SLP_TRUE;
304*7c478bd9Sstevel@tonic-gate }
305*7c478bd9Sstevel@tonic-gate
slp_fabricate_target(void * s)306*7c478bd9Sstevel@tonic-gate slp_target_t *slp_fabricate_target(void *s) {
307*7c478bd9Sstevel@tonic-gate struct da_node *dn;
308*7c478bd9Sstevel@tonic-gate struct scope_targets *st;
309*7c478bd9Sstevel@tonic-gate struct sockaddr_in *sin = (struct sockaddr_in *)s;
310*7c478bd9Sstevel@tonic-gate
311*7c478bd9Sstevel@tonic-gate if (!(st = malloc(sizeof (*st)))) {
312*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_fabricate_target", "out of memory");
313*7c478bd9Sstevel@tonic-gate return (NULL);
314*7c478bd9Sstevel@tonic-gate }
315*7c478bd9Sstevel@tonic-gate if (!(dn = malloc(sizeof (*dn)))) {
316*7c478bd9Sstevel@tonic-gate free(st);
317*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_fabricate_target", "out of memory");
318*7c478bd9Sstevel@tonic-gate return (NULL);
319*7c478bd9Sstevel@tonic-gate }
320*7c478bd9Sstevel@tonic-gate (void) memcpy(&(dn->sin), sin, sizeof (dn->sin));
321*7c478bd9Sstevel@tonic-gate dn->used = dn->failed = SLP_FALSE;
322*7c478bd9Sstevel@tonic-gate dn->coverage = 0;
323*7c478bd9Sstevel@tonic-gate dn->proximity = SLP_REMOTE_PROX;
324*7c478bd9Sstevel@tonic-gate dn->next = dn->prev = NULL;
325*7c478bd9Sstevel@tonic-gate
326*7c478bd9Sstevel@tonic-gate st->da = dn;
327*7c478bd9Sstevel@tonic-gate st->next = NULL;
328*7c478bd9Sstevel@tonic-gate
329*7c478bd9Sstevel@tonic-gate return (st);
330*7c478bd9Sstevel@tonic-gate }
331*7c478bd9Sstevel@tonic-gate
slp_free_target(slp_target_t * target)332*7c478bd9Sstevel@tonic-gate void slp_free_target(slp_target_t *target) {
333*7c478bd9Sstevel@tonic-gate struct scope_targets *t = (struct scope_targets *)target;
334*7c478bd9Sstevel@tonic-gate if (!t)
335*7c478bd9Sstevel@tonic-gate return;
336*7c478bd9Sstevel@tonic-gate free(t->da);
337*7c478bd9Sstevel@tonic-gate free(t);
338*7c478bd9Sstevel@tonic-gate }
339*7c478bd9Sstevel@tonic-gate
slp_destroy_target_list(slp_target_list_t * h)340*7c478bd9Sstevel@tonic-gate void slp_destroy_target_list(slp_target_list_t *h) {
341*7c478bd9Sstevel@tonic-gate struct da_node *das, *dap;
342*7c478bd9Sstevel@tonic-gate int i;
343*7c478bd9Sstevel@tonic-gate struct target_list *tl = (struct target_list *)h;
344*7c478bd9Sstevel@tonic-gate
345*7c478bd9Sstevel@tonic-gate /* free da node list */
346*7c478bd9Sstevel@tonic-gate for (das = tl->DAs; das; das = dap) {
347*7c478bd9Sstevel@tonic-gate dap = das->next;
348*7c478bd9Sstevel@tonic-gate free(das->scopes);
349*7c478bd9Sstevel@tonic-gate free(das);
350*7c478bd9Sstevel@tonic-gate }
351*7c478bd9Sstevel@tonic-gate
352*7c478bd9Sstevel@tonic-gate /* free scope target linked lists */
353*7c478bd9Sstevel@tonic-gate for (i = 0; tl->scopes[i]; i++) {
354*7c478bd9Sstevel@tonic-gate struct scope_targets *sts, *stp;
355*7c478bd9Sstevel@tonic-gate for (sts = tl->scopes[i]; sts; sts = stp) {
356*7c478bd9Sstevel@tonic-gate stp = sts->next;
357*7c478bd9Sstevel@tonic-gate free(sts);
358*7c478bd9Sstevel@tonic-gate }
359*7c478bd9Sstevel@tonic-gate }
360*7c478bd9Sstevel@tonic-gate
361*7c478bd9Sstevel@tonic-gate /* free scope array */
362*7c478bd9Sstevel@tonic-gate free(tl->scopes);
363*7c478bd9Sstevel@tonic-gate
364*7c478bd9Sstevel@tonic-gate /* free any char * lists in use */
365*7c478bd9Sstevel@tonic-gate if (tl->uc_scopes)
366*7c478bd9Sstevel@tonic-gate free(tl->uc_scopes);
367*7c478bd9Sstevel@tonic-gate if (tl->mc_scopes)
368*7c478bd9Sstevel@tonic-gate free(tl->mc_scopes);
369*7c478bd9Sstevel@tonic-gate free(tl->all_scopes);
370*7c478bd9Sstevel@tonic-gate
371*7c478bd9Sstevel@tonic-gate /* free the target list struct */
372*7c478bd9Sstevel@tonic-gate free(tl);
373*7c478bd9Sstevel@tonic-gate }
374*7c478bd9Sstevel@tonic-gate
add2scopes_list(struct da_node * te,struct target_list * tl)375*7c478bd9Sstevel@tonic-gate static void add2scopes_list(struct da_node *te, struct target_list *tl) {
376*7c478bd9Sstevel@tonic-gate struct scope_targets **scopes = tl->scopes;
377*7c478bd9Sstevel@tonic-gate char *p, *s;
378*7c478bd9Sstevel@tonic-gate int i;
379*7c478bd9Sstevel@tonic-gate
380*7c478bd9Sstevel@tonic-gate /*
381*7c478bd9Sstevel@tonic-gate * for each scope in tl->uc_scopes:
382*7c478bd9Sstevel@tonic-gate * add this DA if it serves the scope.
383*7c478bd9Sstevel@tonic-gate */
384*7c478bd9Sstevel@tonic-gate i = 0;
385*7c478bd9Sstevel@tonic-gate for (s = tl->uc_scopes; s; s = p) {
386*7c478bd9Sstevel@tonic-gate p = slp_utf_strchr(s, ',');
387*7c478bd9Sstevel@tonic-gate if (p)
388*7c478bd9Sstevel@tonic-gate *p = 0;
389*7c478bd9Sstevel@tonic-gate if (slp_onlist(s, te->scopes)) {
390*7c478bd9Sstevel@tonic-gate struct scope_targets *st, *stp;
391*7c478bd9Sstevel@tonic-gate /* add this DA node to this scope's target list */
392*7c478bd9Sstevel@tonic-gate if (!(st = malloc(sizeof (*st)))) {
393*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "add2scopes_list",
394*7c478bd9Sstevel@tonic-gate "out of memory");
395*7c478bd9Sstevel@tonic-gate return;
396*7c478bd9Sstevel@tonic-gate }
397*7c478bd9Sstevel@tonic-gate st->da = te;
398*7c478bd9Sstevel@tonic-gate st->next = NULL;
399*7c478bd9Sstevel@tonic-gate /* find the end of the target list */
400*7c478bd9Sstevel@tonic-gate for (stp = scopes[i]; stp && stp->next; ) {
401*7c478bd9Sstevel@tonic-gate stp = stp->next;
402*7c478bd9Sstevel@tonic-gate }
403*7c478bd9Sstevel@tonic-gate if (stp)
404*7c478bd9Sstevel@tonic-gate stp->next = st;
405*7c478bd9Sstevel@tonic-gate else
406*7c478bd9Sstevel@tonic-gate scopes[i] = st;
407*7c478bd9Sstevel@tonic-gate }
408*7c478bd9Sstevel@tonic-gate if (p)
409*7c478bd9Sstevel@tonic-gate *p++ = ',';
410*7c478bd9Sstevel@tonic-gate i++;
411*7c478bd9Sstevel@tonic-gate }
412*7c478bd9Sstevel@tonic-gate }
413*7c478bd9Sstevel@tonic-gate
add_da_entry(struct da_node ** tel,struct sockaddr_in * sin,char * scopes,slp_net_prox proximity,int c)414*7c478bd9Sstevel@tonic-gate static void add_da_entry(struct da_node **tel, struct sockaddr_in *sin,
415*7c478bd9Sstevel@tonic-gate char *scopes, slp_net_prox proximity, int c) {
416*7c478bd9Sstevel@tonic-gate struct da_node *te, *p;
417*7c478bd9Sstevel@tonic-gate
418*7c478bd9Sstevel@tonic-gate if (!(te = malloc(sizeof (*te)))) {
419*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "add_da_entry", "out of memory");
420*7c478bd9Sstevel@tonic-gate return;
421*7c478bd9Sstevel@tonic-gate }
422*7c478bd9Sstevel@tonic-gate te->scopes = scopes;
423*7c478bd9Sstevel@tonic-gate te->coverage = c;
424*7c478bd9Sstevel@tonic-gate te->proximity = proximity;
425*7c478bd9Sstevel@tonic-gate (void) memcpy(&(te->sin), sin, sizeof (te->sin));
426*7c478bd9Sstevel@tonic-gate te->used = SLP_FALSE;
427*7c478bd9Sstevel@tonic-gate te->failed = SLP_FALSE;
428*7c478bd9Sstevel@tonic-gate te->prev = NULL;
429*7c478bd9Sstevel@tonic-gate te->next = NULL;
430*7c478bd9Sstevel@tonic-gate
431*7c478bd9Sstevel@tonic-gate /* find its place in the list */
432*7c478bd9Sstevel@tonic-gate if (!(*tel)) {
433*7c478bd9Sstevel@tonic-gate *tel = te;
434*7c478bd9Sstevel@tonic-gate return;
435*7c478bd9Sstevel@tonic-gate }
436*7c478bd9Sstevel@tonic-gate for (p = *tel; p; p = p->next)
437*7c478bd9Sstevel@tonic-gate if (c >= p->coverage) {
438*7c478bd9Sstevel@tonic-gate /* found a coverage grouping; now sort by proximity */
439*7c478bd9Sstevel@tonic-gate for (; p && proximity < p->proximity; )
440*7c478bd9Sstevel@tonic-gate p = p->next;
441*7c478bd9Sstevel@tonic-gate
442*7c478bd9Sstevel@tonic-gate if (!p) {
443*7c478bd9Sstevel@tonic-gate break;
444*7c478bd9Sstevel@tonic-gate }
445*7c478bd9Sstevel@tonic-gate
446*7c478bd9Sstevel@tonic-gate /* add it here */
447*7c478bd9Sstevel@tonic-gate te->next = p;
448*7c478bd9Sstevel@tonic-gate te->prev = p->prev;
449*7c478bd9Sstevel@tonic-gate if (p->prev)
450*7c478bd9Sstevel@tonic-gate p->prev->next = te;
451*7c478bd9Sstevel@tonic-gate else
452*7c478bd9Sstevel@tonic-gate /* we're at the head */
453*7c478bd9Sstevel@tonic-gate (*tel) = te;
454*7c478bd9Sstevel@tonic-gate p->prev = te;
455*7c478bd9Sstevel@tonic-gate return;
456*7c478bd9Sstevel@tonic-gate }
457*7c478bd9Sstevel@tonic-gate
458*7c478bd9Sstevel@tonic-gate /* didn't find a place in the list, so add it at the end */
459*7c478bd9Sstevel@tonic-gate for (p = *tel; p->next; )
460*7c478bd9Sstevel@tonic-gate p = p->next;
461*7c478bd9Sstevel@tonic-gate
462*7c478bd9Sstevel@tonic-gate p->next = te;
463*7c478bd9Sstevel@tonic-gate te->prev = p;
464*7c478bd9Sstevel@tonic-gate }
465*7c478bd9Sstevel@tonic-gate
466*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
collect_DAs(SLPHandle h,const char * u,unsigned short lifetime,SLPError errCode,void * cookie)467*7c478bd9Sstevel@tonic-gate static SLPBoolean collect_DAs(SLPHandle h, const char *u,
468*7c478bd9Sstevel@tonic-gate unsigned short lifetime,
469*7c478bd9Sstevel@tonic-gate SLPError errCode, void *cookie) {
470*7c478bd9Sstevel@tonic-gate SLPSrvURL *surl = NULL;
471*7c478bd9Sstevel@tonic-gate char *s, *p, *sscopes, *sscopes_end, *url;
472*7c478bd9Sstevel@tonic-gate int coverage, proximity;
473*7c478bd9Sstevel@tonic-gate struct sockaddr_in sin[1];
474*7c478bd9Sstevel@tonic-gate struct target_list *tl = (struct target_list *)cookie;
475*7c478bd9Sstevel@tonic-gate
476*7c478bd9Sstevel@tonic-gate if (errCode != SLP_OK)
477*7c478bd9Sstevel@tonic-gate return (SLP_TRUE);
478*7c478bd9Sstevel@tonic-gate
479*7c478bd9Sstevel@tonic-gate /* dup url so as not to corrupt da cache */
480*7c478bd9Sstevel@tonic-gate if (!(url = strdup(u))) {
481*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "collect_DAs", "out of memory");
482*7c478bd9Sstevel@tonic-gate return (SLP_FALSE);
483*7c478bd9Sstevel@tonic-gate }
484*7c478bd9Sstevel@tonic-gate
485*7c478bd9Sstevel@tonic-gate /* parse url into a SLPSrvURL struct */
486*7c478bd9Sstevel@tonic-gate if (SLPParseSrvURL(url, &surl) != SLP_OK) {
487*7c478bd9Sstevel@tonic-gate return (SLP_TRUE); /* bad URL; skip it */
488*7c478bd9Sstevel@tonic-gate }
489*7c478bd9Sstevel@tonic-gate
490*7c478bd9Sstevel@tonic-gate /* determine proximity */
491*7c478bd9Sstevel@tonic-gate if (slp_surl2sin(surl, sin) != SLP_OK) {
492*7c478bd9Sstevel@tonic-gate goto cleanup;
493*7c478bd9Sstevel@tonic-gate }
494*7c478bd9Sstevel@tonic-gate if (slp_on_localhost(h, sin->sin_addr)) {
495*7c478bd9Sstevel@tonic-gate proximity = SLP_LOCAL_PROX;
496*7c478bd9Sstevel@tonic-gate } else if (slp_on_subnet(h, sin->sin_addr)) {
497*7c478bd9Sstevel@tonic-gate proximity = SLP_SUBNET_PROX;
498*7c478bd9Sstevel@tonic-gate } else {
499*7c478bd9Sstevel@tonic-gate proximity = SLP_REMOTE_PROX;
500*7c478bd9Sstevel@tonic-gate }
501*7c478bd9Sstevel@tonic-gate
502*7c478bd9Sstevel@tonic-gate /*
503*7c478bd9Sstevel@tonic-gate * sort the DAs into the entry list, ranked by the number of
504*7c478bd9Sstevel@tonic-gate * relevant scopes they serve (coverage).
505*7c478bd9Sstevel@tonic-gate */
506*7c478bd9Sstevel@tonic-gate coverage = 0;
507*7c478bd9Sstevel@tonic-gate if (!(sscopes = slp_utf_strchr(surl->s_pcSrvPart, '='))) {
508*7c478bd9Sstevel@tonic-gate /* URL part should be of the form 'scopes=...' */
509*7c478bd9Sstevel@tonic-gate goto cleanup;
510*7c478bd9Sstevel@tonic-gate }
511*7c478bd9Sstevel@tonic-gate sscopes++;
512*7c478bd9Sstevel@tonic-gate
513*7c478bd9Sstevel@tonic-gate /* cut off host scope at end */
514*7c478bd9Sstevel@tonic-gate if (sscopes_end = slp_utf_strchr(sscopes, '=')) {
515*7c478bd9Sstevel@tonic-gate /* skip the =[hostname] at the end */
516*7c478bd9Sstevel@tonic-gate *sscopes_end = 0;
517*7c478bd9Sstevel@tonic-gate }
518*7c478bd9Sstevel@tonic-gate
519*7c478bd9Sstevel@tonic-gate /* copy out the scopes part, since url will be freed after this call */
520*7c478bd9Sstevel@tonic-gate if (!(sscopes = strdup(sscopes))) {
521*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "collect_DAs", "out of memory");
522*7c478bd9Sstevel@tonic-gate free(surl);
523*7c478bd9Sstevel@tonic-gate return (SLP_FALSE);
524*7c478bd9Sstevel@tonic-gate }
525*7c478bd9Sstevel@tonic-gate
526*7c478bd9Sstevel@tonic-gate for (s = tl->all_scopes; s; s = p) {
527*7c478bd9Sstevel@tonic-gate p = slp_utf_strchr(s, ',');
528*7c478bd9Sstevel@tonic-gate if (p)
529*7c478bd9Sstevel@tonic-gate *p = 0;
530*7c478bd9Sstevel@tonic-gate if (slp_onlist(s, sscopes)) {
531*7c478bd9Sstevel@tonic-gate /* add to uc list; remove from mc list */
532*7c478bd9Sstevel@tonic-gate slp_add2list(s, &(tl->uc_scopes), SLP_TRUE);
533*7c478bd9Sstevel@tonic-gate slp_list_subtract(s, &(tl->mc_scopes));
534*7c478bd9Sstevel@tonic-gate coverage++;
535*7c478bd9Sstevel@tonic-gate }
536*7c478bd9Sstevel@tonic-gate if (p)
537*7c478bd9Sstevel@tonic-gate *p++ = ',';
538*7c478bd9Sstevel@tonic-gate }
539*7c478bd9Sstevel@tonic-gate if (coverage)
540*7c478bd9Sstevel@tonic-gate add_da_entry(&(tl->DAs), sin, sscopes, proximity, coverage);
541*7c478bd9Sstevel@tonic-gate
542*7c478bd9Sstevel@tonic-gate cleanup:
543*7c478bd9Sstevel@tonic-gate free(url);
544*7c478bd9Sstevel@tonic-gate if (surl) free(surl);
545*7c478bd9Sstevel@tonic-gate
546*7c478bd9Sstevel@tonic-gate return (SLP_TRUE);
547*7c478bd9Sstevel@tonic-gate }
548*7c478bd9Sstevel@tonic-gate
549*7c478bd9Sstevel@tonic-gate /*
550*7c478bd9Sstevel@tonic-gate * Takes a scopes list of the form 's1,s2,s3,...' and formats it into
551*7c478bd9Sstevel@tonic-gate * an LDAP search filter of the form '(|(SCOPETAG=s1)(SCOPETAG=s2)...)'.
552*7c478bd9Sstevel@tonic-gate * 'scopes' contains the scopes list; 'q' is a buffer allocated
553*7c478bd9Sstevel@tonic-gate * by the caller into which the result will be placed.
554*7c478bd9Sstevel@tonic-gate */
format_query(char * q,const char * scopes)555*7c478bd9Sstevel@tonic-gate static void format_query(char *q, const char *scopes) {
556*7c478bd9Sstevel@tonic-gate char *p, *s;
557*7c478bd9Sstevel@tonic-gate int more_than_one = slp_utf_strchr(scopes, ',') ? 1 : 0;
558*7c478bd9Sstevel@tonic-gate
559*7c478bd9Sstevel@tonic-gate *q++ = '('; *q++ = '&';
560*7c478bd9Sstevel@tonic-gate if (more_than_one) {
561*7c478bd9Sstevel@tonic-gate *q++ = '('; *q++ = '|';
562*7c478bd9Sstevel@tonic-gate }
563*7c478bd9Sstevel@tonic-gate
564*7c478bd9Sstevel@tonic-gate for (p = s = (char *)scopes; p; s = p) {
565*7c478bd9Sstevel@tonic-gate *q++ = '(';
566*7c478bd9Sstevel@tonic-gate (void) strcpy(q, SLP_SUN_SCOPES_TAG);
567*7c478bd9Sstevel@tonic-gate q += strlen(SLP_SUN_SCOPES_TAG);
568*7c478bd9Sstevel@tonic-gate *q++ = '=';
569*7c478bd9Sstevel@tonic-gate
570*7c478bd9Sstevel@tonic-gate p = slp_utf_strchr(s, ',');
571*7c478bd9Sstevel@tonic-gate if (p) {
572*7c478bd9Sstevel@tonic-gate (void) memcpy(q, s, p - s);
573*7c478bd9Sstevel@tonic-gate q += (p - s);
574*7c478bd9Sstevel@tonic-gate p++;
575*7c478bd9Sstevel@tonic-gate } else {
576*7c478bd9Sstevel@tonic-gate (void) strcpy(q, s);
577*7c478bd9Sstevel@tonic-gate q += strlen(s);
578*7c478bd9Sstevel@tonic-gate }
579*7c478bd9Sstevel@tonic-gate *q++ = ')';
580*7c478bd9Sstevel@tonic-gate }
581*7c478bd9Sstevel@tonic-gate
582*7c478bd9Sstevel@tonic-gate if (more_than_one) {
583*7c478bd9Sstevel@tonic-gate *q++ = ')';
584*7c478bd9Sstevel@tonic-gate }
585*7c478bd9Sstevel@tonic-gate *q++ = '(';
586*7c478bd9Sstevel@tonic-gate (void) strcpy(q, SLP_SUN_VERSION_TAG);
587*7c478bd9Sstevel@tonic-gate q += strlen(SLP_SUN_VERSION_TAG);
588*7c478bd9Sstevel@tonic-gate *q++ = '=';
589*7c478bd9Sstevel@tonic-gate *q++ = '2';
590*7c478bd9Sstevel@tonic-gate *q++ = ')';
591*7c478bd9Sstevel@tonic-gate *q++ = ')';
592*7c478bd9Sstevel@tonic-gate *q = 0;
593*7c478bd9Sstevel@tonic-gate }
594