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