PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

lib/libc/rpc/svc_generic.c

http://www.minix3.org/
C | 320 lines | 215 code | 22 blank | 83 comment | 53 complexity | bef3ea8400fd3d870160f97dec3af7d7 MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD
  1. /* $NetBSD: svc_generic.c,v 1.10 2008/04/25 17:44:44 christos Exp $ */
  2. /*
  3. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4. * unrestricted use provided that this legend is included on all tape
  5. * media and as a part of the software program in whole or part. Users
  6. * may copy or modify Sun RPC without charge, but are not authorized
  7. * to license or distribute it to anyone else except as part of a product or
  8. * program developed by the user.
  9. *
  10. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13. *
  14. * Sun RPC is provided with no support and without any obligation on the
  15. * part of Sun Microsystems, Inc. to assist in its use, correction,
  16. * modification or enhancement.
  17. *
  18. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20. * OR ANY PART THEREOF.
  21. *
  22. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23. * or profits or other special, indirect and consequential damages, even if
  24. * Sun has been advised of the possibility of such damages.
  25. *
  26. * Sun Microsystems, Inc.
  27. * 2550 Garcia Avenue
  28. * Mountain View, California 94043
  29. */
  30. /*
  31. * Copyright (c) 1986-1991 by Sun Microsystems Inc.
  32. */
  33. /* #ident "@(#)svc_generic.c 1.19 94/04/24 SMI" */
  34. #include <sys/cdefs.h>
  35. #if defined(LIBC_SCCS) && !defined(lint)
  36. #if 0
  37. static char sccsid[] = "@(#)svc_generic.c 1.21 89/02/28 Copyr 1988 Sun Micro";
  38. #else
  39. __RCSID("$NetBSD: svc_generic.c,v 1.10 2008/04/25 17:44:44 christos Exp $");
  40. #endif
  41. #endif
  42. /*
  43. * svc_generic.c, Server side for RPC.
  44. *
  45. */
  46. #include "namespace.h"
  47. #include "reentrant.h"
  48. #include <sys/types.h>
  49. #include <sys/socket.h>
  50. #include <netinet/in.h>
  51. #include <rpc/rpc.h>
  52. #include <rpc/nettype.h>
  53. #include <stdio.h>
  54. #include <errno.h>
  55. #include <malloc.h>
  56. #include <string.h>
  57. #include <unistd.h>
  58. #include <err.h>
  59. #include "rpc_internal.h"
  60. #ifdef __weak_alias
  61. __weak_alias(svc_create,_svc_create)
  62. __weak_alias(svc_tp_create,_svc_tp_create)
  63. __weak_alias(svc_tli_create,_svc_tli_create)
  64. #endif
  65. extern int __svc_vc_setflag __P((SVCXPRT *, int));
  66. /*
  67. * The highest level interface for server creation.
  68. * It tries for all the nettokens in that particular class of token
  69. * and returns the number of handles it can create and/or find.
  70. *
  71. * It creates a link list of all the handles it could create.
  72. * If svc_create() is called multiple times, it uses the handle
  73. * created earlier instead of creating a new handle every time.
  74. */
  75. int
  76. svc_create(dispatch, prognum, versnum, nettype)
  77. void (*dispatch) __P((struct svc_req *, SVCXPRT *));
  78. rpcprog_t prognum; /* Program number */
  79. rpcvers_t versnum; /* Version number */
  80. const char *nettype; /* Networktype token */
  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. #ifdef _REENTRANT
  92. extern mutex_t xprtlist_lock;
  93. #endif
  94. /* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */
  95. if ((handle = __rpc_setconf(nettype)) == NULL) {
  96. warnx("svc_create: unknown protocol");
  97. return (0);
  98. }
  99. while ((nconf = __rpc_getconf(handle)) != NULL) {
  100. mutex_lock(&xprtlist_lock);
  101. for (l = xprtlist; l; l = l->next) {
  102. if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
  103. /* Found an old one, use it */
  104. (void) rpcb_unset(prognum, versnum, nconf);
  105. if (svc_reg(l->xprt, prognum, versnum,
  106. dispatch, nconf) == FALSE)
  107. warnx(
  108. "svc_create: could not register prog %u vers %u on %s",
  109. (unsigned)prognum, (unsigned)versnum,
  110. nconf->nc_netid);
  111. else
  112. num++;
  113. break;
  114. }
  115. }
  116. if (l == NULL) {
  117. /* It was not found. Now create a new one */
  118. xprt = svc_tp_create(dispatch, prognum, versnum, nconf);
  119. if (xprt) {
  120. l = malloc(sizeof(*l));
  121. if (l == NULL) {
  122. warnx("svc_create: no memory");
  123. mutex_unlock(&xprtlist_lock);
  124. return (0);
  125. }
  126. l->xprt = xprt;
  127. l->next = xprtlist;
  128. xprtlist = l;
  129. num++;
  130. }
  131. }
  132. mutex_unlock(&xprtlist_lock);
  133. }
  134. __rpc_endconf(handle);
  135. /*
  136. * In case of num == 0; the error messages are generated by the
  137. * underlying layers; and hence not needed here.
  138. */
  139. return (num);
  140. }
  141. /*
  142. * The high level interface to svc_tli_create().
  143. * It tries to create a server for "nconf" and registers the service
  144. * with the rpcbind. It calls svc_tli_create();
  145. */
  146. SVCXPRT *
  147. svc_tp_create(dispatch, prognum, versnum, nconf)
  148. void (*dispatch) __P((struct svc_req *, SVCXPRT *));
  149. rpcprog_t prognum; /* Program number */
  150. rpcvers_t versnum; /* Version number */
  151. const struct netconfig *nconf; /* Netconfig structure for the network */
  152. {
  153. SVCXPRT *xprt;
  154. if (nconf == NULL) {
  155. warnx(
  156. "svc_tp_create: invalid netconfig structure for prog %u vers %u",
  157. (unsigned)prognum, (unsigned)versnum);
  158. return (NULL);
  159. }
  160. xprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
  161. if (xprt == NULL) {
  162. return (NULL);
  163. }
  164. (void) rpcb_unset(prognum, versnum, __UNCONST(nconf));
  165. if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
  166. warnx(
  167. "svc_tp_create: Could not register prog %u vers %u on %s",
  168. (unsigned)prognum, (unsigned)versnum,
  169. nconf->nc_netid);
  170. SVC_DESTROY(xprt);
  171. return (NULL);
  172. }
  173. return (xprt);
  174. }
  175. /*
  176. * If fd is RPC_ANYFD, then it opens a fd for the given transport
  177. * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
  178. * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
  179. * NULL bindadr and Connection oriented transports, the value of qlen
  180. * is set to 8.
  181. *
  182. * If sendsz or recvsz are zero, their default values are chosen.
  183. */
  184. SVCXPRT *
  185. svc_tli_create(fd, nconf, bindaddr, sendsz, recvsz)
  186. int fd; /* Connection end point */
  187. const struct netconfig *nconf; /* Netconfig struct for nettoken */
  188. const struct t_bind *bindaddr; /* Local bind address */
  189. u_int sendsz; /* Max sendsize */
  190. u_int recvsz; /* Max recvsize */
  191. {
  192. SVCXPRT *xprt = NULL; /* service handle */
  193. bool_t madefd = FALSE; /* whether fd opened here */
  194. struct __rpc_sockinfo si;
  195. struct sockaddr_storage ss;
  196. socklen_t slen;
  197. if (fd == RPC_ANYFD) {
  198. if (nconf == NULL) {
  199. warnx("svc_tli_create: invalid netconfig");
  200. return (NULL);
  201. }
  202. fd = __rpc_nconf2fd(nconf);
  203. if (fd == -1) {
  204. warnx(
  205. "svc_tli_create: could not open connection for %s",
  206. nconf->nc_netid);
  207. return (NULL);
  208. }
  209. __rpc_nconf2sockinfo(nconf, &si);
  210. madefd = TRUE;
  211. } else {
  212. /*
  213. * It is an open descriptor. Get the transport info.
  214. */
  215. if (!__rpc_fd2sockinfo(fd, &si)) {
  216. warnx(
  217. "svc_tli_create: could not get transport information");
  218. return (NULL);
  219. }
  220. }
  221. /*
  222. * If the fd is unbound, try to bind it.
  223. */
  224. if (madefd || !__rpc_sockisbound(fd)) {
  225. if (bindaddr == NULL) {
  226. if (bindresvport(fd, NULL) < 0) {
  227. memset(&ss, 0, sizeof ss);
  228. ss.ss_family = si.si_af;
  229. ss.ss_len = si.si_alen;
  230. if (bind(fd, (struct sockaddr *)(void *)&ss,
  231. (socklen_t)si.si_alen) < 0) {
  232. warnx(
  233. "svc_tli_create: could not bind to anonymous port");
  234. goto freedata;
  235. }
  236. }
  237. listen(fd, SOMAXCONN);
  238. } else {
  239. if (bind(fd,
  240. (struct sockaddr *)bindaddr->addr.buf,
  241. (socklen_t)si.si_alen) < 0) {
  242. warnx(
  243. "svc_tli_create: could not bind to requested address");
  244. goto freedata;
  245. }
  246. listen(fd, (int)bindaddr->qlen);
  247. }
  248. }
  249. /*
  250. * call transport specific function.
  251. */
  252. switch (si.si_socktype) {
  253. case SOCK_STREAM:
  254. slen = sizeof ss;
  255. if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen)
  256. == 0) {
  257. /* accepted socket */
  258. xprt = svc_fd_create(fd, sendsz, recvsz);
  259. } else
  260. xprt = svc_vc_create(fd, sendsz, recvsz);
  261. if (!nconf || !xprt)
  262. break;
  263. #if 0
  264. /* XXX fvdl */
  265. if (strcmp(nconf->nc_protofmly, "inet") == 0 ||
  266. strcmp(nconf->nc_protofmly, "inet6") == 0)
  267. (void) __svc_vc_setflag(xprt, TRUE);
  268. #endif
  269. break;
  270. case SOCK_DGRAM:
  271. xprt = svc_dg_create(fd, sendsz, recvsz);
  272. break;
  273. default:
  274. warnx("svc_tli_create: bad service type");
  275. goto freedata;
  276. }
  277. if (xprt == NULL)
  278. /*
  279. * The error messages here are spitted out by the lower layers:
  280. * svc_vc_create(), svc_fd_create() and svc_dg_create().
  281. */
  282. goto freedata;
  283. /* Fill in type of service */
  284. xprt->xp_type = __rpc_socktype2seman(si.si_socktype);
  285. if (nconf) {
  286. xprt->xp_netid = strdup(nconf->nc_netid);
  287. xprt->xp_tp = strdup(nconf->nc_device);
  288. if (xprt->xp_netid == NULL || xprt->xp_tp == NULL) {
  289. svc_destroy(xprt);
  290. return NULL;
  291. }
  292. }
  293. return (xprt);
  294. freedata:
  295. if (madefd)
  296. (void) close(fd);
  297. return (NULL);
  298. }