Home | History | Annotate | Line # | Download | only in rpc
svc_generic.c revision 1.1
      1  1.1  fvdl /*	$NetBSD: svc_generic.c,v 1.1 2000/06/02 23:11:16 fvdl 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 /*
     33  1.1  fvdl  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
     34  1.1  fvdl  */
     35  1.1  fvdl 
     36  1.1  fvdl /* #ident	"@(#)svc_generic.c	1.19	94/04/24 SMI" */
     37  1.1  fvdl 
     38  1.1  fvdl #if 0
     39  1.1  fvdl #if !defined(lint) && defined(SCCSIDS)
     40  1.1  fvdl static char sccsid[] = "@(#)svc_generic.c 1.21 89/02/28 Copyr 1988 Sun Micro";
     41  1.1  fvdl #endif
     42  1.1  fvdl #endif
     43  1.1  fvdl 
     44  1.1  fvdl /*
     45  1.1  fvdl  * svc_generic.c, Server side for RPC.
     46  1.1  fvdl  *
     47  1.1  fvdl  */
     48  1.1  fvdl 
     49  1.1  fvdl #include "namespace.h"
     50  1.1  fvdl #include "reentrant.h"
     51  1.1  fvdl #include <sys/types.h>
     52  1.1  fvdl #include <sys/socket.h>
     53  1.1  fvdl #include <netinet/in.h>
     54  1.1  fvdl #include <rpc/rpc.h>
     55  1.1  fvdl #include <stdio.h>
     56  1.1  fvdl #include <errno.h>
     57  1.1  fvdl #include <malloc.h>
     58  1.1  fvdl #include <string.h>
     59  1.1  fvdl #include <unistd.h>
     60  1.1  fvdl #include <err.h>
     61  1.1  fvdl 
     62  1.1  fvdl #include "rpc_com.h"
     63  1.1  fvdl 
     64  1.1  fvdl #ifdef __weak_alias
     65  1.1  fvdl __weak_alias(svc_create,_svc_create)
     66  1.1  fvdl __weak_alias(svc_tp_create,_svc_tp_create)
     67  1.1  fvdl __weak_alias(svc_tli_create,_svc_tli_create)
     68  1.1  fvdl #endif
     69  1.1  fvdl 
     70  1.1  fvdl extern int __svc_vc_setflag __P((SVCXPRT *, int));
     71  1.1  fvdl 
     72  1.1  fvdl /*
     73  1.1  fvdl  * The highest level interface for server creation.
     74  1.1  fvdl  * It tries for all the nettokens in that particular class of token
     75  1.1  fvdl  * and returns the number of handles it can create and/or find.
     76  1.1  fvdl  *
     77  1.1  fvdl  * It creates a link list of all the handles it could create.
     78  1.1  fvdl  * If svc_create() is called multiple times, it uses the handle
     79  1.1  fvdl  * created earlier instead of creating a new handle every time.
     80  1.1  fvdl  */
     81  1.1  fvdl int
     82  1.1  fvdl svc_create(dispatch, prognum, versnum, nettype)
     83  1.1  fvdl 	void (*dispatch) __P((struct svc_req *, SVCXPRT *));
     84  1.1  fvdl 	rpcprog_t prognum;		/* Program number */
     85  1.1  fvdl 	rpcvers_t versnum;		/* Version number */
     86  1.1  fvdl 	const char *nettype;		/* Networktype token */
     87  1.1  fvdl {
     88  1.1  fvdl 	struct xlist {
     89  1.1  fvdl 		SVCXPRT *xprt;		/* Server handle */
     90  1.1  fvdl 		struct xlist *next;	/* Next item */
     91  1.1  fvdl 	} *l;
     92  1.1  fvdl 	static struct xlist *xprtlist;	/* A link list of all the handles */
     93  1.1  fvdl 	int num = 0;
     94  1.1  fvdl 	SVCXPRT *xprt;
     95  1.1  fvdl 	struct netconfig *nconf;
     96  1.1  fvdl 	void *handle;
     97  1.1  fvdl #ifdef __REENT
     98  1.1  fvdl 	extern mutex_t xprtlist_lock;
     99  1.1  fvdl #endif
    100  1.1  fvdl 
    101  1.1  fvdl /* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */
    102  1.1  fvdl 
    103  1.1  fvdl 	if ((handle = __rpc_setconf((char *)nettype)) == NULL) {
    104  1.1  fvdl 		warnx("svc_create: unknown protocol");
    105  1.1  fvdl 		return (0);
    106  1.1  fvdl 	}
    107  1.1  fvdl 	while ((nconf = __rpc_getconf(handle))) {
    108  1.1  fvdl 		mutex_lock(&xprtlist_lock);
    109  1.1  fvdl 		for (l = xprtlist; l; l = l->next) {
    110  1.1  fvdl 			if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
    111  1.1  fvdl 				/* Found an old one, use it */
    112  1.1  fvdl 				(void) rpcb_unset(prognum, versnum, nconf);
    113  1.1  fvdl 				if (svc_reg(l->xprt, prognum, versnum,
    114  1.1  fvdl 					dispatch, nconf) == FALSE)
    115  1.1  fvdl 					warnx(
    116  1.1  fvdl 		"svc_create: could not register prog %u vers %u on %s",
    117  1.1  fvdl 					(unsigned)prognum, (unsigned)versnum,
    118  1.1  fvdl 					 nconf->nc_netid);
    119  1.1  fvdl 				else
    120  1.1  fvdl 					num++;
    121  1.1  fvdl 				break;
    122  1.1  fvdl 			}
    123  1.1  fvdl 		}
    124  1.1  fvdl 		if (l == (struct xlist *)NULL) {
    125  1.1  fvdl 			/* It was not found. Now create a new one */
    126  1.1  fvdl 			xprt = svc_tp_create(dispatch, prognum, versnum, nconf);
    127  1.1  fvdl 			if (xprt) {
    128  1.1  fvdl 				l = (struct xlist *)malloc(sizeof (*l));
    129  1.1  fvdl 				if (l == (struct xlist *)NULL) {
    130  1.1  fvdl 					warnx("svc_create: no memory");
    131  1.1  fvdl 					mutex_unlock(&xprtlist_lock);
    132  1.1  fvdl 					return (0);
    133  1.1  fvdl 				}
    134  1.1  fvdl 				l->xprt = xprt;
    135  1.1  fvdl 				l->next = xprtlist;
    136  1.1  fvdl 				xprtlist = l;
    137  1.1  fvdl 				num++;
    138  1.1  fvdl 			}
    139  1.1  fvdl 		}
    140  1.1  fvdl 		mutex_unlock(&xprtlist_lock);
    141  1.1  fvdl 	}
    142  1.1  fvdl 	__rpc_endconf(handle);
    143  1.1  fvdl 	/*
    144  1.1  fvdl 	 * In case of num == 0; the error messages are generated by the
    145  1.1  fvdl 	 * underlying layers; and hence not needed here.
    146  1.1  fvdl 	 */
    147  1.1  fvdl 	return (num);
    148  1.1  fvdl }
    149  1.1  fvdl 
    150  1.1  fvdl /*
    151  1.1  fvdl  * The high level interface to svc_tli_create().
    152  1.1  fvdl  * It tries to create a server for "nconf" and registers the service
    153  1.1  fvdl  * with the rpcbind. It calls svc_tli_create();
    154  1.1  fvdl  */
    155  1.1  fvdl SVCXPRT *
    156  1.1  fvdl svc_tp_create(dispatch, prognum, versnum, nconf)
    157  1.1  fvdl 	void (*dispatch) __P((struct svc_req *, SVCXPRT *));
    158  1.1  fvdl 	rpcprog_t prognum;		/* Program number */
    159  1.1  fvdl 	rpcvers_t versnum;		/* Version number */
    160  1.1  fvdl 	const struct netconfig *nconf; /* Netconfig structure for the network */
    161  1.1  fvdl {
    162  1.1  fvdl 	SVCXPRT *xprt;
    163  1.1  fvdl 
    164  1.1  fvdl 	if (nconf == (struct netconfig *)NULL) {
    165  1.1  fvdl 		warnx(
    166  1.1  fvdl 	"svc_tp_create: invalid netconfig structure for prog %u vers %u",
    167  1.1  fvdl 				(unsigned)prognum, (unsigned)versnum);
    168  1.1  fvdl 		return ((SVCXPRT *)NULL);
    169  1.1  fvdl 	}
    170  1.1  fvdl 	xprt = svc_tli_create(RPC_ANYFD, nconf, (struct t_bind *)NULL, 0, 0);
    171  1.1  fvdl 	if (xprt == (SVCXPRT *)NULL) {
    172  1.1  fvdl 		return ((SVCXPRT *)NULL);
    173  1.1  fvdl 	}
    174  1.1  fvdl 	(void) rpcb_unset(prognum, versnum, (struct netconfig *) nconf);
    175  1.1  fvdl 	if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
    176  1.1  fvdl 		warnx(
    177  1.1  fvdl 		"svc_tp_create: Could not register prog %u vers %u on %s",
    178  1.1  fvdl 				(unsigned)prognum, (unsigned)versnum,
    179  1.1  fvdl 				nconf->nc_netid);
    180  1.1  fvdl 		SVC_DESTROY(xprt);
    181  1.1  fvdl 		return ((SVCXPRT *)NULL);
    182  1.1  fvdl 	}
    183  1.1  fvdl 	return (xprt);
    184  1.1  fvdl }
    185  1.1  fvdl 
    186  1.1  fvdl /*
    187  1.1  fvdl  * If fd is RPC_ANYFD, then it opens a fd for the given transport
    188  1.1  fvdl  * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
    189  1.1  fvdl  * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
    190  1.1  fvdl  * NULL bindadr and Connection oriented transports, the value of qlen
    191  1.1  fvdl  * is set to 8.
    192  1.1  fvdl  *
    193  1.1  fvdl  * If sendsz or recvsz are zero, their default values are chosen.
    194  1.1  fvdl  */
    195  1.1  fvdl SVCXPRT *
    196  1.1  fvdl svc_tli_create(fd, nconf, bindaddr, sendsz, recvsz)
    197  1.1  fvdl 	int fd;				/* Connection end point */
    198  1.1  fvdl 	const struct netconfig *nconf;	/* Netconfig struct for nettoken */
    199  1.1  fvdl 	const struct t_bind *bindaddr;	/* Local bind address */
    200  1.1  fvdl 	u_int sendsz;			/* Max sendsize */
    201  1.1  fvdl 	u_int recvsz;			/* Max recvsize */
    202  1.1  fvdl {
    203  1.1  fvdl 	register SVCXPRT *xprt = NULL;	/* service handle */
    204  1.1  fvdl 	bool_t madefd = FALSE;		/* whether fd opened here  */
    205  1.1  fvdl 	struct __rpc_sockinfo si;
    206  1.1  fvdl 	struct sockaddr_storage ss;
    207  1.1  fvdl 	int active = 0;
    208  1.1  fvdl 	socklen_t slen;
    209  1.1  fvdl 
    210  1.1  fvdl 	if (fd == RPC_ANYFD) {
    211  1.1  fvdl 		if (nconf == (struct netconfig *)NULL) {
    212  1.1  fvdl 			warnx("svc_tli_create: invalid netconfig");
    213  1.1  fvdl 			return ((SVCXPRT *)NULL);
    214  1.1  fvdl 		}
    215  1.1  fvdl 		fd = __rpc_nconf2fd(nconf);
    216  1.1  fvdl 		if (fd == -1) {
    217  1.1  fvdl 			warnx(
    218  1.1  fvdl 			    "svc_tli_create: could not open connection for %s",
    219  1.1  fvdl 					nconf->nc_netid);
    220  1.1  fvdl 			return ((SVCXPRT *)NULL);
    221  1.1  fvdl 		}
    222  1.1  fvdl 		__rpc_nconf2sockinfo(nconf, &si);
    223  1.1  fvdl 		madefd = TRUE;
    224  1.1  fvdl 	} else {
    225  1.1  fvdl 		/*
    226  1.1  fvdl 		 * It is an open descriptor. Get the transport info.
    227  1.1  fvdl 		 */
    228  1.1  fvdl 		if (!__rpc_fd2sockinfo(fd, &si)) {
    229  1.1  fvdl 			warnx(
    230  1.1  fvdl 		"svc_tli_create: could not get transport information");
    231  1.1  fvdl 			return ((SVCXPRT *)NULL);
    232  1.1  fvdl 		}
    233  1.1  fvdl 	}
    234  1.1  fvdl 
    235  1.1  fvdl 	/*
    236  1.1  fvdl 	 * If the fd is unbound, try to bind it.
    237  1.1  fvdl 	 */
    238  1.1  fvdl 	if (madefd || !__rpc_sockisbound(fd)) {
    239  1.1  fvdl 		if (bindaddr == NULL) {
    240  1.1  fvdl 			memset(&ss, 0, sizeof ss);
    241  1.1  fvdl 			ss.ss_family = si.si_af;
    242  1.1  fvdl 			ss.ss_len = si.si_alen;
    243  1.1  fvdl 			if (bind(fd, (struct sockaddr *)(void *)&ss,
    244  1.1  fvdl 			    si.si_alen) < 0) {
    245  1.1  fvdl 				warnx(
    246  1.1  fvdl 		"svc_tli_create: could not bind to anonymous port");
    247  1.1  fvdl 				goto freedata;
    248  1.1  fvdl 			}
    249  1.1  fvdl 			listen(fd, SOMAXCONN);
    250  1.1  fvdl 		} else {
    251  1.1  fvdl 			if (bind(fd, (struct sockaddr *)&bindaddr->addr.buf,
    252  1.1  fvdl 			    si.si_alen) < 0) {
    253  1.1  fvdl 				warnx(
    254  1.1  fvdl 		"svc_tli_create: could not bind to requested address");
    255  1.1  fvdl 				goto freedata;
    256  1.1  fvdl 			}
    257  1.1  fvdl 			listen(fd, bindaddr->qlen);
    258  1.1  fvdl 		}
    259  1.1  fvdl 
    260  1.1  fvdl 	}
    261  1.1  fvdl 	/*
    262  1.1  fvdl 	 * call transport specific function.
    263  1.1  fvdl 	 */
    264  1.1  fvdl 	switch (si.si_socktype) {
    265  1.1  fvdl 		case SOCK_STREAM:
    266  1.1  fvdl 			slen = sizeof ss;
    267  1.1  fvdl 			if (getpeername(fd, (struct sockaddr *)&ss, &slen)
    268  1.1  fvdl 			    == 0) {
    269  1.1  fvdl 				active = 1;
    270  1.1  fvdl 				/* accepted socket */
    271  1.1  fvdl 				xprt = svc_fd_create(fd, sendsz, recvsz);
    272  1.1  fvdl 			} else
    273  1.1  fvdl 				xprt = svc_vc_create(fd, sendsz, recvsz);
    274  1.1  fvdl 			if (!nconf || !xprt)
    275  1.1  fvdl 				break;
    276  1.1  fvdl #if 0
    277  1.1  fvdl 			/* XXX fvdl */
    278  1.1  fvdl 			if (strcmp(nconf->nc_protofmly, "inet") == 0 ||
    279  1.1  fvdl 			    strcmp(nconf->nc_protofmly, "inet6") == 0)
    280  1.1  fvdl 				(void) __svc_vc_setflag(xprt, TRUE);
    281  1.1  fvdl #endif
    282  1.1  fvdl 			break;
    283  1.1  fvdl 		case SOCK_DGRAM:
    284  1.1  fvdl 			xprt = svc_dg_create(fd, sendsz, recvsz);
    285  1.1  fvdl 			break;
    286  1.1  fvdl 		default:
    287  1.1  fvdl 			warnx("svc_tli_create: bad service type");
    288  1.1  fvdl 			goto freedata;
    289  1.1  fvdl 	}
    290  1.1  fvdl 
    291  1.1  fvdl 	if (xprt == (SVCXPRT *)NULL)
    292  1.1  fvdl 		/*
    293  1.1  fvdl 		 * The error messages here are spitted out by the lower layers:
    294  1.1  fvdl 		 * svc_vc_create(), svc_fd_create() and svc_dg_create().
    295  1.1  fvdl 		 */
    296  1.1  fvdl 		goto freedata;
    297  1.1  fvdl 
    298  1.1  fvdl 	/* Fill in type of service */
    299  1.1  fvdl 	xprt->xp_type = __rpc_socktype2seman(si.si_socktype);
    300  1.1  fvdl 
    301  1.1  fvdl 	if (nconf) {
    302  1.1  fvdl 		xprt->xp_netid = strdup(nconf->nc_netid);
    303  1.1  fvdl 		xprt->xp_tp = strdup(nconf->nc_device);
    304  1.1  fvdl 	}
    305  1.1  fvdl 	return (xprt);
    306  1.1  fvdl 
    307  1.1  fvdl freedata:
    308  1.1  fvdl 	if (madefd)
    309  1.1  fvdl 		(void) close(fd);
    310  1.1  fvdl 	if (xprt) {
    311  1.1  fvdl 		if (!madefd) /* so that svc_destroy doesnt close fd */
    312  1.1  fvdl 			xprt->xp_fd = RPC_ANYFD;
    313  1.1  fvdl 		SVC_DESTROY(xprt);
    314  1.1  fvdl 	}
    315  1.1  fvdl 	return ((SVCXPRT *)NULL);
    316  1.1  fvdl }
    317