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