1 /* $NetBSD: svc_simple.c,v 1.20 2000/07/06 03:10:35 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 * Copyright (c) 1986-1991 by Sun Microsystems Inc. 33 */ 34 35 /* #pragma ident "@(#)svc_simple.c 1.18 94/04/24 SMI" */ 36 37 /* 38 * svc_simple.c 39 * Simplified front end to rpc. 40 */ 41 42 /* 43 * This interface creates a virtual listener for all the services 44 * started thru rpc_reg(). It listens on the same endpoint for 45 * all the services and then executes the corresponding service 46 * for the given prognum and procnum. 47 */ 48 49 #include "namespace.h" 50 #include "reentrant.h" 51 #include <sys/types.h> 52 #include <rpc/rpc.h> 53 #include <rpc/nettype.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <err.h> 58 59 #include "rpc_com.h" 60 61 #ifdef __weak_alias 62 __weak_alias(rpc_reg,_rpc_reg) 63 #endif 64 65 static void universal __P((struct svc_req *, SVCXPRT *)); 66 67 static struct proglst { 68 char *(*p_progname) __P((char *)); 69 rpcprog_t p_prognum; 70 rpcvers_t p_versnum; 71 rpcproc_t p_procnum; 72 SVCXPRT *p_transp; 73 char *p_netid; 74 char *p_xdrbuf; 75 int p_recvsz; 76 xdrproc_t p_inproc, p_outproc; 77 struct proglst *p_nxt; 78 } *proglst; 79 80 static const char rpc_reg_err[] = "%s: %s"; 81 static const char rpc_reg_msg[] = "rpc_reg: "; 82 static const char __reg_err1[] = "can't find appropriate transport"; 83 static const char __reg_err2[] = "can't get protocol info"; 84 static const char __reg_err3[] = "unsupported transport size"; 85 static const char __no_mem_str[] = "out of memory"; 86 87 /* 88 * For simplified, easy to use kind of rpc interfaces. 89 * nettype indicates the type of transport on which the service will be 90 * listening. Used for conservation of the system resource. Only one 91 * handle is created for all the services (actually one of each netid) 92 * and same xdrbuf is used for same netid. The size of the arguments 93 * is also limited by the recvsize for that transport, even if it is 94 * a COTS transport. This may be wrong, but for cases like these, they 95 * should not use the simplified interfaces like this. 96 */ 97 98 int 99 rpc_reg(prognum, versnum, procnum, progname, inproc, outproc, nettype) 100 rpcprog_t prognum; /* program number */ 101 rpcvers_t versnum; /* version number */ 102 rpcproc_t procnum; /* procedure number */ 103 char *(*progname) __P((char *)); /* Server routine */ 104 xdrproc_t inproc, outproc; /* in/out XDR procedures */ 105 char *nettype; /* nettype */ 106 { 107 struct netconfig *nconf; 108 int done = FALSE; 109 void *handle; 110 #ifdef __REENT 111 extern mutex_t proglst_lock; 112 #endif 113 114 115 116 if (procnum == NULLPROC) { 117 warnx("%s can't reassign procedure number %u", rpc_reg_msg, 118 NULLPROC); 119 return (-1); 120 } 121 122 if (nettype == NULL) 123 nettype = "netpath"; /* The default behavior */ 124 if ((handle = __rpc_setconf(nettype)) == NULL) { 125 warnx(rpc_reg_err, rpc_reg_msg, __reg_err1); 126 return (-1); 127 } 128 /* VARIABLES PROTECTED BY proglst_lock: proglst */ 129 mutex_lock(&proglst_lock); 130 while ((nconf = __rpc_getconf(handle)) != NULL) { 131 struct proglst *pl; 132 SVCXPRT *svcxprt; 133 int madenow; 134 u_int recvsz; 135 char *xdrbuf; 136 char *netid; 137 138 madenow = FALSE; 139 svcxprt = NULL; 140 for (pl = proglst; pl; pl = pl->p_nxt) 141 if (strcmp(pl->p_netid, nconf->nc_netid) == 0) { 142 svcxprt = pl->p_transp; 143 xdrbuf = pl->p_xdrbuf; 144 recvsz = pl->p_recvsz; 145 netid = pl->p_netid; 146 break; 147 } 148 149 if (svcxprt == NULL) { 150 struct __rpc_sockinfo si; 151 152 svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0); 153 if (svcxprt == NULL) 154 continue; 155 if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) { 156 warnx(rpc_reg_err, rpc_reg_msg, __reg_err2); 157 SVC_DESTROY(svcxprt); 158 continue; 159 } 160 recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0); 161 if (recvsz == 0) { 162 warnx(rpc_reg_err, rpc_reg_msg, __reg_err3); 163 SVC_DESTROY(svcxprt); 164 continue; 165 } 166 if (((xdrbuf = malloc((unsigned)recvsz)) == NULL) || 167 ((netid = strdup(nconf->nc_netid)) == NULL)) { 168 warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str); 169 SVC_DESTROY(svcxprt); 170 break; 171 } 172 madenow = TRUE; 173 } 174 /* 175 * Check if this (program, version, netid) had already been 176 * registered. The check may save a few RPC calls to rpcbind 177 */ 178 for (pl = proglst; pl; pl = pl->p_nxt) 179 if ((pl->p_prognum == prognum) && 180 (pl->p_versnum == versnum) && 181 (strcmp(pl->p_netid, netid) == 0)) 182 break; 183 if (pl == NULL) { /* Not yet */ 184 (void) rpcb_unset(prognum, versnum, nconf); 185 } else { 186 /* so that svc_reg does not call rpcb_set() */ 187 nconf = NULL; 188 } 189 190 if (!svc_reg(svcxprt, prognum, versnum, universal, nconf)) { 191 warnx("%s couldn't register prog %u vers %u for %s", 192 rpc_reg_msg, (unsigned)prognum, 193 (unsigned)versnum, netid); 194 if (madenow) { 195 SVC_DESTROY(svcxprt); 196 free(xdrbuf); 197 free(netid); 198 } 199 continue; 200 } 201 202 pl = malloc(sizeof (struct proglst)); 203 if (pl == NULL) { 204 warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str); 205 if (madenow) { 206 SVC_DESTROY(svcxprt); 207 free(xdrbuf); 208 free(netid); 209 } 210 break; 211 } 212 pl->p_progname = progname; 213 pl->p_prognum = prognum; 214 pl->p_versnum = versnum; 215 pl->p_procnum = procnum; 216 pl->p_inproc = inproc; 217 pl->p_outproc = outproc; 218 pl->p_transp = svcxprt; 219 pl->p_xdrbuf = xdrbuf; 220 pl->p_recvsz = recvsz; 221 pl->p_netid = netid; 222 pl->p_nxt = proglst; 223 proglst = pl; 224 done = TRUE; 225 } 226 __rpc_endconf(handle); 227 mutex_unlock(&proglst_lock); 228 229 if (done == FALSE) { 230 warnx("%s cant find suitable transport for %s", 231 rpc_reg_msg, nettype); 232 return (-1); 233 } 234 return (0); 235 } 236 237 /* 238 * The universal handler for the services registered using registerrpc. 239 * It handles both the connectionless and the connection oriented cases. 240 */ 241 242 static void 243 universal(rqstp, transp) 244 struct svc_req *rqstp; 245 SVCXPRT *transp; 246 { 247 rpcprog_t prog; 248 rpcvers_t vers; 249 rpcproc_t proc; 250 char *outdata; 251 char *xdrbuf; 252 struct proglst *pl; 253 #ifdef __REENT 254 extern mutex_t proglst_lock; 255 #endif 256 257 /* 258 * enforce "procnum 0 is echo" convention 259 */ 260 if (rqstp->rq_proc == NULLPROC) { 261 if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) == 262 FALSE) { 263 warnx("svc_sendreply failed"); 264 } 265 return; 266 } 267 prog = rqstp->rq_prog; 268 vers = rqstp->rq_vers; 269 proc = rqstp->rq_proc; 270 mutex_lock(&proglst_lock); 271 for (pl = proglst; pl; pl = pl->p_nxt) 272 if (pl->p_prognum == prog && pl->p_procnum == proc && 273 pl->p_versnum == vers && 274 (strcmp(pl->p_netid, transp->xp_netid) == 0)) { 275 /* decode arguments into a CLEAN buffer */ 276 xdrbuf = pl->p_xdrbuf; 277 /* Zero the arguments: reqd ! */ 278 (void) memset(xdrbuf, 0, sizeof (pl->p_recvsz)); 279 /* 280 * Assuming that sizeof (xdrbuf) would be enough 281 * for the arguments; if not then the program 282 * may bomb. BEWARE! 283 */ 284 if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) { 285 svcerr_decode(transp); 286 mutex_unlock(&proglst_lock); 287 return; 288 } 289 outdata = (*(pl->p_progname))(xdrbuf); 290 if (outdata == NULL && 291 pl->p_outproc != (xdrproc_t) xdr_void){ 292 /* there was an error */ 293 mutex_unlock(&proglst_lock); 294 return; 295 } 296 if (!svc_sendreply(transp, pl->p_outproc, outdata)) { 297 warnx( 298 "rpc: rpc_reg trouble replying to prog %u vers %u", 299 (unsigned)prog, (unsigned)vers); 300 mutex_unlock(&proglst_lock); 301 return; 302 } 303 /* free the decoded arguments */ 304 (void) svc_freeargs(transp, pl->p_inproc, xdrbuf); 305 mutex_unlock(&proglst_lock); 306 return; 307 } 308 mutex_unlock(&proglst_lock); 309 /* This should never happen */ 310 warnx("rpc: rpc_reg: never registered prog %u vers %u", 311 (unsigned)prog, (unsigned)vers); 312 return; 313 } 314