mkfs, mkproto: minor improvements
[minix.git] / lib / libc / rpc / svc_generic.c
blob723555712f935dfe2cffc62a0f71bfe598137dc9
1 /* $NetBSD: svc_generic.c,v 1.10 2008/04/25 17:44:44 christos Exp $ */
3 /*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
33 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
36 /* #ident "@(#)svc_generic.c 1.19 94/04/24 SMI" */
38 #include <sys/cdefs.h>
39 #if defined(LIBC_SCCS) && !defined(lint)
40 #if 0
41 static char sccsid[] = "@(#)svc_generic.c 1.21 89/02/28 Copyr 1988 Sun Micro";
42 #else
43 __RCSID("$NetBSD: svc_generic.c,v 1.10 2008/04/25 17:44:44 christos Exp $");
44 #endif
45 #endif
48 * svc_generic.c, Server side for RPC.
52 #include "namespace.h"
53 #include "reentrant.h"
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <rpc/rpc.h>
58 #include <rpc/nettype.h>
59 #include <stdio.h>
60 #include <errno.h>
61 #include <malloc.h>
62 #include <string.h>
63 #include <unistd.h>
64 #include <err.h>
66 #include "rpc_internal.h"
68 #ifdef __weak_alias
69 __weak_alias(svc_create,_svc_create)
70 __weak_alias(svc_tp_create,_svc_tp_create)
71 __weak_alias(svc_tli_create,_svc_tli_create)
72 #endif
74 extern int __svc_vc_setflag __P((SVCXPRT *, int));
77 * The highest level interface for server creation.
78 * It tries for all the nettokens in that particular class of token
79 * and returns the number of handles it can create and/or find.
81 * It creates a link list of all the handles it could create.
82 * If svc_create() is called multiple times, it uses the handle
83 * created earlier instead of creating a new handle every time.
85 int
86 svc_create(dispatch, prognum, versnum, nettype)
87 void (*dispatch) __P((struct svc_req *, SVCXPRT *));
88 rpcprog_t prognum; /* Program number */
89 rpcvers_t versnum; /* Version number */
90 const char *nettype; /* Networktype token */
92 struct xlist {
93 SVCXPRT *xprt; /* Server handle */
94 struct xlist *next; /* Next item */
95 } *l;
96 static struct xlist *xprtlist; /* A link list of all the handles */
97 int num = 0;
98 SVCXPRT *xprt;
99 struct netconfig *nconf;
100 void *handle;
101 #ifdef _REENTRANT
102 extern mutex_t xprtlist_lock;
103 #endif
105 /* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */
107 if ((handle = __rpc_setconf(nettype)) == NULL) {
108 warnx("svc_create: unknown protocol");
109 return (0);
111 while ((nconf = __rpc_getconf(handle)) != NULL) {
112 mutex_lock(&xprtlist_lock);
113 for (l = xprtlist; l; l = l->next) {
114 if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
115 /* Found an old one, use it */
116 (void) rpcb_unset(prognum, versnum, nconf);
117 if (svc_reg(l->xprt, prognum, versnum,
118 dispatch, nconf) == FALSE)
119 warnx(
120 "svc_create: could not register prog %u vers %u on %s",
121 (unsigned)prognum, (unsigned)versnum,
122 nconf->nc_netid);
123 else
124 num++;
125 break;
128 if (l == NULL) {
129 /* It was not found. Now create a new one */
130 xprt = svc_tp_create(dispatch, prognum, versnum, nconf);
131 if (xprt) {
132 l = malloc(sizeof(*l));
133 if (l == NULL) {
134 warnx("svc_create: no memory");
135 mutex_unlock(&xprtlist_lock);
136 return (0);
138 l->xprt = xprt;
139 l->next = xprtlist;
140 xprtlist = l;
141 num++;
144 mutex_unlock(&xprtlist_lock);
146 __rpc_endconf(handle);
148 * In case of num == 0; the error messages are generated by the
149 * underlying layers; and hence not needed here.
151 return (num);
155 * The high level interface to svc_tli_create().
156 * It tries to create a server for "nconf" and registers the service
157 * with the rpcbind. It calls svc_tli_create();
159 SVCXPRT *
160 svc_tp_create(dispatch, prognum, versnum, nconf)
161 void (*dispatch) __P((struct svc_req *, SVCXPRT *));
162 rpcprog_t prognum; /* Program number */
163 rpcvers_t versnum; /* Version number */
164 const struct netconfig *nconf; /* Netconfig structure for the network */
166 SVCXPRT *xprt;
168 if (nconf == NULL) {
169 warnx(
170 "svc_tp_create: invalid netconfig structure for prog %u vers %u",
171 (unsigned)prognum, (unsigned)versnum);
172 return (NULL);
174 xprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
175 if (xprt == NULL) {
176 return (NULL);
178 (void) rpcb_unset(prognum, versnum, __UNCONST(nconf));
179 if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
180 warnx(
181 "svc_tp_create: Could not register prog %u vers %u on %s",
182 (unsigned)prognum, (unsigned)versnum,
183 nconf->nc_netid);
184 SVC_DESTROY(xprt);
185 return (NULL);
187 return (xprt);
191 * If fd is RPC_ANYFD, then it opens a fd for the given transport
192 * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
193 * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
194 * NULL bindadr and Connection oriented transports, the value of qlen
195 * is set to 8.
197 * If sendsz or recvsz are zero, their default values are chosen.
199 SVCXPRT *
200 svc_tli_create(fd, nconf, bindaddr, sendsz, recvsz)
201 int fd; /* Connection end point */
202 const struct netconfig *nconf; /* Netconfig struct for nettoken */
203 const struct t_bind *bindaddr; /* Local bind address */
204 u_int sendsz; /* Max sendsize */
205 u_int recvsz; /* Max recvsize */
207 SVCXPRT *xprt = NULL; /* service handle */
208 bool_t madefd = FALSE; /* whether fd opened here */
209 struct __rpc_sockinfo si;
210 struct sockaddr_storage ss;
211 socklen_t slen;
213 if (fd == RPC_ANYFD) {
214 if (nconf == NULL) {
215 warnx("svc_tli_create: invalid netconfig");
216 return (NULL);
218 fd = __rpc_nconf2fd(nconf);
219 if (fd == -1) {
220 warnx(
221 "svc_tli_create: could not open connection for %s",
222 nconf->nc_netid);
223 return (NULL);
225 __rpc_nconf2sockinfo(nconf, &si);
226 madefd = TRUE;
227 } else {
229 * It is an open descriptor. Get the transport info.
231 if (!__rpc_fd2sockinfo(fd, &si)) {
232 warnx(
233 "svc_tli_create: could not get transport information");
234 return (NULL);
239 * If the fd is unbound, try to bind it.
241 if (madefd || !__rpc_sockisbound(fd)) {
242 if (bindaddr == NULL) {
243 if (bindresvport(fd, NULL) < 0) {
244 memset(&ss, 0, sizeof ss);
245 ss.ss_family = si.si_af;
246 ss.ss_len = si.si_alen;
247 if (bind(fd, (struct sockaddr *)(void *)&ss,
248 (socklen_t)si.si_alen) < 0) {
249 warnx(
250 "svc_tli_create: could not bind to anonymous port");
251 goto freedata;
254 listen(fd, SOMAXCONN);
255 } else {
256 if (bind(fd,
257 (struct sockaddr *)bindaddr->addr.buf,
258 (socklen_t)si.si_alen) < 0) {
259 warnx(
260 "svc_tli_create: could not bind to requested address");
261 goto freedata;
263 listen(fd, (int)bindaddr->qlen);
268 * call transport specific function.
270 switch (si.si_socktype) {
271 case SOCK_STREAM:
272 slen = sizeof ss;
273 if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen)
274 == 0) {
275 /* accepted socket */
276 xprt = svc_fd_create(fd, sendsz, recvsz);
277 } else
278 xprt = svc_vc_create(fd, sendsz, recvsz);
279 if (!nconf || !xprt)
280 break;
281 #if 0
282 /* XXX fvdl */
283 if (strcmp(nconf->nc_protofmly, "inet") == 0 ||
284 strcmp(nconf->nc_protofmly, "inet6") == 0)
285 (void) __svc_vc_setflag(xprt, TRUE);
286 #endif
287 break;
288 case SOCK_DGRAM:
289 xprt = svc_dg_create(fd, sendsz, recvsz);
290 break;
291 default:
292 warnx("svc_tli_create: bad service type");
293 goto freedata;
296 if (xprt == NULL)
298 * The error messages here are spitted out by the lower layers:
299 * svc_vc_create(), svc_fd_create() and svc_dg_create().
301 goto freedata;
303 /* Fill in type of service */
304 xprt->xp_type = __rpc_socktype2seman(si.si_socktype);
306 if (nconf) {
307 xprt->xp_netid = strdup(nconf->nc_netid);
308 xprt->xp_tp = strdup(nconf->nc_device);
309 if (xprt->xp_netid == NULL || xprt->xp_tp == NULL) {
310 svc_destroy(xprt);
311 return NULL;
314 return (xprt);
316 freedata:
317 if (madefd)
318 (void) close(fd);
319 return (NULL);