1 /* $NetBSD: svc_generic.c,v 1.14 2013/03/05 19:55:23 christos Exp $ */ 2 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. 10 * 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. 14 * 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. 18 * 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. 22 * 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. 26 * 27 * Sun Microsystems, Inc. 28 * 2550 Garcia Avenue 29 * Mountain View, California 94043 30 */ 31 32 /* 33 * Copyright (c) 1986-1991 by Sun Microsystems Inc. 34 */ 35 36 /* #ident "@(#)svc_generic.c 1.19 94/04/24 SMI" */ 37 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.14 2013/03/05 19:55:23 christos Exp $"); 44 #endif 45 #endif 46 47 /* 48 * svc_generic.c, Server side for RPC. 49 * 50 */ 51 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 <stdlib.h> 62 #include <string.h> 63 #include <unistd.h> 64 #include <err.h> 65 66 #include "svc_fdset.h" 67 #include "rpc_internal.h" 68 69 #ifdef __weak_alias 70 __weak_alias(svc_create,_svc_create) 71 __weak_alias(svc_tp_create,_svc_tp_create) 72 __weak_alias(svc_tli_create,_svc_tli_create) 73 #endif 74 75 extern int __svc_vc_setflag(SVCXPRT *, int); 76 77 /* 78 * The highest level interface for server creation. 79 * It tries for all the nettokens in that particular class of token 80 * and returns the number of handles it can create and/or find. 81 * 82 * It creates a link list of all the handles it could create. 83 * If svc_create() is called multiple times, it uses the handle 84 * created earlier instead of creating a new handle every time. 85 */ 86 int 87 svc_create( 88 void (*dispatch)(struct svc_req *, SVCXPRT *), 89 rpcprog_t prognum, /* Program number */ 90 rpcvers_t versnum, /* Version number */ 91 const char *nettype) /* Networktype token */ 92 { 93 struct xlist { 94 SVCXPRT *xprt; /* Server handle */ 95 struct xlist *next; /* Next item */ 96 } *l; 97 static struct xlist *xprtlist; /* A link list of all the handles */ 98 int num = 0; 99 SVCXPRT *xprt; 100 struct netconfig *nconf; 101 void *handle; 102 #ifdef _REENTRANT 103 extern mutex_t xprtlist_lock; 104 #endif 105 106 /* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */ 107 108 if ((handle = __rpc_setconf(nettype)) == NULL) { 109 warnx("%s: unknown protocol %s", __func__, nettype); 110 return (0); 111 } 112 while ((nconf = __rpc_getconf(handle)) != NULL) { 113 mutex_lock(&xprtlist_lock); 114 for (l = xprtlist; l; l = l->next) { 115 if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) { 116 /* Found an old one, use it */ 117 (void) rpcb_unset(prognum, versnum, nconf); 118 if (svc_reg(l->xprt, prognum, versnum, 119 dispatch, nconf) == FALSE) 120 warnx("%s: could not register prog %u " 121 "vers %u on %s", __func__, 122 (unsigned)prognum, 123 (unsigned)versnum, nconf->nc_netid); 124 else 125 num++; 126 break; 127 } 128 } 129 if (l == NULL) { 130 /* It was not found. Now create a new one */ 131 xprt = svc_tp_create(dispatch, prognum, versnum, nconf); 132 if (xprt) { 133 l = malloc(sizeof(*l)); 134 if (l == NULL) { 135 warn("%s: out of memory", __func__); 136 mutex_unlock(&xprtlist_lock); 137 return (0); 138 } 139 l->xprt = xprt; 140 l->next = xprtlist; 141 xprtlist = l; 142 num++; 143 } 144 } 145 mutex_unlock(&xprtlist_lock); 146 } 147 __rpc_endconf(handle); 148 /* 149 * In case of num == 0; the error messages are generated by the 150 * underlying layers; and hence not needed here. 151 */ 152 return (num); 153 } 154 155 /* 156 * The high level interface to svc_tli_create(). 157 * It tries to create a server for "nconf" and registers the service 158 * with the rpcbind. It calls svc_tli_create(); 159 */ 160 SVCXPRT * 161 svc_tp_create( 162 void (*dispatch)(struct svc_req *, SVCXPRT *), 163 rpcprog_t prognum, /* Program number */ 164 rpcvers_t versnum, /* Version number */ 165 const struct netconfig *nconf) /* Netconfig structure for the network */ 166 { 167 SVCXPRT *xprt; 168 169 if (nconf == NULL) { 170 warnx("%s: invalid netconfig structure for prog %u vers %u", 171 __func__, (unsigned)prognum, (unsigned)versnum); 172 return (NULL); 173 } 174 xprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0); 175 if (xprt == NULL) { 176 return (NULL); 177 } 178 (void) rpcb_unset(prognum, versnum, __UNCONST(nconf)); 179 if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) { 180 warnx("%s: Could not register prog %u vers %u on %s", 181 __func__, (unsigned)prognum, (unsigned)versnum, 182 nconf->nc_netid); 183 SVC_DESTROY(xprt); 184 return (NULL); 185 } 186 return (xprt); 187 } 188 189 /* 190 * If fd is RPC_ANYFD, then it opens a fd for the given transport 191 * provider (nconf cannot be NULL then). If the t_state is T_UNBND and 192 * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For 193 * NULL bindadr and Connection oriented transports, the value of qlen 194 * is set to 8. 195 * 196 * If sendsz or recvsz are zero, their default values are chosen. 197 */ 198 SVCXPRT * 199 svc_tli_create( 200 int fd, /* Connection end point */ 201 const struct netconfig *nconf, /* Netconfig struct for nettoken */ 202 const struct t_bind *bindaddr, /* Local bind address */ 203 u_int sendsz, /* Max sendsize */ 204 u_int recvsz) /* Max recvsize */ 205 { 206 SVCXPRT *xprt = NULL; /* service handle */ 207 bool_t madefd = FALSE; /* whether fd opened here */ 208 struct __rpc_sockinfo si; 209 struct sockaddr_storage ss; 210 socklen_t slen; 211 212 if (fd == RPC_ANYFD) { 213 if (nconf == NULL) { 214 warnx("%s: invalid netconfig", __func__); 215 return (NULL); 216 } 217 fd = __rpc_nconf2fd(nconf); 218 if (fd == -1) { 219 warnx("%s: could not open connection for %s", __func__, 220 nconf->nc_netid); 221 return (NULL); 222 } 223 __rpc_nconf2sockinfo(nconf, &si); 224 madefd = TRUE; 225 } else { 226 /* 227 * It is an open descriptor. Get the transport info. 228 */ 229 if (!__rpc_fd2sockinfo(fd, &si)) { 230 warnx("%s: could not get transport information", 231 __func__); 232 return (NULL); 233 } 234 } 235 236 /* 237 * If the fd is unbound, try to bind it. 238 */ 239 if (madefd || !__rpc_sockisbound(fd)) { 240 if (bindaddr == NULL) { 241 if (bindresvport(fd, NULL) < 0) { 242 memset(&ss, 0, sizeof ss); 243 ss.ss_family = si.si_af; 244 ss.ss_len = si.si_alen; 245 if (bind(fd, (struct sockaddr *)(void *)&ss, 246 (socklen_t)si.si_alen) < 0) { 247 warn( "%s: could not bind to anonymous " 248 "port", __func__); 249 goto freedata; 250 } 251 } 252 listen(fd, SOMAXCONN); 253 } else { 254 if (bind(fd, 255 (struct sockaddr *)bindaddr->addr.buf, 256 (socklen_t)si.si_alen) < 0) { 257 warnx("%s: could not bind to requested address", 258 __func__); 259 goto freedata; 260 } 261 listen(fd, (int)bindaddr->qlen); 262 } 263 264 } 265 /* 266 * call transport specific function. 267 */ 268 switch (si.si_socktype) { 269 case SOCK_STREAM: 270 slen = sizeof ss; 271 if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) 272 == 0) { 273 /* accepted socket */ 274 xprt = svc_fd_create(fd, sendsz, recvsz); 275 } else 276 xprt = svc_vc_create(fd, sendsz, recvsz); 277 if (!nconf || !xprt) 278 break; 279 #if 0 280 /* XXX fvdl */ 281 if (strcmp(nconf->nc_protofmly, "inet") == 0 || 282 strcmp(nconf->nc_protofmly, "inet6") == 0) 283 (void) __svc_vc_setflag(xprt, TRUE); 284 #endif 285 break; 286 case SOCK_DGRAM: 287 xprt = svc_dg_create(fd, sendsz, recvsz); 288 break; 289 default: 290 warnx("%s: bad service type %u", __func__, si.si_socktype); 291 goto freedata; 292 } 293 294 if (xprt == NULL) 295 /* 296 * The error messages here are spitted out by the lower layers: 297 * svc_vc_create(), svc_fd_create() and svc_dg_create(). 298 */ 299 goto freedata; 300 301 /* Fill in type of service */ 302 xprt->xp_type = __rpc_socktype2seman(si.si_socktype); 303 304 if (nconf) { 305 xprt->xp_netid = strdup(nconf->nc_netid); 306 xprt->xp_tp = strdup(nconf->nc_device); 307 if (xprt->xp_netid == NULL || xprt->xp_tp == NULL) { 308 svc_destroy(xprt); 309 return NULL; 310 } 311 } 312 return (xprt); 313 314 freedata: 315 if (madefd) 316 (void) close(fd); 317 return (NULL); 318 } 319