1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <syslog.h>
32*0Sstevel@tonic-gate #include <string.h>
33*0Sstevel@tonic-gate #include <thread.h>
34*0Sstevel@tonic-gate #include <synch.h>
35*0Sstevel@tonic-gate #include <slp-internal.h>
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate /* This is used to pass needed params to consumer_thr and slp_call */
38*0Sstevel@tonic-gate struct thr_call_args {
39*0Sstevel@tonic-gate slp_handle_impl_t *hp;
40*0Sstevel@tonic-gate SLPGenericAppCB *cb;
41*0Sstevel@tonic-gate void *cookie;
42*0Sstevel@tonic-gate SLPMsgReplyCB *msg_cb;
43*0Sstevel@tonic-gate slp_target_list_t *targets;
44*0Sstevel@tonic-gate };
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate static SLPError consumer(void *);
47*0Sstevel@tonic-gate static void slp_call(void *);
48*0Sstevel@tonic-gate static SLPError check_message_fit(slp_handle_impl_t *, slp_target_list_t *);
49*0Sstevel@tonic-gate
slp_ua_common(SLPHandle hSLP,const char * scopes,SLPGenericAppCB cb,void * cookie,SLPMsgReplyCB msg_cb)50*0Sstevel@tonic-gate SLPError slp_ua_common(SLPHandle hSLP, const char *scopes,
51*0Sstevel@tonic-gate SLPGenericAppCB cb, void *cookie,
52*0Sstevel@tonic-gate SLPMsgReplyCB msg_cb) {
53*0Sstevel@tonic-gate slp_handle_impl_t *hp;
54*0Sstevel@tonic-gate slp_target_list_t *targets;
55*0Sstevel@tonic-gate struct thr_call_args *args;
56*0Sstevel@tonic-gate slp_queue_t *q;
57*0Sstevel@tonic-gate SLPError err;
58*0Sstevel@tonic-gate thread_t tid;
59*0Sstevel@tonic-gate int terr;
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate hp = (slp_handle_impl_t *)hSLP;
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate /* select targets */
64*0Sstevel@tonic-gate if ((err = slp_new_target_list(hp, scopes, &targets)) != SLP_OK)
65*0Sstevel@tonic-gate return (err);
66*0Sstevel@tonic-gate if ((err = check_message_fit(hp, targets)) != SLP_OK) {
67*0Sstevel@tonic-gate slp_destroy_target_list(targets);
68*0Sstevel@tonic-gate return (err);
69*0Sstevel@tonic-gate }
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate /* populate the args structure */
72*0Sstevel@tonic-gate args = malloc(sizeof (*args));
73*0Sstevel@tonic-gate if (args == NULL) {
74*0Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "ua_common", "out of memory");
75*0Sstevel@tonic-gate return (SLP_MEMORY_ALLOC_FAILED);
76*0Sstevel@tonic-gate }
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gate args->hp = hp;
79*0Sstevel@tonic-gate args->cb = cb;
80*0Sstevel@tonic-gate args->cookie = cookie;
81*0Sstevel@tonic-gate args->msg_cb = msg_cb;
82*0Sstevel@tonic-gate args->targets = targets;
83*0Sstevel@tonic-gate
84*0Sstevel@tonic-gate /* create the queue that this call will use */
85*0Sstevel@tonic-gate q = slp_new_queue(&err); /* freed in consumer_thr */
86*0Sstevel@tonic-gate if (err != SLP_OK)
87*0Sstevel@tonic-gate goto error;
88*0Sstevel@tonic-gate hp->q = q;
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate /* kick off the producer thread */
91*0Sstevel@tonic-gate if ((terr = thr_create(
92*0Sstevel@tonic-gate NULL, 0, (void *(*)(void *)) slp_call, args, 0, &tid)) != 0) {
93*0Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "ua_common", "could not start thread: %s",
94*0Sstevel@tonic-gate strerror(terr));
95*0Sstevel@tonic-gate err = SLP_INTERNAL_SYSTEM_ERROR;
96*0Sstevel@tonic-gate goto error;
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate hp->producer_tid = tid;
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate if (hp->async) {
101*0Sstevel@tonic-gate /* kick off the consumer thread */
102*0Sstevel@tonic-gate if ((terr = thr_create(
103*0Sstevel@tonic-gate NULL, 0, (void *(*)(void *))consumer,
104*0Sstevel@tonic-gate args, 0, NULL)) != 0) {
105*0Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "ua_common",
106*0Sstevel@tonic-gate "could not start thread: %s",
107*0Sstevel@tonic-gate strerror(terr));
108*0Sstevel@tonic-gate err = SLP_INTERNAL_SYSTEM_ERROR;
109*0Sstevel@tonic-gate /* cleanup producer thread, if necessary */
110*0Sstevel@tonic-gate hp->cancel = 1;
111*0Sstevel@tonic-gate (void) thr_join(tid, NULL, NULL);
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate goto error;
114*0Sstevel@tonic-gate }
115*0Sstevel@tonic-gate return (SLP_OK);
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate /* else sync */
118*0Sstevel@tonic-gate return (consumer(args));
119*0Sstevel@tonic-gate error:
120*0Sstevel@tonic-gate free(args);
121*0Sstevel@tonic-gate return (err);
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate
consumer(void * ap)124*0Sstevel@tonic-gate static SLPError consumer(void *ap) {
125*0Sstevel@tonic-gate slp_handle_impl_t *hp;
126*0Sstevel@tonic-gate char *reply;
127*0Sstevel@tonic-gate void *collator;
128*0Sstevel@tonic-gate int numResults = 0;
129*0Sstevel@tonic-gate struct thr_call_args *args = (struct thr_call_args *)ap;
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate hp = args->hp;
132*0Sstevel@tonic-gate collator = NULL;
133*0Sstevel@tonic-gate hp->consumer_tid = thr_self();
134*0Sstevel@tonic-gate /* while cb wants more and there is more to get ... */
135*0Sstevel@tonic-gate for (;;) {
136*0Sstevel@tonic-gate SLPBoolean cont;
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate reply = slp_dequeue(hp->q);
139*0Sstevel@tonic-gate /* reply == NULL if no more available or SLPClosed */
140*0Sstevel@tonic-gate cont = args->msg_cb(hp, reply, args->cb, args->cookie,
141*0Sstevel@tonic-gate &collator, &numResults);
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate if (reply) {
144*0Sstevel@tonic-gate free(reply);
145*0Sstevel@tonic-gate } else {
146*0Sstevel@tonic-gate break;
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate if (!cont) {
150*0Sstevel@tonic-gate /* cb doesn't want any more; invoke last call */
151*0Sstevel@tonic-gate args->msg_cb(hp, NULL, args->cb, args->cookie,
152*0Sstevel@tonic-gate &collator, &numResults);
153*0Sstevel@tonic-gate break;
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate }
156*0Sstevel@tonic-gate /* cleanup */
157*0Sstevel@tonic-gate /* clean stop producer [thread] */
158*0Sstevel@tonic-gate hp->cancel = 1;
159*0Sstevel@tonic-gate (void) thr_join(hp->producer_tid, NULL, NULL);
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate /* empty and free queue */
162*0Sstevel@tonic-gate slp_flush_queue(hp->q, free);
163*0Sstevel@tonic-gate slp_destroy_queue(hp->q);
164*0Sstevel@tonic-gate
165*0Sstevel@tonic-gate free(args);
166*0Sstevel@tonic-gate slp_end_call(hp);
167*0Sstevel@tonic-gate return (SLP_OK);
168*0Sstevel@tonic-gate }
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate /*
171*0Sstevel@tonic-gate * This is the producer thread
172*0Sstevel@tonic-gate */
slp_call(void * ap)173*0Sstevel@tonic-gate static void slp_call(void *ap) {
174*0Sstevel@tonic-gate struct thr_call_args *args = (struct thr_call_args *)ap;
175*0Sstevel@tonic-gate slp_target_t *t;
176*0Sstevel@tonic-gate const char *uc_scopes, *mc_scopes;
177*0Sstevel@tonic-gate SLPBoolean use_tcp = SLP_FALSE;
178*0Sstevel@tonic-gate size_t len;
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate /* Unicast */
181*0Sstevel@tonic-gate if (uc_scopes = slp_get_uc_scopes(args->targets)) {
182*0Sstevel@tonic-gate size_t mtu;
183*0Sstevel@tonic-gate int i;
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate /* calculate msg length */
186*0Sstevel@tonic-gate len = slp_hdrlang_length(args->hp);
187*0Sstevel@tonic-gate for (i = 0; i < args->hp->msg.iovlen; i++) {
188*0Sstevel@tonic-gate len += args->hp->msg.iov[i].iov_len;
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate len += strlen(uc_scopes);
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gate mtu = slp_get_mtu();
193*0Sstevel@tonic-gate if (len > mtu)
194*0Sstevel@tonic-gate use_tcp = SLP_TRUE;
195*0Sstevel@tonic-gate
196*0Sstevel@tonic-gate for (
197*0Sstevel@tonic-gate t = slp_next_uc_target(args->targets);
198*0Sstevel@tonic-gate t;
199*0Sstevel@tonic-gate t = slp_next_uc_target(args->targets)) {
200*0Sstevel@tonic-gate if (args->hp->cancel)
201*0Sstevel@tonic-gate break;
202*0Sstevel@tonic-gate
203*0Sstevel@tonic-gate if (use_tcp)
204*0Sstevel@tonic-gate slp_uc_tcp_send(args->hp, t, uc_scopes,
205*0Sstevel@tonic-gate SLP_FALSE, 0);
206*0Sstevel@tonic-gate else
207*0Sstevel@tonic-gate slp_uc_udp_send(args->hp, t, uc_scopes);
208*0Sstevel@tonic-gate }
209*0Sstevel@tonic-gate }
210*0Sstevel@tonic-gate
211*0Sstevel@tonic-gate /* Multicast */
212*0Sstevel@tonic-gate if ((!args->hp->cancel) &&
213*0Sstevel@tonic-gate (mc_scopes = slp_get_mc_scopes(args->targets)))
214*0Sstevel@tonic-gate slp_mc_send(args->hp, mc_scopes);
215*0Sstevel@tonic-gate
216*0Sstevel@tonic-gate /* Wait for TCP to complete, if necessary */
217*0Sstevel@tonic-gate if (args->hp->tcp_lock)
218*0Sstevel@tonic-gate slp_tcp_wait(args->hp);
219*0Sstevel@tonic-gate
220*0Sstevel@tonic-gate slp_destroy_target_list(args->targets);
221*0Sstevel@tonic-gate
222*0Sstevel@tonic-gate /* free the message */
223*0Sstevel@tonic-gate free(args->hp->msg.iov);
224*0Sstevel@tonic-gate free(args->hp->msg.msg);
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gate /* null terminate message queue */
227*0Sstevel@tonic-gate (void) slp_enqueue(args->hp->q, NULL);
228*0Sstevel@tonic-gate
229*0Sstevel@tonic-gate thr_exit(NULL); /* we're outa here */
230*0Sstevel@tonic-gate }
231*0Sstevel@tonic-gate
232*0Sstevel@tonic-gate /*
233*0Sstevel@tonic-gate * If the message to be sent needs to be multicast, check that it
234*0Sstevel@tonic-gate * can fit into a datagram. If not, return BUFFER_OVERFLOW, otherwise
235*0Sstevel@tonic-gate * return SLP_OK.
236*0Sstevel@tonic-gate */
check_message_fit(slp_handle_impl_t * hp,slp_target_list_t * targets)237*0Sstevel@tonic-gate static SLPError check_message_fit(slp_handle_impl_t *hp,
238*0Sstevel@tonic-gate slp_target_list_t *targets) {
239*0Sstevel@tonic-gate size_t msgSize;
240*0Sstevel@tonic-gate int i;
241*0Sstevel@tonic-gate const char *mc_scopes;
242*0Sstevel@tonic-gate
243*0Sstevel@tonic-gate if (!(mc_scopes = slp_get_mc_scopes(targets)))
244*0Sstevel@tonic-gate return (SLP_OK); /* no mc targets to worry about */
245*0Sstevel@tonic-gate
246*0Sstevel@tonic-gate msgSize = slp_hdrlang_length(hp);
247*0Sstevel@tonic-gate for (i = 0; i < hp->msg.iovlen; i++) {
248*0Sstevel@tonic-gate msgSize += hp->msg.iov[i].iov_len;
249*0Sstevel@tonic-gate }
250*0Sstevel@tonic-gate msgSize += strlen(mc_scopes);
251*0Sstevel@tonic-gate
252*0Sstevel@tonic-gate if (msgSize > slp_get_mtu())
253*0Sstevel@tonic-gate return (SLP_BUFFER_OVERFLOW);
254*0Sstevel@tonic-gate return (SLP_OK);
255*0Sstevel@tonic-gate }
256