xref: /freebsd/lib/libc/rpc/svc_generic.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1 /*	$NetBSD: svc_generic.c,v 1.3 2000/07/06 03:10:35 christos Exp $	*/
2 /*	$FreeBSD$ */
3 
4 /*
5  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
6  * unrestricted use provided that this legend is included on all tape
7  * media and as a part of the software program in whole or part.  Users
8  * may copy or modify Sun RPC without charge, but are not authorized
9  * to license or distribute it to anyone else except as part of a product or
10  * program developed by the user.
11  *
12  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15  *
16  * Sun RPC is provided with no support and without any obligation on the
17  * part of Sun Microsystems, Inc. to assist in its use, correction,
18  * modification or enhancement.
19  *
20  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22  * OR ANY PART THEREOF.
23  *
24  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25  * or profits or other special, indirect and consequential damages, even if
26  * Sun has been advised of the possibility of such damages.
27  *
28  * Sun Microsystems, Inc.
29  * 2550 Garcia Avenue
30  * Mountain View, California  94043
31  */
32 
33 /*
34  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35  */
36 
37 /* #ident	"@(#)svc_generic.c	1.19	94/04/24 SMI" */
38 
39 #if 0
40 #if !defined(lint) && defined(SCCSIDS)
41 static char sccsid[] = "@(#)svc_generic.c 1.21 89/02/28 Copyr 1988 Sun Micro";
42 #endif
43 #endif
44 
45 /*
46  * svc_generic.c, Server side for RPC.
47  *
48  */
49 
50 #include "namespace.h"
51 #include "reentrant.h"
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <rpc/rpc.h>
56 #include <rpc/nettype.h>
57 #include <stdio.h>
58 #include <errno.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <err.h>
63 #include "un-namespace.h"
64 
65 #include "rpc_com.h"
66 
67 extern int __svc_vc_setflag __P((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 int
79 svc_create(dispatch, prognum, versnum, nettype)
80 	void (*dispatch) __P((struct svc_req *, SVCXPRT *));
81 	rpcprog_t prognum;		/* Program number */
82 	rpcvers_t versnum;		/* Version number */
83 	const char *nettype;		/* Networktype token */
84 {
85 	struct xlist {
86 		SVCXPRT *xprt;		/* Server handle */
87 		struct xlist *next;	/* Next item */
88 	} *l;
89 	static struct xlist *xprtlist;	/* A link list of all the handles */
90 	int num = 0;
91 	SVCXPRT *xprt;
92 	struct netconfig *nconf;
93 	void *handle;
94 	extern mutex_t xprtlist_lock;
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 					return (0);
128 				}
129 				l->xprt = xprt;
130 				l->next = xprtlist;
131 				xprtlist = l;
132 				num++;
133 			}
134 		}
135 		mutex_unlock(&xprtlist_lock);
136 	}
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 SVCXPRT *
151 svc_tp_create(dispatch, prognum, versnum, nconf)
152 	void (*dispatch) __P((struct svc_req *, SVCXPRT *));
153 	rpcprog_t prognum;		/* Program number */
154 	rpcvers_t versnum;		/* Version number */
155 	const struct netconfig *nconf; /* Netconfig structure for the network */
156 {
157 	SVCXPRT *xprt;
158 
159 	if (nconf == NULL) {
160 		warnx(
161 	"svc_tp_create: invalid netconfig structure for prog %u vers %u",
162 				(unsigned)prognum, (unsigned)versnum);
163 		return (NULL);
164 	}
165 	xprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
166 	if (xprt == NULL) {
167 		return (NULL);
168 	}
169 	/*LINTED const castaway*/
170 	(void) rpcb_unset(prognum, versnum, (struct netconfig *) nconf);
171 	if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
172 		warnx(
173 		"svc_tp_create: Could not register prog %u vers %u on %s",
174 				(unsigned)prognum, (unsigned)versnum,
175 				nconf->nc_netid);
176 		SVC_DESTROY(xprt);
177 		return (NULL);
178 	}
179 	return (xprt);
180 }
181 
182 /*
183  * If fd is RPC_ANYFD, then it opens a fd for the given transport
184  * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
185  * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
186  * NULL bindadr and Connection oriented transports, the value of qlen
187  * is set to 8.
188  *
189  * If sendsz or recvsz are zero, their default values are chosen.
190  */
191 SVCXPRT *
192 svc_tli_create(fd, nconf, bindaddr, sendsz, recvsz)
193 	int fd;				/* Connection end point */
194 	const struct netconfig *nconf;	/* Netconfig struct for nettoken */
195 	const struct t_bind *bindaddr;	/* Local bind address */
196 	u_int sendsz;			/* Max sendsize */
197 	u_int recvsz;			/* Max recvsize */
198 {
199 	SVCXPRT *xprt = NULL;		/* service handle */
200 	bool_t madefd = FALSE;		/* whether fd opened here  */
201 	struct __rpc_sockinfo si;
202 	struct sockaddr_storage ss;
203 	socklen_t slen;
204 
205 	if (fd == RPC_ANYFD) {
206 		if (nconf == NULL) {
207 			warnx("svc_tli_create: invalid netconfig");
208 			return (NULL);
209 		}
210 		fd = __rpc_nconf2fd(nconf);
211 		if (fd == -1) {
212 			warnx(
213 			    "svc_tli_create: could not open connection for %s",
214 					nconf->nc_netid);
215 			return (NULL);
216 		}
217 		__rpc_nconf2sockinfo(nconf, &si);
218 		madefd = TRUE;
219 	} else {
220 		/*
221 		 * It is an open descriptor. Get the transport info.
222 		 */
223 		if (!__rpc_fd2sockinfo(fd, &si)) {
224 			warnx(
225 		"svc_tli_create: could not get transport information");
226 			return (NULL);
227 		}
228 	}
229 
230 	/*
231 	 * If the fd is unbound, try to bind it.
232 	 */
233 	if (madefd || !__rpc_sockisbound(fd)) {
234 		if (bindaddr == NULL) {
235 			if (bindresvport(fd, NULL) < 0) {
236 				memset(&ss, 0, sizeof ss);
237 				ss.ss_family = si.si_af;
238 				ss.ss_len = si.si_alen;
239 				if (_bind(fd, (struct sockaddr *)(void *)&ss,
240 				    (socklen_t)si.si_alen) < 0) {
241 					warnx(
242 			"svc_tli_create: could not bind to anonymous port");
243 					goto freedata;
244 				}
245 			}
246 			_listen(fd, SOMAXCONN);
247 		} else {
248 			if (_bind(fd,
249 			    (struct sockaddr *)(void *)&bindaddr->addr.buf,
250 			    (socklen_t)si.si_alen) < 0) {
251 				warnx(
252 		"svc_tli_create: could not bind to requested address");
253 				goto freedata;
254 			}
255 			_listen(fd, (int)bindaddr->qlen);
256 		}
257 
258 	}
259 	/*
260 	 * call transport specific function.
261 	 */
262 	switch (si.si_socktype) {
263 		case SOCK_STREAM:
264 			slen = sizeof ss;
265 			if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen)
266 			    == 0) {
267 				/* accepted socket */
268 				xprt = svc_fd_create(fd, sendsz, recvsz);
269 			} else
270 				xprt = svc_vc_create(fd, sendsz, recvsz);
271 			if (!nconf || !xprt)
272 				break;
273 #if 0
274 			/* XXX fvdl */
275 			if (strcmp(nconf->nc_protofmly, "inet") == 0 ||
276 			    strcmp(nconf->nc_protofmly, "inet6") == 0)
277 				(void) __svc_vc_setflag(xprt, TRUE);
278 #endif
279 			break;
280 		case SOCK_DGRAM:
281 			xprt = svc_dg_create(fd, sendsz, recvsz);
282 			break;
283 		default:
284 			warnx("svc_tli_create: bad service type");
285 			goto freedata;
286 	}
287 
288 	if (xprt == NULL)
289 		/*
290 		 * The error messages here are spitted out by the lower layers:
291 		 * svc_vc_create(), svc_fd_create() and svc_dg_create().
292 		 */
293 		goto freedata;
294 
295 	/* Fill in type of service */
296 	xprt->xp_type = __rpc_socktype2seman(si.si_socktype);
297 
298 	if (nconf) {
299 		xprt->xp_netid = strdup(nconf->nc_netid);
300 		xprt->xp_tp = strdup(nconf->nc_device);
301 	}
302 	return (xprt);
303 
304 freedata:
305 	if (madefd)
306 		(void)_close(fd);
307 	if (xprt) {
308 		if (!madefd) /* so that svc_destroy doesnt close fd */
309 			xprt->xp_fd = RPC_ANYFD;
310 		SVC_DESTROY(xprt);
311 	}
312 	return (NULL);
313 }
314