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.
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.
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.
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.
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.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
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 $
35 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
39 * svc_generic.c, Server side for RPC.
42 #include "namespace.h"
43 #include "reentrant.h"
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
48 #include <rpc/nettype.h>
55 #include "un-namespace.h"
60 extern int __svc_vc_setflag(SVCXPRT
*, int);
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.
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.
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 */
78 SVCXPRT
*xprt
; /* Server handle */
79 struct xlist
*next
; /* Next item */
81 static struct xlist
*xprtlist
; /* A link list of all the handles */
84 struct netconfig
*nconf
;
87 /* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */
89 if ((handle
= __rpc_setconf(nettype
)) == NULL
) {
90 warnx("svc_create: unknown protocol");
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
)
102 "svc_create: could not register prog %u vers %u on %s",
103 (unsigned)prognum
, (unsigned)versnum
,
111 /* It was not found. Now create a new one */
112 xprt
= svc_tp_create(dispatch
, prognum
, versnum
, nconf
);
114 l
= (struct xlist
*)malloc(sizeof (*l
));
116 warnx("svc_create: no memory");
117 mutex_unlock(&xprtlist_lock
);
126 mutex_unlock(&xprtlist_lock
);
128 __rpc_endconf(handle
);
130 * In case of num == 0; the error messages are generated by the
131 * underlying layers; and hence not needed here.
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();
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 */
151 "svc_tp_create: invalid netconfig structure for prog %u vers %u",
152 (unsigned)prognum
, (unsigned)versnum
);
155 xprt
= svc_tli_create(RPC_ANYFD
, nconf
, NULL
, 0, 0);
159 /*LINTED const castaway*/
160 rpcb_unset(prognum
, versnum
, (struct netconfig
*) nconf
);
161 if (svc_reg(xprt
, prognum
, versnum
, dispatch
, nconf
) == FALSE
) {
163 "svc_tp_create: Could not register prog %u vers %u on %s",
164 (unsigned)prognum
, (unsigned)versnum
,
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
179 * If sendsz or recvsz are zero, their default values are chosen.
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 */
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
;
195 if (fd
== RPC_ANYFD
) {
197 warnx("svc_tli_create: invalid netconfig");
200 fd
= __rpc_nconf2fd(nconf
);
203 "svc_tli_create: could not open connection for %s",
207 __rpc_nconf2sockinfo(nconf
, &si
);
211 * It is an open descriptor. Get the transport info.
213 if (!__rpc_fd2sockinfo(fd
, &si
)) {
215 "svc_tli_create: could not get transport information");
221 * If the fd is unbound, try to bind it.
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) {
232 "svc_tli_create: could not bind to anonymous port");
236 _listen(fd
, SOMAXCONN
);
239 (struct sockaddr
*)bindaddr
->addr
.buf
,
240 (socklen_t
)si
.si_alen
) < 0) {
242 "svc_tli_create: could not bind to requested address");
245 _listen(fd
, (int)bindaddr
->qlen
);
250 * call transport specific function.
252 switch (si
.si_socktype
) {
255 if (_getpeername(fd
, (struct sockaddr
*)(void *)&ss
, &slen
)
257 /* accepted socket */
258 xprt
= svc_fd_create(fd
, sendsz
, recvsz
);
260 xprt
= svc_vc_create(fd
, sendsz
, recvsz
);
265 if (strcmp(nconf
->nc_protofmly
, "inet") == 0 ||
266 strcmp(nconf
->nc_protofmly
, "inet6") == 0)
267 (void) __svc_vc_setflag(xprt
, TRUE
);
271 xprt
= svc_dg_create(fd
, sendsz
, recvsz
);
274 warnx("svc_tli_create: bad service type");
280 * The error messages here are spitted out by the lower layers:
281 * svc_vc_create(), svc_fd_create() and svc_dg_create().
285 /* Fill in type of service */
286 xprt
->xp_type
= __rpc_socktype2seman(si
.si_socktype
);
289 xprt
->xp_netid
= strdup(nconf
->nc_netid
);
290 xprt
->xp_tp
= strdup(nconf
->nc_device
);
298 if (!madefd
) /* so that svc_destroy doesnt close fd */
299 xprt
->xp_fd
= RPC_ANYFD
;