NetBSD/lib/libc/rpc/rpc_soc.c

432 lines
10 KiB
C

/* $NetBSD: rpc_soc.c,v 1.5 2000/06/07 01:45:25 fvdl Exp $ */
/*
* Sun RPC is a product of Sun Microsystems, Inc. and is provided for
* unrestricted use provided that this legend is included on all tape
* media and as a part of the software program in whole or part. Users
* may copy or modify Sun RPC without charge, but are not authorized
* to license or distribute it to anyone else except as part of a product or
* program developed by the user.
*
* SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
* WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
*
* Sun RPC is provided with no support and without any obligation on the
* part of Sun Microsystems, Inc. to assist in its use, correction,
* modification or enhancement.
*
* SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
* OR ANY PART THEREOF.
*
* In no event will Sun Microsystems, Inc. be liable for any lost revenue
* or profits or other special, indirect and consequential damages, even if
* Sun has been advised of the possibility of such damages.
*
* Sun Microsystems, Inc.
* 2550 Garcia Avenue
* Mountain View, California 94043
*/
/* #ident "@(#)rpc_soc.c 1.17 94/04/24 SMI" */
/*
* Copyright (c) 1986-1991 by Sun Microsystems Inc.
* In addition, portions of such source code were derived from Berkeley
* 4.3 BSD under license from the Regents of the University of
* California.
*/
#if 0
#if !defined(lint) && defined(SCCSIDS)
static char sccsid[] = "@(#)rpc_soc.c 1.41 89/05/02 Copyr 1988 Sun Micro";
#endif
#endif
#ifdef PORTMAP
/*
* rpc_soc.c
*
* The backward compatibility routines for the earlier implementation
* of RPC, where the only transports supported were tcp/ip and udp/ip.
* Based on berkeley socket abstraction, now implemented on the top
* of TLI/Streams
*/
#include "namespace.h"
#include "reentrant.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <rpc/rpc.h>
#include <rpc/pmap_clnt.h>
#include <rpc/pmap_prot.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <syslog.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "rpc_com.h"
#ifdef __weak_alias
__weak_alias(clntudp_bufcreate,_clntudp_bufcreate)
__weak_alias(clntudp_create,_clntudp_create)
__weak_alias(clnttcp_create,_clnttcp_create)
__weak_alias(clntraw_create,_clntraw_create)
__weak_alias(get_myaddress,_get_myaddress)
__weak_alias(svcfd_create,_svcfd_create)
__weak_alias(svcudp_bufcreate,_svcudp_bufcreate)
__weak_alias(svcudp_create,_svcudp_create)
__weak_alias(svctcp_create,_svctcp_create)
__weak_alias(svcraw_create,_svcraw_create)
__weak_alias(callrpc,_callrpc)
__weak_alias(registerrpc,_registerrpc)
__weak_alias(clnt_broadcast,_clnt_broadcast)
#endif
#ifdef __REENT
extern mutex_t rpcsoc_lock;
#endif
static CLIENT *clnt_com_create __P((struct sockaddr_in *, rpcprog_t, rpcvers_t,
int *, u_int, u_int, char *));
static SVCXPRT *svc_com_create __P((int, u_int, u_int, char *));
static bool_t rpc_wrap_bcast __P((char *, struct netbuf *, struct netconfig *));
/*
* A common clnt create routine
*/
static CLIENT *
clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, tp)
struct sockaddr_in *raddr;
rpcprog_t prog;
rpcvers_t vers;
int *sockp;
u_int sendsz;
u_int recvsz;
char *tp;
{
CLIENT *cl;
int madefd = FALSE;
int fd = *sockp;
struct netconfig *nconf;
struct netbuf bindaddr;
mutex_lock(&rpcsoc_lock);
if ((nconf = __rpc_getconfip(tp)) == NULL) {
rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
mutex_unlock(&rpcsoc_lock);
return ((CLIENT *)NULL);
}
if (fd == RPC_ANYSOCK) {
fd = __rpc_nconf2fd(nconf);
if (fd == -1)
goto syserror;
madefd = TRUE;
}
if (raddr->sin_port == 0) {
u_int proto;
u_short sport;
mutex_unlock(&rpcsoc_lock); /* pmap_getport is recursive */
proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
sport = pmap_getport(raddr, prog, vers, proto);
if (sport == 0) {
goto err;
}
raddr->sin_port = htons(sport);
mutex_lock(&rpcsoc_lock); /* pmap_getport is recursive */
}
/* Transform sockaddr_in to netbuf */
bindaddr.maxlen = bindaddr.len = sizeof (struct sockaddr_in);
bindaddr.buf = raddr;
bindresvport(fd, NULL);
cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
sendsz, recvsz);
if (cl) {
if (madefd == TRUE) {
/*
* The fd should be closed while destroying the handle.
*/
(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, (char *)NULL);
*sockp = fd;
}
(void) freenetconfigent(nconf);
mutex_unlock(&rpcsoc_lock);
return (cl);
}
goto err;
syserror:
rpc_createerr.cf_stat = RPC_SYSTEMERROR;
rpc_createerr.cf_error.re_errno = errno;
err: if (madefd == TRUE)
(void) close(fd);
(void) freenetconfigent(nconf);
mutex_unlock(&rpcsoc_lock);
return ((CLIENT *)NULL);
}
CLIENT *
clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz)
register struct sockaddr_in *raddr;
u_long prog;
u_long vers;
struct timeval wait;
int *sockp;
u_int sendsz;
u_int recvsz;
{
CLIENT *cl;
cl = clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, "udp");
if (cl == (CLIENT *)NULL) {
return ((CLIENT *)NULL);
}
(void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, (char *)&wait);
return (cl);
}
CLIENT *
clntudp_create(raddr, program, version, wait, sockp)
struct sockaddr_in *raddr;
u_long program;
u_long version;
struct timeval wait;
int *sockp;
{
return clntudp_bufcreate(raddr, program, version, wait, sockp,
UDPMSGSIZE, UDPMSGSIZE);
}
CLIENT *
clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
struct sockaddr_in *raddr;
u_long prog;
u_long vers;
register int *sockp;
u_int sendsz;
u_int recvsz;
{
return clnt_com_create(raddr, prog, vers, sockp, sendsz,
recvsz, "tcp");
}
CLIENT *
clntraw_create(prog, vers)
u_long prog;
u_long vers;
{
return clnt_raw_create(prog, vers);
}
/*
* A common server create routine
*/
static SVCXPRT *
svc_com_create(fd, sendsize, recvsize, netid)
register int fd;
u_int sendsize;
u_int recvsize;
char *netid;
{
struct netconfig *nconf;
SVCXPRT *svc;
int madefd = FALSE;
int port;
struct sockaddr_in sin;
if ((nconf = __rpc_getconfip(netid)) == NULL) {
(void) syslog(LOG_ERR, "Could not get %s transport", netid);
return ((SVCXPRT *)NULL);
}
if (fd == RPC_ANYSOCK) {
fd = __rpc_nconf2fd(nconf);
if (fd == -1) {
(void) freenetconfigent(nconf);
(void) syslog(LOG_ERR,
"svc%s_create: could not open connection", netid);
return ((SVCXPRT *)NULL);
}
madefd = TRUE;
}
memset(&sin, 0, sizeof sin);
sin.sin_family = AF_INET;
bindresvport(fd, &sin);
listen(fd, SOMAXCONN);
svc = svc_tli_create(fd, nconf, (struct t_bind *)NULL,
sendsize, recvsize);
(void) freenetconfigent(nconf);
if (svc == (SVCXPRT *)NULL) {
if (madefd)
(void) close(fd);
return ((SVCXPRT *)NULL);
}
port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
svc->xp_port = ntohs(port);
return (svc);
}
SVCXPRT *
svctcp_create(fd, sendsize, recvsize)
register int fd;
u_int sendsize;
u_int recvsize;
{
return svc_com_create(fd, sendsize, recvsize, "tcp");
}
SVCXPRT *
svcudp_bufcreate(fd, sendsz, recvsz)
register int fd;
u_int sendsz, recvsz;
{
return svc_com_create(fd, sendsz, recvsz, "udp");
}
SVCXPRT *
svcfd_create(fd, sendsize, recvsize)
int fd;
u_int sendsize;
u_int recvsize;
{
return svc_fd_create(fd, sendsize, recvsize);
}
SVCXPRT *
svcudp_create(fd)
register int fd;
{
return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
}
SVCXPRT *
svcraw_create()
{
return svc_raw_create();
}
int
get_myaddress(addr)
struct sockaddr_in *addr;
{
memset((void *) addr, 0, sizeof(*addr));
addr->sin_family = AF_INET;
addr->sin_port = htons(PMAPPORT);
addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
return (0);
}
/*
* For connectionless "udp" transport. Obsoleted by rpc_call().
*/
int
callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
char *host;
int prognum, versnum, procnum;
xdrproc_t inproc, outproc;
char *in, *out;
{
return (int)rpc_call(host, (u_long)prognum, (u_long)versnum,
(u_long)procnum, inproc, in, outproc, out, "udp");
}
/*
* For connectionless kind of transport. Obsoleted by rpc_reg()
*/
int
registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
int prognum, versnum, procnum;
char *(*progname) __P((char [UDPMSGSIZE]));
xdrproc_t inproc, outproc;
{
return rpc_reg((u_long)prognum, (u_long)versnum, (u_long)procnum,
progname, inproc, outproc, "udp");
}
/*
* All the following clnt_broadcast stuff is convulated; it supports
* the earlier calling style of the callback function
*/
#ifdef __REENT
static thread_key_t clnt_broadcast_key;
#endif
static resultproc_t clnt_broadcast_result_main;
/*
* Need to translate the netbuf address into sockaddr_in address.
* Dont care about netid here.
*/
/* ARGSUSED */
static bool_t
rpc_wrap_bcast(resultp, addr, nconf)
char *resultp; /* results of the call */
struct netbuf *addr; /* address of the guy who responded */
struct netconfig *nconf; /* Netconf of the transport */
{
resultproc_t clnt_broadcast_result;
if (strcmp(nconf->nc_netid, "udp"))
return (FALSE);
#ifdef __REENT
if (_thr_main())
clnt_broadcast_result = clnt_broadcast_result_main;
else
thr_getspecific(clnt_broadcast_key,
(void **) &clnt_broadcast_result);
#else
clnt_broadcast_result = clnt_broadcast_result_main;
#endif
return (*clnt_broadcast_result)(resultp,
(struct sockaddr_in *)addr->buf);
}
/*
* Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
*/
enum clnt_stat
clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
u_long prog; /* program number */
u_long vers; /* version number */
u_long proc; /* procedure number */
xdrproc_t xargs; /* xdr routine for args */
caddr_t argsp; /* pointer to args */
xdrproc_t xresults; /* xdr routine for results */
caddr_t resultsp; /* pointer to results */
resultproc_t eachresult; /* call with each result obtained */
{
#ifdef __REENT
extern mutex_t tsd_lock;
#endif
#ifdef __REENT
if (_thr_main())
clnt_broadcast_result_main = eachresult;
else {
if (clnt_broadcast_key == 0) {
mutex_lock(&tsd_lock);
if (clnt_broadcast_key == 0)
thr_keycreate(&clnt_broadcast_key, free);
mutex_unlock(&tsd_lock);
}
thr_setspecific(clnt_broadcast_key, (void *) eachresult);
}
#else
clnt_broadcast_result_main = eachresult;
#endif
return rpc_broadcast(prog, vers, proc, xargs, argsp, xresults,
resultsp, (resultproc_t) rpc_wrap_bcast, "udp");
}
#endif /* PORTMAP */