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