rpc_soc.c revision 1.2 1 /* $NetBSD: rpc_soc.c,v 1.2 2000/06/03 13:04:14 fvdl Exp $ */
2
3 /*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31
32 /* #ident "@(#)rpc_soc.c 1.17 94/04/24 SMI" */
33
34 /*
35 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
36 * In addition, portions of such source code were derived from Berkeley
37 * 4.3 BSD under license from the Regents of the University of
38 * California.
39 */
40
41 #if 0
42 #if !defined(lint) && defined(SCCSIDS)
43 static char sccsid[] = "@(#)rpc_soc.c 1.41 89/05/02 Copyr 1988 Sun Micro";
44 #endif
45 #endif
46
47 #ifdef PORTMAP
48 /*
49 * rpc_soc.c
50 *
51 * The backward compatibility routines for the earlier implementation
52 * of RPC, where the only transports supported were tcp/ip and udp/ip.
53 * Based on berkeley socket abstraction, now implemented on the top
54 * of TLI/Streams
55 */
56
57 #include "namespace.h"
58 #include "reentrant.h"
59 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <stdio.h>
62 #include <rpc/rpc.h>
63 #include <rpc/pmap_clnt.h>
64 #include <rpc/pmap_prot.h>
65 #include <netinet/in.h>
66 #include <netdb.h>
67 #include <errno.h>
68 #include <syslog.h>
69 #include <stdlib.h>
70 #include <unistd.h>
71
72 #include "rpc_com.h"
73
74 #ifdef __weak_alias
75 __weak_alias(clntudp_bufcreate,_clntudp_bufcreate)
76 __weak_alias(clntudp_create,_clntudp_create)
77 __weak_alias(clnttcp_create,_clnttcp_create)
78 __weak_alias(clntraw_create,_clntraw_create)
79 __weak_alias(get_myaddress,_get_myaddress)
80 __weak_alias(svcudp_bufcreate,_svcudp_bufcreate)
81 __weak_alias(svcudp_create,_svcudp_create)
82 __weak_alias(svctcp_create,_svctcp_create)
83 __weak_alias(svcraw_create,_svcraw_create)
84 __weak_alias(callrpc,_callrpc)
85 __weak_alias(registerrpc,_registerrpc)
86 __weak_alias(clnt_broadcast,_clnt_broadcast)
87 #endif
88
89 #ifdef __REENT
90 extern mutex_t rpcsoc_lock;
91 #endif
92
93 static CLIENT *clnt_com_create __P((struct sockaddr_in *, rpcprog_t, rpcvers_t,
94 int *, u_int, u_int, char *));
95 static SVCXPRT *svc_com_create __P((int, u_int, u_int, char *));
96 static bool_t rpc_wrap_bcast __P((char *, struct netbuf *, struct netconfig *));
97
98 /*
99 * A common clnt create routine
100 */
101 static CLIENT *
102 clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, tp)
103 struct sockaddr_in *raddr;
104 rpcprog_t prog;
105 rpcvers_t vers;
106 int *sockp;
107 u_int sendsz;
108 u_int recvsz;
109 char *tp;
110 {
111 CLIENT *cl;
112 int madefd = FALSE;
113 int fd = *sockp;
114 struct netconfig *nconf;
115 struct netbuf bindaddr;
116
117 mutex_lock(&rpcsoc_lock);
118 if ((nconf = __rpc_getconfip(tp)) == NULL) {
119 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
120 mutex_unlock(&rpcsoc_lock);
121 return ((CLIENT *)NULL);
122 }
123 if (fd == RPC_ANYSOCK) {
124 fd = __rpc_nconf2fd(nconf);
125 if (fd == -1)
126 goto syserror;
127 madefd = TRUE;
128 }
129
130 if (raddr->sin_port == 0) {
131 u_int proto;
132 u_short sport;
133
134 mutex_unlock(&rpcsoc_lock); /* pmap_getport is recursive */
135 proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
136 sport = pmap_getport(raddr, prog, vers, proto);
137 if (sport == 0) {
138 goto err;
139 }
140 raddr->sin_port = htons(sport);
141 mutex_lock(&rpcsoc_lock); /* pmap_getport is recursive */
142 }
143
144 /* Transform sockaddr_in to netbuf */
145 bindaddr.maxlen = bindaddr.len = sizeof (struct sockaddr_in);
146 bindaddr.buf = raddr;
147
148 bindresvport(fd, NULL);
149 cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
150 sendsz, recvsz);
151 if (cl) {
152 if (madefd == TRUE) {
153 /*
154 * The fd should be closed while destroying the handle.
155 */
156 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, (char *)NULL);
157 *sockp = fd;
158 }
159 (void) freenetconfigent(nconf);
160 mutex_unlock(&rpcsoc_lock);
161 return (cl);
162 }
163 goto err;
164
165 syserror:
166 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
167 rpc_createerr.cf_error.re_errno = errno;
168
169 err: if (madefd == TRUE)
170 (void) close(fd);
171 (void) freenetconfigent(nconf);
172 mutex_unlock(&rpcsoc_lock);
173 return ((CLIENT *)NULL);
174 }
175
176 CLIENT *
177 clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz)
178 register struct sockaddr_in *raddr;
179 u_long prog;
180 u_long vers;
181 struct timeval wait;
182 int *sockp;
183 u_int sendsz;
184 u_int recvsz;
185 {
186 CLIENT *cl;
187
188 cl = clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, "udp");
189 if (cl == (CLIENT *)NULL) {
190 return ((CLIENT *)NULL);
191 }
192 (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, (char *)&wait);
193 return (cl);
194 }
195
196 CLIENT *
197 clntudp_create(raddr, program, version, wait, sockp)
198 struct sockaddr_in *raddr;
199 u_long program;
200 u_long version;
201 struct timeval wait;
202 int *sockp;
203 {
204 return clntudp_bufcreate(raddr, program, version, wait, sockp,
205 UDPMSGSIZE, UDPMSGSIZE);
206 }
207
208 CLIENT *
209 clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
210 struct sockaddr_in *raddr;
211 u_long prog;
212 u_long vers;
213 register int *sockp;
214 u_int sendsz;
215 u_int recvsz;
216 {
217 return clnt_com_create(raddr, prog, vers, sockp, sendsz,
218 recvsz, "tcp");
219 }
220
221 CLIENT *
222 clntraw_create(prog, vers)
223 u_long prog;
224 u_long vers;
225 {
226 return clnt_raw_create(prog, vers);
227 }
228
229 /*
230 * A common server create routine
231 */
232 static SVCXPRT *
233 svc_com_create(fd, sendsize, recvsize, netid)
234 register int fd;
235 u_int sendsize;
236 u_int recvsize;
237 char *netid;
238 {
239 struct netconfig *nconf;
240 SVCXPRT *svc;
241 int madefd = FALSE;
242 int port;
243 struct sockaddr_in sin;
244
245 if ((nconf = __rpc_getconfip(netid)) == NULL) {
246 (void) syslog(LOG_ERR, "Could not get %s transport", netid);
247 return ((SVCXPRT *)NULL);
248 }
249 if (fd == RPC_ANYSOCK) {
250 fd = __rpc_nconf2fd(nconf);
251 if (fd == -1) {
252 (void) freenetconfigent(nconf);
253 (void) syslog(LOG_ERR,
254 "svc%s_create: could not open connection", netid);
255 return ((SVCXPRT *)NULL);
256 }
257 madefd = TRUE;
258 }
259
260 memset(&sin, 0, sizeof sin);
261 sin.sin_family = AF_INET;
262 bindresvport(fd, &sin);
263 svc = svc_tli_create(fd, nconf, (struct t_bind *)NULL,
264 sendsize, recvsize);
265 (void) freenetconfigent(nconf);
266 if (svc == (SVCXPRT *)NULL) {
267 if (madefd)
268 (void) close(fd);
269 return ((SVCXPRT *)NULL);
270 }
271 port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
272 svc->xp_port = ntohs(port);
273 return (svc);
274 }
275
276 SVCXPRT *
277 svctcp_create(fd, sendsize, recvsize)
278 register int fd;
279 u_int sendsize;
280 u_int recvsize;
281 {
282 return svc_com_create(fd, sendsize, recvsize, "tcp");
283 }
284
285 SVCXPRT *
286 svcudp_bufcreate(fd, sendsz, recvsz)
287 register int fd;
288 u_int sendsz, recvsz;
289 {
290 return svc_com_create(fd, sendsz, recvsz, "udp");
291 }
292
293 SVCXPRT *
294 svcfd_create(fd, sendsize, recvsize)
295 int fd;
296 u_int sendsize;
297 u_int recvsize;
298 {
299 return svc_fd_create(fd, sendsize, recvsize);
300 }
301
302
303 SVCXPRT *
304 svcudp_create(fd)
305 register int fd;
306 {
307 return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
308 }
309
310 SVCXPRT *
311 svcraw_create()
312 {
313 return svc_raw_create();
314 }
315
316 int
317 get_myaddress(addr)
318 struct sockaddr_in *addr;
319 {
320 memset((void *) addr, 0, sizeof(*addr));
321 addr->sin_family = AF_INET;
322 addr->sin_port = htons(PMAPPORT);
323 addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
324 return (0);
325 }
326
327 /*
328 * For connectionless "udp" transport. Obsoleted by rpc_call().
329 */
330 int
331 callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
332 char *host;
333 int prognum, versnum, procnum;
334 xdrproc_t inproc, outproc;
335 char *in, *out;
336 {
337 return (int)rpc_call(host, (u_long)prognum, (u_long)versnum,
338 (u_long)procnum, inproc, in, outproc, out, "udp");
339 }
340
341 /*
342 * For connectionless kind of transport. Obsoleted by rpc_reg()
343 */
344 int
345 registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
346 int prognum, versnum, procnum;
347 char *(*progname) __P((char [UDPMSGSIZE]));
348 xdrproc_t inproc, outproc;
349 {
350 return rpc_reg((u_long)prognum, (u_long)versnum, (u_long)procnum,
351 progname, inproc, outproc, "udp");
352 }
353
354 /*
355 * All the following clnt_broadcast stuff is convulated; it supports
356 * the earlier calling style of the callback function
357 */
358 #ifdef __REENT
359 static thread_key_t clnt_broadcast_key;
360 #endif
361 static resultproc_t clnt_broadcast_result_main;
362
363 /*
364 * Need to translate the netbuf address into sockaddr_in address.
365 * Dont care about netid here.
366 */
367 /* ARGSUSED */
368 static bool_t
369 rpc_wrap_bcast(resultp, addr, nconf)
370 char *resultp; /* results of the call */
371 struct netbuf *addr; /* address of the guy who responded */
372 struct netconfig *nconf; /* Netconf of the transport */
373 {
374 resultproc_t clnt_broadcast_result;
375
376 if (strcmp(nconf->nc_netid, "udp"))
377 return (FALSE);
378 #ifdef __REENT
379 if (_thr_main())
380 clnt_broadcast_result = clnt_broadcast_result_main;
381 else
382 thr_getspecific(clnt_broadcast_key,
383 (void **) &clnt_broadcast_result);
384 #else
385 clnt_broadcast_result = clnt_broadcast_result_main;
386 #endif
387 return (*clnt_broadcast_result)(resultp,
388 (struct sockaddr_in *)addr->buf);
389 }
390
391 /*
392 * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
393 */
394 enum clnt_stat
395 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
396 u_long prog; /* program number */
397 u_long vers; /* version number */
398 u_long proc; /* procedure number */
399 xdrproc_t xargs; /* xdr routine for args */
400 caddr_t argsp; /* pointer to args */
401 xdrproc_t xresults; /* xdr routine for results */
402 caddr_t resultsp; /* pointer to results */
403 resultproc_t eachresult; /* call with each result obtained */
404 {
405 #ifdef __REENT
406 extern mutex_t tsd_lock;
407 #endif
408
409 #ifdef __REENT
410 if (_thr_main())
411 clnt_broadcast_result_main = eachresult;
412 else {
413 if (clnt_broadcast_key == 0) {
414 mutex_lock(&tsd_lock);
415 if (clnt_broadcast_key == 0)
416 thr_keycreate(&clnt_broadcast_key, free);
417 mutex_unlock(&tsd_lock);
418 }
419 thr_setspecific(clnt_broadcast_key, (void *) eachresult);
420 }
421 #else
422 clnt_broadcast_result_main = eachresult;
423 #endif
424 return rpc_broadcast(prog, vers, proc, xargs, argsp, xresults,
425 resultsp, (resultproc_t) rpc_wrap_bcast, "udp");
426 }
427
428 #endif /* PORTMAP */
429