svc_generic.c revision 331722
1/*	$NetBSD: svc_generic.c,v 1.3 2000/07/06 03:10:35 christos Exp $	*/
2
3/*-
4 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 *   this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 *   this list of conditions and the following disclaimer in the documentation
13 *   and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 *   contributors may be used to endorse or promote products derived
16 *   from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/*
32 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33 */
34
35#if defined(LIBC_SCCS) && !defined(lint)
36#ident	"@(#)svc_generic.c	1.19	94/04/24 SMI"
37static char sccsid[] = "@(#)svc_generic.c 1.21 89/02/28 Copyr 1988 Sun Micro";
38#endif
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: stable/11/lib/libc/rpc/svc_generic.c 331722 2018-03-29 02:50:57Z eadler $");
41
42/*
43 * svc_generic.c, Server side for RPC.
44 *
45 */
46
47#include "namespace.h"
48#include "reentrant.h"
49#include <sys/types.h>
50#include <sys/socket.h>
51#include <netinet/in.h>
52#include <rpc/rpc.h>
53#include <rpc/nettype.h>
54#include <stdio.h>
55#include <errno.h>
56#include <stdlib.h>
57#include <string.h>
58#include <unistd.h>
59#include <err.h>
60#include "un-namespace.h"
61
62#include "rpc_com.h"
63#include "mt_misc.h"
64
65extern int __svc_vc_setflag(SVCXPRT *, int);
66
67/*
68 * The highest level interface for server creation.
69 * It tries for all the nettokens in that particular class of token
70 * and returns the number of handles it can create and/or find.
71 *
72 * It creates a link list of all the handles it could create.
73 * If svc_create() is called multiple times, it uses the handle
74 * created earlier instead of creating a new handle every time.
75 *
76 * prognum - Program number
77 * versnum - Version number
78 * nettype - Networktype token
79 */
80int
81svc_create(void (*dispatch)(struct svc_req *, SVCXPRT *),
82    rpcprog_t prognum, rpcvers_t versnum, const char *nettype)
83{
84	struct xlist {
85		SVCXPRT *xprt;		/* Server handle */
86		struct xlist *next;	/* Next item */
87	} *l;
88	static struct xlist *xprtlist;	/* A link list of all the handles */
89	int num = 0;
90	SVCXPRT *xprt;
91	struct netconfig *nconf;
92	void *handle;
93
94/* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */
95
96	if ((handle = __rpc_setconf(nettype)) == NULL) {
97		warnx("svc_create: unknown protocol");
98		return (0);
99	}
100	while ((nconf = __rpc_getconf(handle)) != NULL) {
101		mutex_lock(&xprtlist_lock);
102		for (l = xprtlist; l; l = l->next) {
103			if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
104				/* Found an old one, use it */
105				(void) rpcb_unset(prognum, versnum, nconf);
106				if (svc_reg(l->xprt, prognum, versnum,
107					dispatch, nconf) == FALSE)
108					warnx(
109		"svc_create: could not register prog %u vers %u on %s",
110					(unsigned)prognum, (unsigned)versnum,
111					 nconf->nc_netid);
112				else
113					num++;
114				break;
115			}
116		}
117		if (l == NULL) {
118			/* It was not found. Now create a new one */
119			xprt = svc_tp_create(dispatch, prognum, versnum, nconf);
120			if (xprt) {
121				l = (struct xlist *)malloc(sizeof (*l));
122				if (l == NULL) {
123					warnx("svc_create: no memory");
124					mutex_unlock(&xprtlist_lock);
125					num = 0;
126					goto done;
127				}
128				l->xprt = xprt;
129				l->next = xprtlist;
130				xprtlist = l;
131				num++;
132			}
133		}
134		mutex_unlock(&xprtlist_lock);
135	}
136done:
137	__rpc_endconf(handle);
138	/*
139	 * In case of num == 0; the error messages are generated by the
140	 * underlying layers; and hence not needed here.
141	 */
142	return (num);
143}
144
145/*
146 * The high level interface to svc_tli_create().
147 * It tries to create a server for "nconf" and registers the service
148 * with the rpcbind. It calls svc_tli_create();
149 *
150 * prognum - Program number
151 * versnum - Version number
152 * ncofn   - Netconfig structure for the network
153 */
154SVCXPRT *
155svc_tp_create(void (*dispatch)(struct svc_req *, SVCXPRT *),
156    rpcprog_t prognum, rpcvers_t versnum, const struct netconfig *nconf)
157{
158	SVCXPRT *xprt;
159
160	if (nconf == NULL) {
161		warnx(
162	"svc_tp_create: invalid netconfig structure for prog %u vers %u",
163				(unsigned)prognum, (unsigned)versnum);
164		return (NULL);
165	}
166	xprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
167	if (xprt == NULL) {
168		return (NULL);
169	}
170	/*LINTED const castaway*/
171	(void) rpcb_unset(prognum, versnum, (struct netconfig *) nconf);
172	if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
173		warnx(
174		"svc_tp_create: Could not register prog %u vers %u on %s",
175				(unsigned)prognum, (unsigned)versnum,
176				nconf->nc_netid);
177		SVC_DESTROY(xprt);
178		return (NULL);
179	}
180	return (xprt);
181}
182
183/*
184 * If fd is RPC_ANYFD, then it opens a fd for the given transport
185 * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
186 * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
187 * NULL bindadr and Connection oriented transports, the value of qlen
188 * is set to 8.
189 *
190 * If sendsz or recvsz are zero, their default values are chosen.
191 *
192 * fd       - Connection end point
193 * nconf    - Netconfig struct for nettoken
194 * bindaddr - Local bind address
195 * sendsz   - Max sendsize
196 * recvxz   - Max recvsize
197 */
198SVCXPRT *
199svc_tli_create(int fd, const struct netconfig *nconf,
200    const struct t_bind *bindaddr, u_int sendsz, u_int recvsz)
201{
202	SVCXPRT *xprt = NULL;		/* service handle */
203	bool_t madefd = FALSE;		/* whether fd opened here  */
204	struct __rpc_sockinfo si;
205	struct sockaddr_storage ss;
206	socklen_t slen;
207
208	if (fd == RPC_ANYFD) {
209		if (nconf == NULL) {
210			warnx("svc_tli_create: invalid netconfig");
211			return (NULL);
212		}
213		fd = __rpc_nconf2fd(nconf);
214		if (fd == -1) {
215			warnx(
216			    "svc_tli_create: could not open connection for %s",
217					nconf->nc_netid);
218			return (NULL);
219		}
220		__rpc_nconf2sockinfo(nconf, &si);
221		madefd = TRUE;
222	} else {
223		/*
224		 * It is an open descriptor. Get the transport info.
225		 */
226		if (!__rpc_fd2sockinfo(fd, &si)) {
227			warnx(
228		"svc_tli_create: could not get transport information");
229			return (NULL);
230		}
231	}
232
233	/*
234	 * If the fd is unbound, try to bind it.
235	 */
236	if (madefd || !__rpc_sockisbound(fd)) {
237		if (bindaddr == NULL) {
238			if (bindresvport(fd, NULL) < 0) {
239				memset(&ss, 0, sizeof ss);
240				ss.ss_family = si.si_af;
241				ss.ss_len = si.si_alen;
242				if (_bind(fd, (struct sockaddr *)(void *)&ss,
243				    (socklen_t)si.si_alen) < 0) {
244					warnx(
245			"svc_tli_create: could not bind to anonymous port");
246					goto freedata;
247				}
248			}
249			_listen(fd, SOMAXCONN);
250		} else {
251			if (_bind(fd,
252			    (struct sockaddr *)bindaddr->addr.buf,
253			    (socklen_t)si.si_alen) < 0) {
254				warnx(
255		"svc_tli_create: could not bind to requested address");
256				goto freedata;
257			}
258			_listen(fd, (int)bindaddr->qlen);
259		}
260
261	}
262	/*
263	 * call transport specific function.
264	 */
265	switch (si.si_socktype) {
266		case SOCK_STREAM:
267			slen = sizeof ss;
268			if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen)
269			    == 0) {
270				/* accepted socket */
271				xprt = svc_fd_create(fd, sendsz, recvsz);
272			} else
273				xprt = svc_vc_create(fd, sendsz, recvsz);
274			if (!nconf || !xprt)
275				break;
276#if 0
277			/* XXX fvdl */
278			if (strcmp(nconf->nc_protofmly, "inet") == 0 ||
279			    strcmp(nconf->nc_protofmly, "inet6") == 0)
280				(void) __svc_vc_setflag(xprt, TRUE);
281#endif
282			break;
283		case SOCK_DGRAM:
284			xprt = svc_dg_create(fd, sendsz, recvsz);
285			break;
286		default:
287			warnx("svc_tli_create: bad service type");
288			goto freedata;
289	}
290
291	if (xprt == NULL)
292		/*
293		 * The error messages here are spitted out by the lower layers:
294		 * svc_vc_create(), svc_fd_create() and svc_dg_create().
295		 */
296		goto freedata;
297
298	/* Fill in type of service */
299	xprt->xp_type = __rpc_socktype2seman(si.si_socktype);
300
301	if (nconf) {
302		xprt->xp_netid = strdup(nconf->nc_netid);
303		xprt->xp_tp = strdup(nconf->nc_device);
304	}
305	return (xprt);
306
307freedata:
308	if (madefd)
309		(void)_close(fd);
310	if (xprt) {
311		if (!madefd) /* so that svc_destroy doesnt close fd */
312			xprt->xp_fd = RPC_ANYFD;
313		SVC_DESTROY(xprt);
314	}
315	return (NULL);
316}
317