xref: /dragonfly/lib/libc/rpc/svc_generic.c (revision 0dace59e)
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  *
29  * @(#)svc_generic.c	1.19	94/04/24 SMI; 1.21 89/02/28 Copyr 1988 Sun Micro
30  * $NetBSD: svc_generic.c,v 1.3 2000/07/06 03:10:35 christos Exp $
31  * $FreeBSD: src/lib/libc/rpc/svc_generic.c,v 1.7 2006/02/27 22:10:59 deischen Exp $
32  */
33 
34 /*
35  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
36  */
37 
38 /*
39  * svc_generic.c, Server side for RPC.
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 int
72 svc_create(void (*dispatch)(struct svc_req *, SVCXPRT *),
73 	   rpcprog_t prognum,		/* Program number */
74 	   rpcvers_t versnum,		/* Version number */
75 	   const char *nettype)		/* Networktype token */
76 {
77 	struct xlist {
78 		SVCXPRT *xprt;		/* Server handle */
79 		struct xlist *next;	/* Next item */
80 	} *l;
81 	static struct xlist *xprtlist;	/* A link list of all the handles */
82 	int num = 0;
83 	SVCXPRT *xprt;
84 	struct netconfig *nconf;
85 	void *handle;
86 
87 /* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */
88 
89 	if ((handle = __rpc_setconf(nettype)) == NULL) {
90 		warnx("svc_create: unknown protocol");
91 		return (0);
92 	}
93 	while ((nconf = __rpc_getconf(handle)) != NULL) {
94 		mutex_lock(&xprtlist_lock);
95 		for (l = xprtlist; l; l = l->next) {
96 			if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
97 				/* Found an old one, use it */
98 				rpcb_unset(prognum, versnum, nconf);
99 				if (svc_reg(l->xprt, prognum, versnum,
100 					dispatch, nconf) == FALSE)
101 					warnx(
102 		"svc_create: could not register prog %u vers %u on %s",
103 					(unsigned)prognum, (unsigned)versnum,
104 					 nconf->nc_netid);
105 				else
106 					num++;
107 				break;
108 			}
109 		}
110 		if (l == NULL) {
111 			/* It was not found. Now create a new one */
112 			xprt = svc_tp_create(dispatch, prognum, versnum, nconf);
113 			if (xprt) {
114 				l = (struct xlist *)malloc(sizeof (*l));
115 				if (l == NULL) {
116 					warnx("svc_create: no memory");
117 					mutex_unlock(&xprtlist_lock);
118 					return (0);
119 				}
120 				l->xprt = xprt;
121 				l->next = xprtlist;
122 				xprtlist = l;
123 				num++;
124 			}
125 		}
126 		mutex_unlock(&xprtlist_lock);
127 	}
128 	__rpc_endconf(handle);
129 	/*
130 	 * In case of num == 0; the error messages are generated by the
131 	 * underlying layers; and hence not needed here.
132 	 */
133 	return (num);
134 }
135 
136 /*
137  * The high level interface to svc_tli_create().
138  * It tries to create a server for "nconf" and registers the service
139  * with the rpcbind. It calls svc_tli_create();
140  */
141 SVCXPRT *
142 svc_tp_create(void (*dispatch)(struct svc_req *, SVCXPRT *),
143 	      rpcprog_t prognum,		/* Program number */
144 	      rpcvers_t versnum,		/* Version number */
145 	      const struct netconfig *nconf)	/* Netconfig structure for the network */
146 {
147 	SVCXPRT *xprt;
148 
149 	if (nconf == NULL) {
150 		warnx(
151 	"svc_tp_create: invalid netconfig structure for prog %u vers %u",
152 				(unsigned)prognum, (unsigned)versnum);
153 		return (NULL);
154 	}
155 	xprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
156 	if (xprt == NULL) {
157 		return (NULL);
158 	}
159 	/*LINTED const castaway*/
160 	rpcb_unset(prognum, versnum, (struct netconfig *) nconf);
161 	if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
162 		warnx(
163 		"svc_tp_create: Could not register prog %u vers %u on %s",
164 				(unsigned)prognum, (unsigned)versnum,
165 				nconf->nc_netid);
166 		SVC_DESTROY(xprt);
167 		return (NULL);
168 	}
169 	return (xprt);
170 }
171 
172 /*
173  * If fd is RPC_ANYFD, then it opens a fd for the given transport
174  * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
175  * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
176  * NULL bindadr and Connection oriented transports, the value of qlen
177  * is set to 8.
178  *
179  * If sendsz or recvsz are zero, their default values are chosen.
180  */
181 SVCXPRT *
182 svc_tli_create(
183 	int fd,				/* Connection end point */
184 	const struct netconfig *nconf,	/* Netconfig struct for nettoken */
185 	const struct t_bind *bindaddr,	/* Local bind address */
186 	u_int sendsz,			/* Max sendsize */
187 	u_int recvsz)			/* Max recvsize */
188 {
189 	SVCXPRT *xprt = NULL;		/* service handle */
190 	bool_t madefd = FALSE;		/* whether fd opened here  */
191 	struct __rpc_sockinfo si;
192 	struct sockaddr_storage ss;
193 	socklen_t slen;
194 
195 	if (fd == RPC_ANYFD) {
196 		if (nconf == NULL) {
197 			warnx("svc_tli_create: invalid netconfig");
198 			return (NULL);
199 		}
200 		fd = __rpc_nconf2fd(nconf);
201 		if (fd == -1) {
202 			warnx(
203 			    "svc_tli_create: could not open connection for %s",
204 					nconf->nc_netid);
205 			return (NULL);
206 		}
207 		__rpc_nconf2sockinfo(nconf, &si);
208 		madefd = TRUE;
209 	} else {
210 		/*
211 		 * It is an open descriptor. Get the transport info.
212 		 */
213 		if (!__rpc_fd2sockinfo(fd, &si)) {
214 			warnx(
215 		"svc_tli_create: could not get transport information");
216 			return (NULL);
217 		}
218 	}
219 
220 	/*
221 	 * If the fd is unbound, try to bind it.
222 	 */
223 	if (madefd || !__rpc_sockisbound(fd)) {
224 		if (bindaddr == NULL) {
225 			if (bindresvport(fd, NULL) < 0) {
226 				memset(&ss, 0, sizeof ss);
227 				ss.ss_family = si.si_af;
228 				ss.ss_len = si.si_alen;
229 				if (_bind(fd, (struct sockaddr *)(void *)&ss,
230 				    (socklen_t)si.si_alen) < 0) {
231 					warnx(
232 			"svc_tli_create: could not bind to anonymous port");
233 					goto freedata;
234 				}
235 			}
236 			_listen(fd, SOMAXCONN);
237 		} else {
238 			if (_bind(fd,
239 			    (struct sockaddr *)bindaddr->addr.buf,
240 			    (socklen_t)si.si_alen) < 0) {
241 				warnx(
242 		"svc_tli_create: could not bind to requested address");
243 				goto freedata;
244 			}
245 			_listen(fd, (int)bindaddr->qlen);
246 		}
247 
248 	}
249 	/*
250 	 * call transport specific function.
251 	 */
252 	switch (si.si_socktype) {
253 		case SOCK_STREAM:
254 			slen = sizeof ss;
255 			if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen)
256 			    == 0) {
257 				/* accepted socket */
258 				xprt = svc_fd_create(fd, sendsz, recvsz);
259 			} else
260 				xprt = svc_vc_create(fd, sendsz, recvsz);
261 			if (!nconf || !xprt)
262 				break;
263 #if 0
264 			/* XXX fvdl */
265 			if (strcmp(nconf->nc_protofmly, "inet") == 0 ||
266 			    strcmp(nconf->nc_protofmly, "inet6") == 0)
267 				(void) __svc_vc_setflag(xprt, TRUE);
268 #endif
269 			break;
270 		case SOCK_DGRAM:
271 			xprt = svc_dg_create(fd, sendsz, recvsz);
272 			break;
273 		default:
274 			warnx("svc_tli_create: bad service type");
275 			goto freedata;
276 	}
277 
278 	if (xprt == NULL)
279 		/*
280 		 * The error messages here are spitted out by the lower layers:
281 		 * svc_vc_create(), svc_fd_create() and svc_dg_create().
282 		 */
283 		goto freedata;
284 
285 	/* Fill in type of service */
286 	xprt->xp_type = __rpc_socktype2seman(si.si_socktype);
287 
288 	if (nconf) {
289 		xprt->xp_netid = strdup(nconf->nc_netid);
290 		xprt->xp_tp = strdup(nconf->nc_device);
291 	}
292 	return (xprt);
293 
294 freedata:
295 	if (madefd)
296 		_close(fd);
297 	if (xprt) {
298 		if (!madefd) /* so that svc_destroy doesnt close fd */
299 			xprt->xp_fd = RPC_ANYFD;
300 		SVC_DESTROY(xprt);
301 	}
302 	return (NULL);
303 }
304