rpc_soc.c revision 1.16 1 1.16 abs /* $NetBSD: rpc_soc.c,v 1.16 2012/06/25 22:32:45 abs 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.11 itojun #include <sys/cdefs.h>
42 1.11 itojun #if defined(LIBC_SCCS) && !defined(lint)
43 1.1 fvdl #if 0
44 1.1 fvdl static char sccsid[] = "@(#)rpc_soc.c 1.41 89/05/02 Copyr 1988 Sun Micro";
45 1.11 itojun #else
46 1.16 abs __RCSID("$NetBSD: rpc_soc.c,v 1.16 2012/06/25 22:32:45 abs Exp $");
47 1.1 fvdl #endif
48 1.1 fvdl #endif
49 1.1 fvdl
50 1.1 fvdl #ifdef PORTMAP
51 1.1 fvdl /*
52 1.1 fvdl * rpc_soc.c
53 1.1 fvdl *
54 1.1 fvdl * The backward compatibility routines for the earlier implementation
55 1.1 fvdl * of RPC, where the only transports supported were tcp/ip and udp/ip.
56 1.1 fvdl * Based on berkeley socket abstraction, now implemented on the top
57 1.1 fvdl * of TLI/Streams
58 1.1 fvdl */
59 1.1 fvdl
60 1.1 fvdl #include "namespace.h"
61 1.1 fvdl #include "reentrant.h"
62 1.1 fvdl #include <sys/types.h>
63 1.1 fvdl #include <sys/socket.h>
64 1.1 fvdl #include <stdio.h>
65 1.1 fvdl #include <rpc/rpc.h>
66 1.1 fvdl #include <rpc/pmap_clnt.h>
67 1.1 fvdl #include <rpc/pmap_prot.h>
68 1.6 christos #include <rpc/nettype.h>
69 1.1 fvdl #include <netinet/in.h>
70 1.7 lukem #include <assert.h>
71 1.7 lukem #include <errno.h>
72 1.1 fvdl #include <netdb.h>
73 1.1 fvdl #include <stdlib.h>
74 1.4 thorpej #include <string.h>
75 1.7 lukem #include <syslog.h>
76 1.1 fvdl #include <unistd.h>
77 1.1 fvdl
78 1.9 fvdl #include "rpc_internal.h"
79 1.1 fvdl
80 1.1 fvdl #ifdef __weak_alias
81 1.1 fvdl __weak_alias(clntudp_bufcreate,_clntudp_bufcreate)
82 1.1 fvdl __weak_alias(clntudp_create,_clntudp_create)
83 1.1 fvdl __weak_alias(clnttcp_create,_clnttcp_create)
84 1.1 fvdl __weak_alias(clntraw_create,_clntraw_create)
85 1.1 fvdl __weak_alias(get_myaddress,_get_myaddress)
86 1.3 fvdl __weak_alias(svcfd_create,_svcfd_create)
87 1.2 fvdl __weak_alias(svcudp_bufcreate,_svcudp_bufcreate)
88 1.1 fvdl __weak_alias(svcudp_create,_svcudp_create)
89 1.1 fvdl __weak_alias(svctcp_create,_svctcp_create)
90 1.1 fvdl __weak_alias(svcraw_create,_svcraw_create)
91 1.1 fvdl __weak_alias(callrpc,_callrpc)
92 1.1 fvdl __weak_alias(registerrpc,_registerrpc)
93 1.1 fvdl __weak_alias(clnt_broadcast,_clnt_broadcast)
94 1.1 fvdl #endif
95 1.1 fvdl
96 1.10 thorpej #ifdef _REENTRANT
97 1.1 fvdl extern mutex_t rpcsoc_lock;
98 1.1 fvdl #endif
99 1.1 fvdl
100 1.14 matt static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t,
101 1.14 matt int *, u_int, u_int, const char *);
102 1.14 matt static SVCXPRT *svc_com_create(int, u_int, u_int, const char *);
103 1.14 matt static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *);
104 1.1 fvdl
105 1.1 fvdl /*
106 1.1 fvdl * A common clnt create routine
107 1.1 fvdl */
108 1.1 fvdl static CLIENT *
109 1.14 matt clnt_com_create(struct sockaddr_in *raddr, rpcprog_t prog, rpcvers_t vers,
110 1.14 matt int *sockp, u_int sendsz, u_int recvsz, const char *tp)
111 1.1 fvdl {
112 1.1 fvdl CLIENT *cl;
113 1.1 fvdl int madefd = FALSE;
114 1.7 lukem int fd;
115 1.1 fvdl struct netconfig *nconf;
116 1.1 fvdl struct netbuf bindaddr;
117 1.1 fvdl
118 1.7 lukem _DIAGASSERT(raddr != NULL);
119 1.7 lukem _DIAGASSERT(sockp != NULL);
120 1.7 lukem _DIAGASSERT(tp != NULL);
121 1.7 lukem
122 1.7 lukem fd = *sockp;
123 1.7 lukem
124 1.1 fvdl mutex_lock(&rpcsoc_lock);
125 1.1 fvdl if ((nconf = __rpc_getconfip(tp)) == NULL) {
126 1.1 fvdl rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
127 1.1 fvdl mutex_unlock(&rpcsoc_lock);
128 1.6 christos return (NULL);
129 1.1 fvdl }
130 1.1 fvdl if (fd == RPC_ANYSOCK) {
131 1.1 fvdl fd = __rpc_nconf2fd(nconf);
132 1.1 fvdl if (fd == -1)
133 1.1 fvdl goto syserror;
134 1.1 fvdl madefd = TRUE;
135 1.1 fvdl }
136 1.1 fvdl
137 1.1 fvdl if (raddr->sin_port == 0) {
138 1.1 fvdl u_int proto;
139 1.1 fvdl u_short sport;
140 1.1 fvdl
141 1.1 fvdl mutex_unlock(&rpcsoc_lock); /* pmap_getport is recursive */
142 1.1 fvdl proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
143 1.6 christos sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
144 1.6 christos proto);
145 1.1 fvdl if (sport == 0) {
146 1.1 fvdl goto err;
147 1.1 fvdl }
148 1.1 fvdl raddr->sin_port = htons(sport);
149 1.1 fvdl mutex_lock(&rpcsoc_lock); /* pmap_getport is recursive */
150 1.1 fvdl }
151 1.1 fvdl
152 1.1 fvdl /* Transform sockaddr_in to netbuf */
153 1.1 fvdl bindaddr.maxlen = bindaddr.len = sizeof (struct sockaddr_in);
154 1.1 fvdl bindaddr.buf = raddr;
155 1.1 fvdl
156 1.1 fvdl bindresvport(fd, NULL);
157 1.1 fvdl cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
158 1.1 fvdl sendsz, recvsz);
159 1.1 fvdl if (cl) {
160 1.1 fvdl if (madefd == TRUE) {
161 1.1 fvdl /*
162 1.1 fvdl * The fd should be closed while destroying the handle.
163 1.1 fvdl */
164 1.6 christos (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
165 1.1 fvdl *sockp = fd;
166 1.1 fvdl }
167 1.1 fvdl (void) freenetconfigent(nconf);
168 1.1 fvdl mutex_unlock(&rpcsoc_lock);
169 1.1 fvdl return (cl);
170 1.1 fvdl }
171 1.1 fvdl goto err;
172 1.1 fvdl
173 1.1 fvdl syserror:
174 1.1 fvdl rpc_createerr.cf_stat = RPC_SYSTEMERROR;
175 1.1 fvdl rpc_createerr.cf_error.re_errno = errno;
176 1.1 fvdl
177 1.1 fvdl err: if (madefd == TRUE)
178 1.1 fvdl (void) close(fd);
179 1.1 fvdl (void) freenetconfigent(nconf);
180 1.1 fvdl mutex_unlock(&rpcsoc_lock);
181 1.6 christos return (NULL);
182 1.1 fvdl }
183 1.1 fvdl
184 1.1 fvdl CLIENT *
185 1.16 abs clntudp_bufcreate(struct sockaddr_in *raddr, u_long prog, u_long vers, struct timeval wait, int *sockp, u_int sendsz, u_int recvsz)
186 1.1 fvdl {
187 1.1 fvdl CLIENT *cl;
188 1.1 fvdl
189 1.7 lukem _DIAGASSERT(raddr != NULL);
190 1.7 lukem _DIAGASSERT(sockp != NULL);
191 1.7 lukem
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.16 abs clntudp_create(struct sockaddr_in *raddr, u_long program, u_long version,
203 1.16 abs struct timeval wait, int *sockp)
204 1.1 fvdl {
205 1.1 fvdl return clntudp_bufcreate(raddr, program, version, wait, sockp,
206 1.1 fvdl UDPMSGSIZE, UDPMSGSIZE);
207 1.1 fvdl }
208 1.1 fvdl
209 1.1 fvdl CLIENT *
210 1.16 abs clnttcp_create(struct sockaddr_in *raddr, u_long prog, u_long vers, int *sockp,
211 1.16 abs u_int sendsz, u_int recvsz)
212 1.1 fvdl {
213 1.6 christos return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
214 1.6 christos sendsz, recvsz, "tcp");
215 1.1 fvdl }
216 1.1 fvdl
217 1.1 fvdl CLIENT *
218 1.14 matt clntraw_create(u_long prog, u_long vers)
219 1.1 fvdl {
220 1.6 christos return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
221 1.1 fvdl }
222 1.1 fvdl
223 1.1 fvdl /*
224 1.1 fvdl * A common server create routine
225 1.1 fvdl */
226 1.1 fvdl static SVCXPRT *
227 1.14 matt svc_com_create(int fd, u_int sendsize, u_int recvsize, const char *netid)
228 1.1 fvdl {
229 1.1 fvdl struct netconfig *nconf;
230 1.1 fvdl SVCXPRT *svc;
231 1.1 fvdl int madefd = FALSE;
232 1.1 fvdl int port;
233 1.8 lukem struct sockaddr_in sccsin;
234 1.1 fvdl
235 1.7 lukem _DIAGASSERT(netid != NULL);
236 1.7 lukem
237 1.1 fvdl if ((nconf = __rpc_getconfip(netid)) == NULL) {
238 1.1 fvdl (void) syslog(LOG_ERR, "Could not get %s transport", netid);
239 1.6 christos return (NULL);
240 1.1 fvdl }
241 1.1 fvdl if (fd == RPC_ANYSOCK) {
242 1.1 fvdl fd = __rpc_nconf2fd(nconf);
243 1.1 fvdl if (fd == -1) {
244 1.1 fvdl (void) freenetconfigent(nconf);
245 1.1 fvdl (void) syslog(LOG_ERR,
246 1.1 fvdl "svc%s_create: could not open connection", netid);
247 1.6 christos return (NULL);
248 1.1 fvdl }
249 1.1 fvdl madefd = TRUE;
250 1.1 fvdl }
251 1.1 fvdl
252 1.8 lukem memset(&sccsin, 0, sizeof sccsin);
253 1.8 lukem sccsin.sin_family = AF_INET;
254 1.8 lukem bindresvport(fd, &sccsin);
255 1.5 fvdl listen(fd, SOMAXCONN);
256 1.6 christos svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
257 1.1 fvdl (void) freenetconfigent(nconf);
258 1.6 christos if (svc == NULL) {
259 1.1 fvdl if (madefd)
260 1.1 fvdl (void) close(fd);
261 1.6 christos return (NULL);
262 1.1 fvdl }
263 1.1 fvdl port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
264 1.1 fvdl svc->xp_port = ntohs(port);
265 1.1 fvdl return (svc);
266 1.1 fvdl }
267 1.1 fvdl
268 1.1 fvdl SVCXPRT *
269 1.16 abs svctcp_create(int fd, u_int sendsize, u_int recvsize)
270 1.1 fvdl {
271 1.1 fvdl return svc_com_create(fd, sendsize, recvsize, "tcp");
272 1.1 fvdl }
273 1.1 fvdl
274 1.1 fvdl SVCXPRT *
275 1.16 abs svcudp_bufcreate(int fd, u_int sendsz, u_int recvsz)
276 1.1 fvdl {
277 1.1 fvdl return svc_com_create(fd, sendsz, recvsz, "udp");
278 1.1 fvdl }
279 1.1 fvdl
280 1.1 fvdl SVCXPRT *
281 1.16 abs svcfd_create(int fd, u_int sendsize, u_int recvsize)
282 1.1 fvdl {
283 1.1 fvdl return svc_fd_create(fd, sendsize, recvsize);
284 1.1 fvdl }
285 1.1 fvdl
286 1.1 fvdl
287 1.1 fvdl SVCXPRT *
288 1.16 abs svcudp_create(int fd)
289 1.1 fvdl {
290 1.1 fvdl return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
291 1.1 fvdl }
292 1.1 fvdl
293 1.1 fvdl SVCXPRT *
294 1.15 christos svcraw_create(void)
295 1.1 fvdl {
296 1.1 fvdl return svc_raw_create();
297 1.1 fvdl }
298 1.1 fvdl
299 1.1 fvdl int
300 1.16 abs get_myaddress(struct sockaddr_in *addr)
301 1.1 fvdl {
302 1.7 lukem
303 1.7 lukem _DIAGASSERT(addr != NULL);
304 1.7 lukem
305 1.1 fvdl memset((void *) addr, 0, sizeof(*addr));
306 1.1 fvdl addr->sin_family = AF_INET;
307 1.1 fvdl addr->sin_port = htons(PMAPPORT);
308 1.1 fvdl addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
309 1.1 fvdl return (0);
310 1.1 fvdl }
311 1.1 fvdl
312 1.1 fvdl /*
313 1.1 fvdl * For connectionless "udp" transport. Obsoleted by rpc_call().
314 1.1 fvdl */
315 1.1 fvdl int
316 1.14 matt callrpc(char *host, int prognum, int versnum, int procnum,
317 1.14 matt xdrproc_t inproc, char *in, xdrproc_t outproc, char *out)
318 1.1 fvdl {
319 1.6 christos return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
320 1.6 christos (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
321 1.1 fvdl }
322 1.1 fvdl
323 1.1 fvdl /*
324 1.1 fvdl * For connectionless kind of transport. Obsoleted by rpc_reg()
325 1.1 fvdl */
326 1.1 fvdl int
327 1.14 matt registerrpc(int prognum, int versnum, int procnum,
328 1.14 matt char *(*progname)(char [UDPMSGSIZE]),
329 1.14 matt xdrproc_t inproc, xdrproc_t outproc)
330 1.1 fvdl {
331 1.6 christos return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
332 1.12 christos (rpcproc_t)procnum, progname, inproc, outproc, __UNCONST("udp"));
333 1.1 fvdl }
334 1.1 fvdl
335 1.1 fvdl /*
336 1.1 fvdl * All the following clnt_broadcast stuff is convulated; it supports
337 1.1 fvdl * the earlier calling style of the callback function
338 1.1 fvdl */
339 1.10 thorpej #ifdef _REENTRANT
340 1.1 fvdl static thread_key_t clnt_broadcast_key;
341 1.1 fvdl #endif
342 1.1 fvdl static resultproc_t clnt_broadcast_result_main;
343 1.1 fvdl
344 1.1 fvdl /*
345 1.1 fvdl * Need to translate the netbuf address into sockaddr_in address.
346 1.1 fvdl * Dont care about netid here.
347 1.1 fvdl */
348 1.1 fvdl /* ARGSUSED */
349 1.1 fvdl static bool_t
350 1.14 matt rpc_wrap_bcast(
351 1.14 matt char *resultp, /* results of the call */
352 1.14 matt struct netbuf *addr, /* address of the guy who responded */
353 1.14 matt struct netconfig *nconf) /* Netconf of the transport */
354 1.1 fvdl {
355 1.1 fvdl resultproc_t clnt_broadcast_result;
356 1.7 lukem
357 1.7 lukem _DIAGASSERT(resultp != NULL);
358 1.7 lukem _DIAGASSERT(addr != NULL);
359 1.7 lukem _DIAGASSERT(nconf != NULL);
360 1.1 fvdl
361 1.1 fvdl if (strcmp(nconf->nc_netid, "udp"))
362 1.1 fvdl return (FALSE);
363 1.10 thorpej #ifdef _REENTRANT
364 1.10 thorpej if (__isthreaded == 0)
365 1.1 fvdl clnt_broadcast_result = clnt_broadcast_result_main;
366 1.1 fvdl else
367 1.10 thorpej clnt_broadcast_result = thr_getspecific(clnt_broadcast_key);
368 1.1 fvdl #else
369 1.1 fvdl clnt_broadcast_result = clnt_broadcast_result_main;
370 1.1 fvdl #endif
371 1.1 fvdl return (*clnt_broadcast_result)(resultp,
372 1.1 fvdl (struct sockaddr_in *)addr->buf);
373 1.1 fvdl }
374 1.1 fvdl
375 1.10 thorpej #ifdef _REENTRANT
376 1.10 thorpej static once_t clnt_broadcast_once = ONCE_INITIALIZER;
377 1.10 thorpej
378 1.10 thorpej static void
379 1.10 thorpej clnt_broadcast_setup(void)
380 1.10 thorpej {
381 1.10 thorpej
382 1.10 thorpej thr_keycreate(&clnt_broadcast_key, free);
383 1.10 thorpej }
384 1.10 thorpej #endif
385 1.10 thorpej
386 1.1 fvdl /*
387 1.1 fvdl * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
388 1.1 fvdl */
389 1.1 fvdl enum clnt_stat
390 1.14 matt clnt_broadcast(
391 1.14 matt u_long prog, /* program number */
392 1.14 matt u_long vers, /* version number */
393 1.14 matt u_long proc, /* procedure number */
394 1.14 matt xdrproc_t xargs, /* xdr routine for args */
395 1.14 matt caddr_t argsp, /* pointer to args */
396 1.14 matt xdrproc_t xresults, /* xdr routine for results */
397 1.14 matt caddr_t resultsp, /* pointer to results */
398 1.14 matt resultproc_t eachresult) /* call with each result obtained */
399 1.1 fvdl {
400 1.10 thorpej #ifdef _REENTRANT
401 1.10 thorpej if (__isthreaded == 0)
402 1.1 fvdl clnt_broadcast_result_main = eachresult;
403 1.1 fvdl else {
404 1.10 thorpej thr_once(&clnt_broadcast_once, clnt_broadcast_setup);
405 1.1 fvdl thr_setspecific(clnt_broadcast_key, (void *) eachresult);
406 1.1 fvdl }
407 1.1 fvdl #else
408 1.1 fvdl clnt_broadcast_result_main = eachresult;
409 1.1 fvdl #endif
410 1.6 christos return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
411 1.6 christos (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
412 1.6 christos (resultproc_t) rpc_wrap_bcast, "udp");
413 1.1 fvdl }
414 1.1 fvdl
415 1.1 fvdl #endif /* PORTMAP */
416