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