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