1 /* $NetBSD: svc_generic.c,v 1.3 2000/07/06 03:10:35 christos Exp $ */
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.
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.
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.
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.
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.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
33 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #ident "@(#)svc_generic.c 1.19 94/04/24 SMI"
38 static char sccsid[] = "@(#)svc_generic.c 1.21 89/02/28 Copyr 1988 Sun Micro";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 * svc_generic.c, Server side for RPC.
48 #include "opt_inet6.h"
50 #include <sys/param.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/mutex.h>
55 #include <sys/protosw.h>
56 #include <sys/queue.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/systm.h>
61 #include <sys/ucred.h>
66 #include <rpc/rpcb_clnt.h>
67 #include <rpc/nettype.h>
69 #include <rpc/rpc_com.h>
71 extern int __svc_vc_setflag(SVCXPRT *, int);
74 * The highest level interface for server creation.
75 * It tries for all the nettokens in that particular class of token
76 * and returns the number of handles it can create and/or find.
78 * It creates a link list of all the handles it could create.
79 * If svc_create() is called multiple times, it uses the handle
80 * created earlier instead of creating a new handle every time.
85 void (*dispatch)(struct svc_req *, SVCXPRT *),
86 rpcprog_t prognum, /* Program number */
87 rpcvers_t versnum, /* Version number */
88 const char *nettype) /* Networktype token */
92 struct netconfig *nconf;
95 if ((handle = __rpc_setconf(nettype)) == NULL) {
96 printf("svc_create: unknown protocol");
99 while ((nconf = __rpc_getconf(handle)) != NULL) {
100 mtx_lock(&pool->sp_lock);
101 TAILQ_FOREACH(xprt, &pool->sp_xlist, xp_link) {
102 if (strcmp(xprt->xp_netid, nconf->nc_netid) == 0) {
103 /* Found an old one, use it */
104 mtx_unlock(&pool->sp_lock);
105 (void) rpcb_unset(prognum, versnum, nconf);
106 if (svc_reg(xprt, prognum, versnum,
107 dispatch, nconf) == FALSE) {
109 "svc_create: could not register prog %u vers %u on %s\n",
110 (unsigned)prognum, (unsigned)versnum,
112 mtx_lock(&pool->sp_lock);
115 mtx_lock(&pool->sp_lock);
120 mtx_unlock(&pool->sp_lock);
122 /* It was not found. Now create a new one */
123 xprt = svc_tp_create(pool, dispatch, prognum, versnum,
131 __rpc_endconf(handle);
133 * In case of num == 0; the error messages are generated by the
134 * underlying layers; and hence not needed here.
140 * The high level interface to svc_tli_create().
141 * It tries to create a server for "nconf" and registers the service
142 * with the rpcbind. It calls svc_tli_create();
147 void (*dispatch)(struct svc_req *, SVCXPRT *),
148 rpcprog_t prognum, /* Program number */
149 rpcvers_t versnum, /* Version number */
150 const char *uaddr, /* Address (or null for default) */
151 const struct netconfig *nconf) /* Netconfig structure for the network */
153 struct netconfig nconfcopy;
154 struct netbuf *taddr;
160 "svc_tp_create: invalid netconfig structure for prog %u vers %u\n",
161 (unsigned)prognum, (unsigned)versnum);
165 taddr = uaddr2taddr(nconf, uaddr);
168 bind.qlen = SOMAXCONN;
169 xprt = svc_tli_create(pool, NULL, nconf, &bind, 0, 0);
170 free(bind.addr.buf, M_RPC);
172 xprt = svc_tli_create(pool, NULL, nconf, NULL, 0, 0);
177 /*LINTED const castaway*/
179 (void) rpcb_unset(prognum, versnum, &nconfcopy);
180 if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
182 "svc_tp_create: Could not register prog %u vers %u on %s\n",
183 (unsigned)prognum, (unsigned)versnum,
185 xprt_unregister(xprt);
193 * If so is NULL, then it opens a socket for the given transport
194 * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
195 * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
196 * NULL bindadr and Connection oriented transports, the value of qlen
199 * If sendsz or recvsz are zero, their default values are chosen.
204 struct socket *so, /* Connection end point */
205 const struct netconfig *nconf, /* Netconfig struct for nettoken */
206 const struct t_bind *bindaddr, /* Local bind address */
207 size_t sendsz, /* Max sendsize */
208 size_t recvsz) /* Max recvsize */
210 SVCXPRT *xprt = NULL; /* service handle */
211 bool_t madeso = FALSE; /* whether so opened here */
212 struct __rpc_sockinfo si;
213 struct sockaddr_storage ss;
217 printf("svc_tli_create: invalid netconfig\n");
220 so = __rpc_nconf2socket(nconf);
223 "svc_tli_create: could not open connection for %s\n",
227 __rpc_nconf2sockinfo(nconf, &si);
231 * It is an open socket. Get the transport info.
233 if (!__rpc_socket2sockinfo(so, &si)) {
235 "svc_tli_create: could not get transport information\n");
241 * If the socket is unbound, try to bind it.
243 if (madeso || !__rpc_sockisbound(so)) {
244 if (bindaddr == NULL) {
245 if (bindresvport(so, NULL)) {
246 memset(&ss, 0, sizeof ss);
247 ss.ss_family = si.si_af;
248 ss.ss_len = si.si_alen;
249 if (sobind(so, (struct sockaddr *)&ss,
252 "svc_tli_create: could not bind to anonymous port\n");
256 solisten(so, SOMAXCONN, curthread);
259 (struct sockaddr *)bindaddr->addr.buf)) {
261 "svc_tli_create: could not bind to requested address\n");
264 solisten(so, (int)bindaddr->qlen, curthread);
269 * call transport specific function.
271 switch (si.si_socktype) {
275 if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen)
277 /* accepted socket */
278 xprt = svc_fd_create(fd, sendsz, recvsz);
281 xprt = svc_vc_create(pool, so, sendsz, recvsz);
286 if (strcmp(nconf->nc_protofmly, "inet") == 0 ||
287 strcmp(nconf->nc_protofmly, "inet6") == 0)
288 (void) __svc_vc_setflag(xprt, TRUE);
292 xprt = svc_dg_create(pool, so, sendsz, recvsz);
295 printf("svc_tli_create: bad service type");
301 * The error messages here are spitted out by the lower layers:
302 * svc_vc_create(), svc_fd_create() and svc_dg_create().
306 /* Fill in type of service */
307 xprt->xp_type = __rpc_socktype2seman(si.si_socktype);
310 xprt->xp_netid = strdup(nconf->nc_netid, M_RPC);
318 if (!madeso) /* so that svc_destroy doesnt close fd */
319 xprt->xp_socket = NULL;
320 xprt_unregister(xprt);