2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
28 * @(#)rpc_soc.c 1.17 94/04/24 SMI; 1.41 89/05/02 Copyr 1988 Sun Micro
29 * $NetBSD: rpc_soc.c,v 1.6 2000/07/06 03:10:35 christos Exp $
30 * $FreeBSD: src/lib/libc/rpc/rpc_soc.c,v 1.15 2006/02/27 22:10:59 deischen Exp $
34 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 * In addition, portions of such source code were derived from Berkeley
36 * 4.3 BSD under license from the Regents of the University of
44 * The backward compatibility routines for the earlier implementation
45 * of RPC, where the only transports supported were tcp/ip and udp/ip.
46 * Based on berkeley socket abstraction, now implemented on the top
50 #include "namespace.h"
51 #include "reentrant.h"
52 #include <sys/types.h>
53 #include <sys/socket.h>
56 #include <rpc/pmap_clnt.h>
57 #include <rpc/pmap_prot.h>
58 #include <rpc/nettype.h>
60 #include <netinet/in.h>
66 #include "un-namespace.h"
71 static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t,
72 int *, u_int, u_int, char *);
73 static SVCXPRT *svc_com_create(int, u_int, u_int, char *);
74 static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *);
77 #define IN4_LOCALHOST_STRING "127.0.0.1"
78 #define IN6_LOCALHOST_STRING "::1"
81 * A common clnt create routine
84 clnt_com_create(struct sockaddr_in *raddr, rpcprog_t prog, rpcvers_t vers,
85 int *sockp, u_int sendsz, u_int recvsz, char *tp)
90 struct netconfig *nconf;
91 struct netbuf bindaddr;
93 mutex_lock(&rpcsoc_lock);
94 if ((nconf = __rpc_getconfip(tp)) == NULL) {
95 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
96 mutex_unlock(&rpcsoc_lock);
99 if (fd == RPC_ANYSOCK) {
100 fd = __rpc_nconf2fd(nconf);
106 if (raddr->sin_port == 0) {
110 mutex_unlock(&rpcsoc_lock); /* pmap_getport is recursive */
111 proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
112 sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
117 raddr->sin_port = htons(sport);
118 mutex_lock(&rpcsoc_lock); /* pmap_getport is recursive */
121 /* Transform sockaddr_in to netbuf */
122 bindaddr.maxlen = bindaddr.len = sizeof (struct sockaddr_in);
123 bindaddr.buf = raddr;
125 bindresvport(fd, NULL);
126 cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
129 if (madefd == TRUE) {
131 * The fd should be closed while destroying the handle.
133 CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
136 freenetconfigent(nconf);
137 mutex_unlock(&rpcsoc_lock);
143 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
144 rpc_createerr.cf_error.re_errno = errno;
146 err: if (madefd == TRUE)
148 freenetconfigent(nconf);
149 mutex_unlock(&rpcsoc_lock);
154 clntudp_bufcreate(struct sockaddr_in *raddr, u_long prog, u_long vers,
155 struct timeval wait, int *sockp, u_int sendsz, u_int recvsz)
159 cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
160 sendsz, recvsz, "udp");
164 CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
169 clntudp_create(struct sockaddr_in *raddr, u_long program, u_long version,
170 struct timeval wait, int *sockp)
173 return clntudp_bufcreate(raddr, program, version, wait, sockp,
174 UDPMSGSIZE, UDPMSGSIZE);
178 clnttcp_create(struct sockaddr_in *raddr, u_long prog, u_long vers, int *sockp,
179 u_int sendsz, u_int recvsz)
182 return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
183 sendsz, recvsz, "tcp");
187 clntraw_create(u_long prog, u_long vers)
190 return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
194 * A common server create routine
197 svc_com_create(int fd, u_int sendsize, u_int recvsize, char *netid)
199 struct netconfig *nconf;
203 struct sockaddr_in sin;
205 if ((nconf = __rpc_getconfip(netid)) == NULL) {
206 syslog(LOG_ERR, "Could not get %s transport", netid);
209 if (fd == RPC_ANYSOCK) {
210 fd = __rpc_nconf2fd(nconf);
212 freenetconfigent(nconf);
214 "svc%s_create: could not open connection", netid);
220 memset(&sin, 0, sizeof sin);
221 sin.sin_family = AF_INET;
222 bindresvport(fd, &sin);
223 _listen(fd, SOMAXCONN);
224 svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
225 freenetconfigent(nconf);
231 port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
232 svc->xp_port = ntohs(port);
237 svctcp_create(int fd, u_int sendsize, u_int recvsize)
240 return svc_com_create(fd, sendsize, recvsize, "tcp");
244 svcudp_bufcreate(int fd, u_int sendsz, u_int recvsz)
247 return svc_com_create(fd, sendsz, recvsz, "udp");
251 svcfd_create(int fd, u_int sendsize, u_int recvsize)
254 return svc_fd_create(fd, sendsize, recvsize);
259 svcudp_create(int fd)
262 return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
269 return svc_raw_create();
273 get_myaddress(struct sockaddr_in *addr)
276 memset((void *) addr, 0, sizeof(*addr));
277 addr->sin_family = AF_INET;
278 addr->sin_port = htons(PMAPPORT);
279 addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
284 * For connectionless "udp" transport. Obsoleted by rpc_call().
287 callrpc(const char *host, int prognum, int versnum, int procnum,
288 xdrproc_t inproc, void *in, xdrproc_t outproc, void *out)
291 return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
292 (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
296 * For connectionless kind of transport. Obsoleted by rpc_reg()
299 registerrpc(int prognum, int versnum, int procnum, char *(*progname)(char *),
300 xdrproc_t inproc, xdrproc_t outproc)
303 return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
304 (rpcproc_t)procnum, progname, inproc, outproc, "udp");
308 * All the following clnt_broadcast stuff is convulated; it supports
309 * the earlier calling style of the callback function
311 static thread_key_t clnt_broadcast_key;
312 static resultproc_t clnt_broadcast_result_main;
315 * Need to translate the netbuf address into sockaddr_in address.
316 * Dont care about netid here.
320 rpc_wrap_bcast(char *resultp, /* results of the call */
321 struct netbuf *addr, /* address of the guy who responded */
322 struct netconfig *nconf) /* Netconf of the transport */
324 resultproc_t clnt_broadcast_result;
326 if (strcmp(nconf->nc_netid, "udp"))
329 clnt_broadcast_result = clnt_broadcast_result_main;
331 clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key);
332 return (*clnt_broadcast_result)(resultp,
333 (struct sockaddr_in *)addr->buf);
337 * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
340 clnt_broadcast(u_long prog, /* program number */
341 u_long vers, /* version number */
342 u_long proc, /* procedure number */
343 xdrproc_t xargs, /* xdr routine for args */
344 void *argsp, /* pointer to args */
345 xdrproc_t xresults, /* xdr routine for results */
346 void *resultsp, /* pointer to results */
347 resultproc_t eachresult) /* call with each result obtained */
351 clnt_broadcast_result_main = eachresult;
353 if (clnt_broadcast_key == 0) {
354 mutex_lock(&tsd_lock);
355 if (clnt_broadcast_key == 0)
356 thr_keycreate(&clnt_broadcast_key, free);
357 mutex_unlock(&tsd_lock);
359 thr_setspecific(clnt_broadcast_key, (void *) eachresult);
361 return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
362 (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
363 (resultproc_t) rpc_wrap_bcast, "udp");
367 * Create the client des authentication object. Obsoleted by
368 * authdes_seccreate().
371 authdes_create(char *servername, /* network name of server */
372 u_int window, /* time to live */
373 struct sockaddr *syncaddr,/* optional hostaddr to sync with */
374 des_block *ckey) /* optional conversation key to use */
378 char hostname[NI_MAXHOST];
382 * Change addr to hostname, because that is the way
383 * new interface takes it.
385 if (getnameinfo(syncaddr, syncaddr->sa_len, hostname,
386 sizeof hostname, NULL, 0, 0) != 0)
389 nauth = authdes_seccreate(servername, window, hostname, ckey);
393 dummy = authdes_seccreate(servername, window, NULL, ckey);
398 * Create a client handle for a unix connection. Obsoleted by clnt_vc_create()
401 clntunix_create(struct sockaddr_un *raddr, u_long prog, u_long vers, int *sockp,
402 u_int sendsz, u_int recvsz)
404 struct netbuf *svcaddr;
405 struct netconfig *nconf;
412 if ((raddr->sun_len == 0) ||
413 ((svcaddr = malloc(sizeof(struct netbuf))) == NULL ) ||
414 ((svcaddr->buf = malloc(sizeof(struct sockaddr_un))) == NULL)) {
417 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
418 rpc_createerr.cf_error.re_errno = errno;
422 *sockp = _socket(AF_LOCAL, SOCK_STREAM, 0);
423 len = raddr->sun_len = SUN_LEN(raddr);
424 if ((*sockp < 0) || (_connect(*sockp,
425 (struct sockaddr *)raddr, len) < 0)) {
426 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
427 rpc_createerr.cf_error.re_errno = errno;
433 svcaddr->buf = raddr;
434 svcaddr->len = raddr->sun_len;
435 svcaddr->maxlen = sizeof (struct sockaddr_un);
436 cl = clnt_vc_create(*sockp, svcaddr, prog,
437 vers, sendsz, recvsz);
445 * Creates, registers, and returns a (rpc) unix based transporter.
446 * Obsoleted by svc_vc_create().
449 svcunix_create(int sock, u_int sendsize, u_int recvsize, char *path)
451 struct netconfig *nconf;
453 struct sockaddr_un sun;
460 localhandle = setnetconfig();
461 while ((nconf = getnetconfig(localhandle)) != NULL) {
462 if (nconf->nc_protofmly != NULL &&
463 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
469 if ((sock = __rpc_nconf2fd(nconf)) < 0)
472 memset(&sun, 0, sizeof sun);
473 sun.sun_family = AF_LOCAL;
474 if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
475 sizeof(sun.sun_path))
477 sun.sun_len = SUN_LEN(&sun);
478 addrlen = sizeof (struct sockaddr_un);
479 sa = (struct sockaddr *)&sun;
481 if (_bind(sock, sa, addrlen) < 0)
484 taddr.addr.len = taddr.addr.maxlen = addrlen;
485 taddr.addr.buf = malloc(addrlen);
486 if (taddr.addr.buf == NULL)
488 memcpy(taddr.addr.buf, sa, addrlen);
490 if (nconf->nc_semantics != NC_TPI_CLTS) {
491 if (_listen(sock, SOMAXCONN) < 0) {
492 free(taddr.addr.buf);
497 xprt = (SVCXPRT *)svc_tli_create(sock, nconf, &taddr, sendsize, recvsize);
500 endnetconfig(localhandle);
505 * Like svunix_create(), except the routine takes any *open* UNIX file
506 * descriptor as its first input. Obsoleted by svc_fd_create();
509 svcunixfd_create(int fd, u_int sendsize, u_int recvsize)
511 return (svc_fd_create(fd, sendsize, recvsize));