1*ba96d07fShrs /*-
2*ba96d07fShrs * Copyright (c) 2009, Sun Microsystems, Inc.
3*ba96d07fShrs * All rights reserved.
4ce0e08e2SPeter Avalos *
5*ba96d07fShrs * Redistribution and use in source and binary forms, with or without
6*ba96d07fShrs * modification, are permitted provided that the following conditions are met:
7*ba96d07fShrs * - Redistributions of source code must retain the above copyright notice,
8*ba96d07fShrs * this list of conditions and the following disclaimer.
9*ba96d07fShrs * - Redistributions in binary form must reproduce the above copyright notice,
10*ba96d07fShrs * this list of conditions and the following disclaimer in the documentation
11*ba96d07fShrs * and/or other materials provided with the distribution.
12*ba96d07fShrs * - Neither the name of Sun Microsystems, Inc. nor the names of its
13*ba96d07fShrs * contributors may be used to endorse or promote products derived
14*ba96d07fShrs * from this software without specific prior written permission.
15ce0e08e2SPeter Avalos *
16*ba96d07fShrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*ba96d07fShrs * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*ba96d07fShrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*ba96d07fShrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*ba96d07fShrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*ba96d07fShrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*ba96d07fShrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*ba96d07fShrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*ba96d07fShrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*ba96d07fShrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*ba96d07fShrs * POSSIBILITY OF SUCH DAMAGE.
27ce0e08e2SPeter Avalos *
28ce0e08e2SPeter Avalos * @(#)svc_generic.c 1.19 94/04/24 SMI; 1.21 89/02/28 Copyr 1988 Sun Micro
29ce0e08e2SPeter Avalos * $NetBSD: svc_generic.c,v 1.3 2000/07/06 03:10:35 christos Exp $
30ce0e08e2SPeter Avalos * $FreeBSD: src/lib/libc/rpc/svc_generic.c,v 1.7 2006/02/27 22:10:59 deischen Exp $
31ce0e08e2SPeter Avalos */
32ce0e08e2SPeter Avalos
33ce0e08e2SPeter Avalos /*
34ce0e08e2SPeter Avalos * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35ce0e08e2SPeter Avalos */
36ce0e08e2SPeter Avalos
37ce0e08e2SPeter Avalos /*
38ce0e08e2SPeter Avalos * svc_generic.c, Server side for RPC.
39ce0e08e2SPeter Avalos */
40ce0e08e2SPeter Avalos
41ce0e08e2SPeter Avalos #include "namespace.h"
42ce0e08e2SPeter Avalos #include "reentrant.h"
43ce0e08e2SPeter Avalos #include <sys/types.h>
44ce0e08e2SPeter Avalos #include <sys/socket.h>
45ce0e08e2SPeter Avalos #include <netinet/in.h>
46ce0e08e2SPeter Avalos #include <rpc/rpc.h>
47ce0e08e2SPeter Avalos #include <rpc/nettype.h>
48ce0e08e2SPeter Avalos #include <stdio.h>
49ce0e08e2SPeter Avalos #include <errno.h>
50ce0e08e2SPeter Avalos #include <stdlib.h>
51ce0e08e2SPeter Avalos #include <string.h>
52ce0e08e2SPeter Avalos #include <unistd.h>
53ce0e08e2SPeter Avalos #include <err.h>
54ce0e08e2SPeter Avalos #include "un-namespace.h"
55ce0e08e2SPeter Avalos
56ce0e08e2SPeter Avalos #include "rpc_com.h"
57ce0e08e2SPeter Avalos #include "mt_misc.h"
58ce0e08e2SPeter Avalos
59ce0e08e2SPeter Avalos extern int __svc_vc_setflag(SVCXPRT *, int);
60ce0e08e2SPeter Avalos
61ce0e08e2SPeter Avalos /*
62ce0e08e2SPeter Avalos * The highest level interface for server creation.
63ce0e08e2SPeter Avalos * It tries for all the nettokens in that particular class of token
64ce0e08e2SPeter Avalos * and returns the number of handles it can create and/or find.
65ce0e08e2SPeter Avalos *
66ce0e08e2SPeter Avalos * It creates a link list of all the handles it could create.
67ce0e08e2SPeter Avalos * If svc_create() is called multiple times, it uses the handle
68ce0e08e2SPeter Avalos * created earlier instead of creating a new handle every time.
69ce0e08e2SPeter Avalos */
70ce0e08e2SPeter Avalos int
svc_create(void (* dispatch)(struct svc_req *,SVCXPRT *),rpcprog_t prognum,rpcvers_t versnum,const char * nettype)71ce0e08e2SPeter Avalos svc_create(void (*dispatch)(struct svc_req *, SVCXPRT *),
72ce0e08e2SPeter Avalos rpcprog_t prognum, /* Program number */
73ce0e08e2SPeter Avalos rpcvers_t versnum, /* Version number */
74ce0e08e2SPeter Avalos const char *nettype) /* Networktype token */
75ce0e08e2SPeter Avalos {
76ce0e08e2SPeter Avalos struct xlist {
77ce0e08e2SPeter Avalos SVCXPRT *xprt; /* Server handle */
78ce0e08e2SPeter Avalos struct xlist *next; /* Next item */
79ce0e08e2SPeter Avalos } *l;
80ce0e08e2SPeter Avalos static struct xlist *xprtlist; /* A link list of all the handles */
81ce0e08e2SPeter Avalos int num = 0;
82ce0e08e2SPeter Avalos SVCXPRT *xprt;
83ce0e08e2SPeter Avalos struct netconfig *nconf;
84ce0e08e2SPeter Avalos void *handle;
85ce0e08e2SPeter Avalos
86ce0e08e2SPeter Avalos /* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */
87ce0e08e2SPeter Avalos
88ce0e08e2SPeter Avalos if ((handle = __rpc_setconf(nettype)) == NULL) {
89ce0e08e2SPeter Avalos warnx("svc_create: unknown protocol");
90ce0e08e2SPeter Avalos return (0);
91ce0e08e2SPeter Avalos }
92ce0e08e2SPeter Avalos while ((nconf = __rpc_getconf(handle)) != NULL) {
93ce0e08e2SPeter Avalos mutex_lock(&xprtlist_lock);
94ce0e08e2SPeter Avalos for (l = xprtlist; l; l = l->next) {
95ce0e08e2SPeter Avalos if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
96ce0e08e2SPeter Avalos /* Found an old one, use it */
97ce0e08e2SPeter Avalos rpcb_unset(prognum, versnum, nconf);
98ce0e08e2SPeter Avalos if (svc_reg(l->xprt, prognum, versnum,
99ce0e08e2SPeter Avalos dispatch, nconf) == FALSE)
100ce0e08e2SPeter Avalos warnx(
101ce0e08e2SPeter Avalos "svc_create: could not register prog %u vers %u on %s",
102ce0e08e2SPeter Avalos (unsigned)prognum, (unsigned)versnum,
103ce0e08e2SPeter Avalos nconf->nc_netid);
104ce0e08e2SPeter Avalos else
105ce0e08e2SPeter Avalos num++;
106ce0e08e2SPeter Avalos break;
107ce0e08e2SPeter Avalos }
108ce0e08e2SPeter Avalos }
109ce0e08e2SPeter Avalos if (l == NULL) {
110ce0e08e2SPeter Avalos /* It was not found. Now create a new one */
111ce0e08e2SPeter Avalos xprt = svc_tp_create(dispatch, prognum, versnum, nconf);
112ce0e08e2SPeter Avalos if (xprt) {
113ce0e08e2SPeter Avalos l = (struct xlist *)malloc(sizeof (*l));
114ce0e08e2SPeter Avalos if (l == NULL) {
115ce0e08e2SPeter Avalos warnx("svc_create: no memory");
116ce0e08e2SPeter Avalos mutex_unlock(&xprtlist_lock);
117ce0e08e2SPeter Avalos return (0);
118ce0e08e2SPeter Avalos }
119ce0e08e2SPeter Avalos l->xprt = xprt;
120ce0e08e2SPeter Avalos l->next = xprtlist;
121ce0e08e2SPeter Avalos xprtlist = l;
122ce0e08e2SPeter Avalos num++;
123ce0e08e2SPeter Avalos }
124ce0e08e2SPeter Avalos }
125ce0e08e2SPeter Avalos mutex_unlock(&xprtlist_lock);
126ce0e08e2SPeter Avalos }
127ce0e08e2SPeter Avalos __rpc_endconf(handle);
128ce0e08e2SPeter Avalos /*
129ce0e08e2SPeter Avalos * In case of num == 0; the error messages are generated by the
130ce0e08e2SPeter Avalos * underlying layers; and hence not needed here.
131ce0e08e2SPeter Avalos */
132ce0e08e2SPeter Avalos return (num);
133ce0e08e2SPeter Avalos }
134ce0e08e2SPeter Avalos
135ce0e08e2SPeter Avalos /*
136ce0e08e2SPeter Avalos * The high level interface to svc_tli_create().
137ce0e08e2SPeter Avalos * It tries to create a server for "nconf" and registers the service
138ce0e08e2SPeter Avalos * with the rpcbind. It calls svc_tli_create();
139ce0e08e2SPeter Avalos */
140ce0e08e2SPeter Avalos SVCXPRT *
svc_tp_create(void (* dispatch)(struct svc_req *,SVCXPRT *),rpcprog_t prognum,rpcvers_t versnum,const struct netconfig * nconf)141ce0e08e2SPeter Avalos svc_tp_create(void (*dispatch)(struct svc_req *, SVCXPRT *),
142ce0e08e2SPeter Avalos rpcprog_t prognum, /* Program number */
143ce0e08e2SPeter Avalos rpcvers_t versnum, /* Version number */
144ce0e08e2SPeter Avalos const struct netconfig *nconf) /* Netconfig structure for the network */
145ce0e08e2SPeter Avalos {
146ce0e08e2SPeter Avalos SVCXPRT *xprt;
147ce0e08e2SPeter Avalos
148ce0e08e2SPeter Avalos if (nconf == NULL) {
149ce0e08e2SPeter Avalos warnx(
150ce0e08e2SPeter Avalos "svc_tp_create: invalid netconfig structure for prog %u vers %u",
151ce0e08e2SPeter Avalos (unsigned)prognum, (unsigned)versnum);
152ce0e08e2SPeter Avalos return (NULL);
153ce0e08e2SPeter Avalos }
154ce0e08e2SPeter Avalos xprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
155ce0e08e2SPeter Avalos if (xprt == NULL) {
156ce0e08e2SPeter Avalos return (NULL);
157ce0e08e2SPeter Avalos }
158ce0e08e2SPeter Avalos /*LINTED const castaway*/
159ce0e08e2SPeter Avalos rpcb_unset(prognum, versnum, (struct netconfig *) nconf);
160ce0e08e2SPeter Avalos if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
161ce0e08e2SPeter Avalos warnx(
162ce0e08e2SPeter Avalos "svc_tp_create: Could not register prog %u vers %u on %s",
163ce0e08e2SPeter Avalos (unsigned)prognum, (unsigned)versnum,
164ce0e08e2SPeter Avalos nconf->nc_netid);
165ce0e08e2SPeter Avalos SVC_DESTROY(xprt);
166ce0e08e2SPeter Avalos return (NULL);
167ce0e08e2SPeter Avalos }
168ce0e08e2SPeter Avalos return (xprt);
169ce0e08e2SPeter Avalos }
170ce0e08e2SPeter Avalos
171ce0e08e2SPeter Avalos /*
172ce0e08e2SPeter Avalos * If fd is RPC_ANYFD, then it opens a fd for the given transport
173ce0e08e2SPeter Avalos * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
174ce0e08e2SPeter Avalos * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
175ce0e08e2SPeter Avalos * NULL bindadr and Connection oriented transports, the value of qlen
176ce0e08e2SPeter Avalos * is set to 8.
177ce0e08e2SPeter Avalos *
178ce0e08e2SPeter Avalos * If sendsz or recvsz are zero, their default values are chosen.
179ce0e08e2SPeter Avalos */
180ce0e08e2SPeter Avalos SVCXPRT *
svc_tli_create(int fd,const struct netconfig * nconf,const struct t_bind * bindaddr,u_int sendsz,u_int recvsz)181ce0e08e2SPeter Avalos svc_tli_create(
182ce0e08e2SPeter Avalos int fd, /* Connection end point */
183ce0e08e2SPeter Avalos const struct netconfig *nconf, /* Netconfig struct for nettoken */
184ce0e08e2SPeter Avalos const struct t_bind *bindaddr, /* Local bind address */
185ce0e08e2SPeter Avalos u_int sendsz, /* Max sendsize */
186ce0e08e2SPeter Avalos u_int recvsz) /* Max recvsize */
187ce0e08e2SPeter Avalos {
188ce0e08e2SPeter Avalos SVCXPRT *xprt = NULL; /* service handle */
189ce0e08e2SPeter Avalos bool_t madefd = FALSE; /* whether fd opened here */
190ce0e08e2SPeter Avalos struct __rpc_sockinfo si;
191ce0e08e2SPeter Avalos struct sockaddr_storage ss;
192ce0e08e2SPeter Avalos socklen_t slen;
193ce0e08e2SPeter Avalos
194ce0e08e2SPeter Avalos if (fd == RPC_ANYFD) {
195ce0e08e2SPeter Avalos if (nconf == NULL) {
196ce0e08e2SPeter Avalos warnx("svc_tli_create: invalid netconfig");
197ce0e08e2SPeter Avalos return (NULL);
198ce0e08e2SPeter Avalos }
199ce0e08e2SPeter Avalos fd = __rpc_nconf2fd(nconf);
200ce0e08e2SPeter Avalos if (fd == -1) {
201ce0e08e2SPeter Avalos warnx(
202ce0e08e2SPeter Avalos "svc_tli_create: could not open connection for %s",
203ce0e08e2SPeter Avalos nconf->nc_netid);
204ce0e08e2SPeter Avalos return (NULL);
205ce0e08e2SPeter Avalos }
206ce0e08e2SPeter Avalos __rpc_nconf2sockinfo(nconf, &si);
207ce0e08e2SPeter Avalos madefd = TRUE;
208ce0e08e2SPeter Avalos } else {
209ce0e08e2SPeter Avalos /*
210ce0e08e2SPeter Avalos * It is an open descriptor. Get the transport info.
211ce0e08e2SPeter Avalos */
212ce0e08e2SPeter Avalos if (!__rpc_fd2sockinfo(fd, &si)) {
213ce0e08e2SPeter Avalos warnx(
214ce0e08e2SPeter Avalos "svc_tli_create: could not get transport information");
215ce0e08e2SPeter Avalos return (NULL);
216ce0e08e2SPeter Avalos }
217ce0e08e2SPeter Avalos }
218ce0e08e2SPeter Avalos
219ce0e08e2SPeter Avalos /*
220ce0e08e2SPeter Avalos * If the fd is unbound, try to bind it.
221ce0e08e2SPeter Avalos */
222ce0e08e2SPeter Avalos if (madefd || !__rpc_sockisbound(fd)) {
223ce0e08e2SPeter Avalos if (bindaddr == NULL) {
224ce0e08e2SPeter Avalos if (bindresvport(fd, NULL) < 0) {
225ce0e08e2SPeter Avalos memset(&ss, 0, sizeof ss);
226ce0e08e2SPeter Avalos ss.ss_family = si.si_af;
227ce0e08e2SPeter Avalos ss.ss_len = si.si_alen;
228ce0e08e2SPeter Avalos if (_bind(fd, (struct sockaddr *)(void *)&ss,
229ce0e08e2SPeter Avalos (socklen_t)si.si_alen) < 0) {
230ce0e08e2SPeter Avalos warnx(
231ce0e08e2SPeter Avalos "svc_tli_create: could not bind to anonymous port");
232ce0e08e2SPeter Avalos goto freedata;
233ce0e08e2SPeter Avalos }
234ce0e08e2SPeter Avalos }
235ce0e08e2SPeter Avalos _listen(fd, SOMAXCONN);
236ce0e08e2SPeter Avalos } else {
237ce0e08e2SPeter Avalos if (_bind(fd,
238ce0e08e2SPeter Avalos (struct sockaddr *)bindaddr->addr.buf,
239ce0e08e2SPeter Avalos (socklen_t)si.si_alen) < 0) {
240ce0e08e2SPeter Avalos warnx(
241ce0e08e2SPeter Avalos "svc_tli_create: could not bind to requested address");
242ce0e08e2SPeter Avalos goto freedata;
243ce0e08e2SPeter Avalos }
244ce0e08e2SPeter Avalos _listen(fd, (int)bindaddr->qlen);
245ce0e08e2SPeter Avalos }
246ce0e08e2SPeter Avalos
247ce0e08e2SPeter Avalos }
248ce0e08e2SPeter Avalos /*
249ce0e08e2SPeter Avalos * call transport specific function.
250ce0e08e2SPeter Avalos */
251ce0e08e2SPeter Avalos switch (si.si_socktype) {
252ce0e08e2SPeter Avalos case SOCK_STREAM:
253ce0e08e2SPeter Avalos slen = sizeof ss;
254ce0e08e2SPeter Avalos if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen)
255ce0e08e2SPeter Avalos == 0) {
256ce0e08e2SPeter Avalos /* accepted socket */
257ce0e08e2SPeter Avalos xprt = svc_fd_create(fd, sendsz, recvsz);
258ce0e08e2SPeter Avalos } else
259ce0e08e2SPeter Avalos xprt = svc_vc_create(fd, sendsz, recvsz);
260ce0e08e2SPeter Avalos if (!nconf || !xprt)
261ce0e08e2SPeter Avalos break;
262ce0e08e2SPeter Avalos #if 0
263ce0e08e2SPeter Avalos /* XXX fvdl */
264ce0e08e2SPeter Avalos if (strcmp(nconf->nc_protofmly, "inet") == 0 ||
265ce0e08e2SPeter Avalos strcmp(nconf->nc_protofmly, "inet6") == 0)
266ce0e08e2SPeter Avalos (void) __svc_vc_setflag(xprt, TRUE);
267ce0e08e2SPeter Avalos #endif
268ce0e08e2SPeter Avalos break;
269ce0e08e2SPeter Avalos case SOCK_DGRAM:
270ce0e08e2SPeter Avalos xprt = svc_dg_create(fd, sendsz, recvsz);
271ce0e08e2SPeter Avalos break;
272ce0e08e2SPeter Avalos default:
273ce0e08e2SPeter Avalos warnx("svc_tli_create: bad service type");
274ce0e08e2SPeter Avalos goto freedata;
275ce0e08e2SPeter Avalos }
276ce0e08e2SPeter Avalos
277ce0e08e2SPeter Avalos if (xprt == NULL)
278ce0e08e2SPeter Avalos /*
279ce0e08e2SPeter Avalos * The error messages here are spitted out by the lower layers:
280ce0e08e2SPeter Avalos * svc_vc_create(), svc_fd_create() and svc_dg_create().
281ce0e08e2SPeter Avalos */
282ce0e08e2SPeter Avalos goto freedata;
283ce0e08e2SPeter Avalos
284ce0e08e2SPeter Avalos /* Fill in type of service */
285ce0e08e2SPeter Avalos xprt->xp_type = __rpc_socktype2seman(si.si_socktype);
286ce0e08e2SPeter Avalos
287ce0e08e2SPeter Avalos if (nconf) {
288ce0e08e2SPeter Avalos xprt->xp_netid = strdup(nconf->nc_netid);
289ce0e08e2SPeter Avalos xprt->xp_tp = strdup(nconf->nc_device);
290ce0e08e2SPeter Avalos }
291ce0e08e2SPeter Avalos return (xprt);
292ce0e08e2SPeter Avalos
293ce0e08e2SPeter Avalos freedata:
294ce0e08e2SPeter Avalos if (madefd)
295ce0e08e2SPeter Avalos _close(fd);
296ce0e08e2SPeter Avalos if (xprt) {
297ce0e08e2SPeter Avalos if (!madefd) /* so that svc_destroy doesnt close fd */
298ce0e08e2SPeter Avalos xprt->xp_fd = RPC_ANYFD;
299ce0e08e2SPeter Avalos SVC_DESTROY(xprt);
300ce0e08e2SPeter Avalos }
301ce0e08e2SPeter Avalos return (NULL);
302ce0e08e2SPeter Avalos }
303