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