rpc_soc.c revision 1.3 1 /* $NetBSD: rpc_soc.c,v 1.3 2000/06/03 14:30:11 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(svcfd_create,_svcfd_create)
81 __weak_alias(svcudp_bufcreate,_svcudp_bufcreate)
82 __weak_alias(svcudp_create,_svcudp_create)
83 __weak_alias(svctcp_create,_svctcp_create)
84 __weak_alias(svcraw_create,_svcraw_create)
85 __weak_alias(callrpc,_callrpc)
86 __weak_alias(registerrpc,_registerrpc)
87 __weak_alias(clnt_broadcast,_clnt_broadcast)
88 #endif
89
90 #ifdef __REENT
91 extern mutex_t rpcsoc_lock;
92 #endif
93
94 static CLIENT *clnt_com_create __P((struct sockaddr_in *, rpcprog_t, rpcvers_t,
95 int *, u_int, u_int, char *));
96 static SVCXPRT *svc_com_create __P((int, u_int, u_int, char *));
97 static bool_t rpc_wrap_bcast __P((char *, struct netbuf *, struct netconfig *));
98
99 /*
100 * A common clnt create routine
101 */
102 static CLIENT *
103 clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, tp)
104 struct sockaddr_in *raddr;
105 rpcprog_t prog;
106 rpcvers_t vers;
107 int *sockp;
108 u_int sendsz;
109 u_int recvsz;
110 char *tp;
111 {
112 CLIENT *cl;
113 int madefd = FALSE;
114 int fd = *sockp;
115 struct netconfig *nconf;
116 struct netbuf bindaddr;
117
118 mutex_lock(&rpcsoc_lock);
119 if ((nconf = __rpc_getconfip(tp)) == NULL) {
120 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
121 mutex_unlock(&rpcsoc_lock);
122 return ((CLIENT *)NULL);
123 }
124 if (fd == RPC_ANYSOCK) {
125 fd = __rpc_nconf2fd(nconf);
126 if (fd == -1)
127 goto syserror;
128 madefd = TRUE;
129 }
130
131 if (raddr->sin_port == 0) {
132 u_int proto;
133 u_short sport;
134
135 mutex_unlock(&rpcsoc_lock); /* pmap_getport is recursive */
136 proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
137 sport = pmap_getport(raddr, prog, vers, proto);
138 if (sport == 0) {
139 goto err;
140 }
141 raddr->sin_port = htons(sport);
142 mutex_lock(&rpcsoc_lock); /* pmap_getport is recursive */
143 }
144
145 /* Transform sockaddr_in to netbuf */
146 bindaddr.maxlen = bindaddr.len = sizeof (struct sockaddr_in);
147 bindaddr.buf = raddr;
148
149 bindresvport(fd, NULL);
150 cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
151 sendsz, recvsz);
152 if (cl) {
153 if (madefd == TRUE) {
154 /*
155 * The fd should be closed while destroying the handle.
156 */
157 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, (char *)NULL);
158 *sockp = fd;
159 }
160 (void) freenetconfigent(nconf);
161 mutex_unlock(&rpcsoc_lock);
162 return (cl);
163 }
164 goto err;
165
166 syserror:
167 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
168 rpc_createerr.cf_error.re_errno = errno;
169
170 err: if (madefd == TRUE)
171 (void) close(fd);
172 (void) freenetconfigent(nconf);
173 mutex_unlock(&rpcsoc_lock);
174 return ((CLIENT *)NULL);
175 }
176
177 CLIENT *
178 clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz)
179 register struct sockaddr_in *raddr;
180 u_long prog;
181 u_long vers;
182 struct timeval wait;
183 int *sockp;
184 u_int sendsz;
185 u_int recvsz;
186 {
187 CLIENT *cl;
188
189 cl = clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, "udp");
190 if (cl == (CLIENT *)NULL) {
191 return ((CLIENT *)NULL);
192 }
193 (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, (char *)&wait);
194 return (cl);
195 }
196
197 CLIENT *
198 clntudp_create(raddr, program, version, wait, sockp)
199 struct sockaddr_in *raddr;
200 u_long program;
201 u_long version;
202 struct timeval wait;
203 int *sockp;
204 {
205 return clntudp_bufcreate(raddr, program, version, wait, sockp,
206 UDPMSGSIZE, UDPMSGSIZE);
207 }
208
209 CLIENT *
210 clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
211 struct sockaddr_in *raddr;
212 u_long prog;
213 u_long vers;
214 register int *sockp;
215 u_int sendsz;
216 u_int recvsz;
217 {
218 return clnt_com_create(raddr, prog, vers, sockp, sendsz,
219 recvsz, "tcp");
220 }
221
222 CLIENT *
223 clntraw_create(prog, vers)
224 u_long prog;
225 u_long vers;
226 {
227 return clnt_raw_create(prog, vers);
228 }
229
230 /*
231 * A common server create routine
232 */
233 static SVCXPRT *
234 svc_com_create(fd, sendsize, recvsize, netid)
235 register int fd;
236 u_int sendsize;
237 u_int recvsize;
238 char *netid;
239 {
240 struct netconfig *nconf;
241 SVCXPRT *svc;
242 int madefd = FALSE;
243 int port;
244 struct sockaddr_in sin;
245
246 if ((nconf = __rpc_getconfip(netid)) == NULL) {
247 (void) syslog(LOG_ERR, "Could not get %s transport", netid);
248 return ((SVCXPRT *)NULL);
249 }
250 if (fd == RPC_ANYSOCK) {
251 fd = __rpc_nconf2fd(nconf);
252 if (fd == -1) {
253 (void) freenetconfigent(nconf);
254 (void) syslog(LOG_ERR,
255 "svc%s_create: could not open connection", netid);
256 return ((SVCXPRT *)NULL);
257 }
258 madefd = TRUE;
259 }
260
261 memset(&sin, 0, sizeof sin);
262 sin.sin_family = AF_INET;
263 bindresvport(fd, &sin);
264 svc = svc_tli_create(fd, nconf, (struct t_bind *)NULL,
265 sendsize, recvsize);
266 (void) freenetconfigent(nconf);
267 if (svc == (SVCXPRT *)NULL) {
268 if (madefd)
269 (void) close(fd);
270 return ((SVCXPRT *)NULL);
271 }
272 port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
273 svc->xp_port = ntohs(port);
274 return (svc);
275 }
276
277 SVCXPRT *
278 svctcp_create(fd, sendsize, recvsize)
279 register int fd;
280 u_int sendsize;
281 u_int recvsize;
282 {
283 return svc_com_create(fd, sendsize, recvsize, "tcp");
284 }
285
286 SVCXPRT *
287 svcudp_bufcreate(fd, sendsz, recvsz)
288 register int fd;
289 u_int sendsz, recvsz;
290 {
291 return svc_com_create(fd, sendsz, recvsz, "udp");
292 }
293
294 SVCXPRT *
295 svcfd_create(fd, sendsize, recvsize)
296 int fd;
297 u_int sendsize;
298 u_int recvsize;
299 {
300 return svc_fd_create(fd, sendsize, recvsize);
301 }
302
303
304 SVCXPRT *
305 svcudp_create(fd)
306 register int fd;
307 {
308 return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
309 }
310
311 SVCXPRT *
312 svcraw_create()
313 {
314 return svc_raw_create();
315 }
316
317 int
318 get_myaddress(addr)
319 struct sockaddr_in *addr;
320 {
321 memset((void *) addr, 0, sizeof(*addr));
322 addr->sin_family = AF_INET;
323 addr->sin_port = htons(PMAPPORT);
324 addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
325 return (0);
326 }
327
328 /*
329 * For connectionless "udp" transport. Obsoleted by rpc_call().
330 */
331 int
332 callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
333 char *host;
334 int prognum, versnum, procnum;
335 xdrproc_t inproc, outproc;
336 char *in, *out;
337 {
338 return (int)rpc_call(host, (u_long)prognum, (u_long)versnum,
339 (u_long)procnum, inproc, in, outproc, out, "udp");
340 }
341
342 /*
343 * For connectionless kind of transport. Obsoleted by rpc_reg()
344 */
345 int
346 registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
347 int prognum, versnum, procnum;
348 char *(*progname) __P((char [UDPMSGSIZE]));
349 xdrproc_t inproc, outproc;
350 {
351 return rpc_reg((u_long)prognum, (u_long)versnum, (u_long)procnum,
352 progname, inproc, outproc, "udp");
353 }
354
355 /*
356 * All the following clnt_broadcast stuff is convulated; it supports
357 * the earlier calling style of the callback function
358 */
359 #ifdef __REENT
360 static thread_key_t clnt_broadcast_key;
361 #endif
362 static resultproc_t clnt_broadcast_result_main;
363
364 /*
365 * Need to translate the netbuf address into sockaddr_in address.
366 * Dont care about netid here.
367 */
368 /* ARGSUSED */
369 static bool_t
370 rpc_wrap_bcast(resultp, addr, nconf)
371 char *resultp; /* results of the call */
372 struct netbuf *addr; /* address of the guy who responded */
373 struct netconfig *nconf; /* Netconf of the transport */
374 {
375 resultproc_t clnt_broadcast_result;
376
377 if (strcmp(nconf->nc_netid, "udp"))
378 return (FALSE);
379 #ifdef __REENT
380 if (_thr_main())
381 clnt_broadcast_result = clnt_broadcast_result_main;
382 else
383 thr_getspecific(clnt_broadcast_key,
384 (void **) &clnt_broadcast_result);
385 #else
386 clnt_broadcast_result = clnt_broadcast_result_main;
387 #endif
388 return (*clnt_broadcast_result)(resultp,
389 (struct sockaddr_in *)addr->buf);
390 }
391
392 /*
393 * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
394 */
395 enum clnt_stat
396 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
397 u_long prog; /* program number */
398 u_long vers; /* version number */
399 u_long proc; /* procedure number */
400 xdrproc_t xargs; /* xdr routine for args */
401 caddr_t argsp; /* pointer to args */
402 xdrproc_t xresults; /* xdr routine for results */
403 caddr_t resultsp; /* pointer to results */
404 resultproc_t eachresult; /* call with each result obtained */
405 {
406 #ifdef __REENT
407 extern mutex_t tsd_lock;
408 #endif
409
410 #ifdef __REENT
411 if (_thr_main())
412 clnt_broadcast_result_main = eachresult;
413 else {
414 if (clnt_broadcast_key == 0) {
415 mutex_lock(&tsd_lock);
416 if (clnt_broadcast_key == 0)
417 thr_keycreate(&clnt_broadcast_key, free);
418 mutex_unlock(&tsd_lock);
419 }
420 thr_setspecific(clnt_broadcast_key, (void *) eachresult);
421 }
422 #else
423 clnt_broadcast_result_main = eachresult;
424 #endif
425 return rpc_broadcast(prog, vers, proc, xargs, argsp, xresults,
426 resultsp, (resultproc_t) rpc_wrap_bcast, "udp");
427 }
428
429 #endif /* PORTMAP */
430