Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/svc_generic.c

raw symbols SCIP
1
2 /*
3 * Copyright (c) 2009, Sun Microsystems, Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * - Neither the name of Sun Microsystems, Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
32 */
33
34 /*
35 * svc_generic.c, Server side for RPC.
36 *
37 */
38 #include <pthread.h>
39 #include <reentrant.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <rpc/rpc.h>
44 #include <rpc/nettype.h>
45 #include <stdio.h>
46 #include <errno.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <err.h>
51
52 #include "rpc_com.h"
53 #include <rpc/svc.h>
54
55 extern int __svc_vc_setflag(SVCXPRT *, int);
56 extern int __binddynport(int fd);
57
58 /*
59 * The highest level interface for server creation.
60 * It tries for all the nettokens in that particular class of token
61 * and returns the number of handles it can create and/or find.
62 *
63 * It creates a link list of all the handles it could create.
64 * If svc_create() is called multiple times, it uses the handle
65 * created earlier instead of creating a new handle every time.
66 */
67 int
68 svc_create(dispatch, prognum, versnum, nettype)
69 void (*dispatch)(struct svc_req *, SVCXPRT *);
70 rpcprog_t prognum; /* Program number */
71 rpcvers_t versnum; /* Version number */
72 const char *nettype; /* Networktype token */
73 {
74 struct xlist {
75 SVCXPRT *xprt; /* Server handle */
76 struct xlist *next; /* Next item */
77 } *l;
78 static struct xlist *xprtlist; /* A link list of all the handles */
79 int num = 0;
80 SVCXPRT *xprt;
81 struct netconfig *nconf;
82 void *handle;
83 extern mutex_t xprtlist_lock;
84
85 /* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */
86
87 if ((handle = __rpc_setconf(nettype)) == NULL) {
88 warnx("svc_create: unknown protocol");
89 return (0);
90 }
91 while ((nconf = __rpc_getconf(handle)) != NULL) {
92 mutex_lock(&xprtlist_lock);
93 for (l = xprtlist; l; l = l->next) {
94 if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
95 /* Found an old one, use it */
96 (void) rpcb_unset(prognum, versnum, nconf);
97 if (svc_reg(l->xprt, prognum, versnum,
98 dispatch, nconf) == FALSE)
99 warnx(
100 "svc_create: could not register prog %u vers %u on %s",
101 (unsigned)prognum, (unsigned)versnum,
102 nconf->nc_netid);
103 else
104 num++;
105 break;
106 }
107 }
108 if (l == NULL) {
109 /* It was not found. Now create a new one */
110 xprt = svc_tp_create(dispatch, prognum, versnum, nconf);
111 if (xprt) {
112 l = (struct xlist *)malloc(sizeof (*l));
113 if (l == NULL) {
114 warnx("svc_create: no memory");
115 mutex_unlock(&xprtlist_lock);
116 __rpc_endconf(handle);
117 return (0);
118 }
119 l->xprt = xprt;
120 l->next = xprtlist;
121 xprtlist = l;
122 num++;
123 }
124 }
125 mutex_unlock(&xprtlist_lock);
126 }
127 __rpc_endconf(handle);
128 /*
129 * In case of num == 0; the error messages are generated by the
130 * underlying layers; and hence not needed here.
131 */
132 return (num);
133 }
134
135 /*
136 * The high level interface to svc_tli_create().
137 * It tries to create a server for "nconf" and registers the service
138 * with the rpcbind. It calls svc_tli_create();
139 */
140 SVCXPRT *
141 svc_tp_create(dispatch, prognum, versnum, nconf)
142 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 (void) 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(fd, nconf, bindaddr, sendsz, recvsz)
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 (__binddynport(fd) == -1) {
226 warnx(
227 "svc_tli_create: could not bind to anonymous port");
228 goto freedata;
229 }
230 listen(fd, SOMAXCONN);
231 } else {
232 if (bind(fd,
233 (struct sockaddr *)bindaddr->addr.buf,
234 (socklen_t)si.si_alen) < 0) {
235 warnx(
236 "svc_tli_create: could not bind to requested address");
237 goto freedata;
238 }
239 listen(fd, (int)bindaddr->qlen);
240 }
241
242 }
243 /*
244 * call transport specific function.
245 */
246 switch (si.si_socktype) {
247 case SOCK_STREAM:
248 slen = sizeof ss;
249 if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen)
250 == 0) {
251 /* accepted socket */
252 xprt = svc_fd_create(fd, sendsz, recvsz);
253 } else
254 xprt = svc_vc_create(fd, sendsz, recvsz);
255 if (!nconf || !xprt)
256 break;
257 #if 0
258 /* XXX fvdl */
259 if (strcmp(nconf->nc_protofmly, "inet") == 0 ||
260 strcmp(nconf->nc_protofmly, "inet6") == 0)
261 (void) __svc_vc_setflag(xprt, TRUE);
262 #endif
263 break;
264 case SOCK_DGRAM:
265 xprt = svc_dg_create(fd, sendsz, recvsz);
266 break;
267 default:
268 warnx("svc_tli_create: bad service type");
269 goto freedata;
270 }
271
272 if (xprt == NULL)
273 /*
274 * The error messages here are spitted out by the lower layers:
275 * svc_vc_create(), svc_fd_create() and svc_dg_create().
276 */
277 goto freedata;
278
279 /* Fill in type of service */
280 xprt->xp_type = __rpc_socktype2seman(si.si_socktype);
281
282 if (nconf) {
283 if (xprt->xp_netid != NULL)
284 free(xprt->xp_netid);
285 xprt->xp_netid = strdup(nconf->nc_netid);
286 xprt->xp_tp = strdup(nconf->nc_device);
287 }
288 return (xprt);
289
290 freedata:
291 if (madefd)
292 (void)close(fd);
293 if (xprt) {
294 if (!madefd) /* so that svc_destroy doesnt close fd */
295 xprt->xp_fd = RPC_ANYFD;
296 SVC_DESTROY(xprt);
297 }
298 return (NULL);
299 }