svc_dg.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* $NetBSD: svc_dg.c,v 1.4 2000/07/06 03:10:35 christos Exp $ */
  2. /*-
  3. * SPDX-License-Identifier: BSD-3-Clause
  4. *
  5. * Copyright (c) 2009, Sun Microsystems, Inc.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * - Neither the name of Sun Microsystems, Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * Copyright (c) 1986-1991 by Sun Microsystems Inc.
  33. */
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. #ident "@(#)svc_dg.c 1.17 94/04/24 SMI"
  36. #endif
  37. #include <sys/cdefs.h>
  38. __FBSDID("$FreeBSD$");
  39. /*
  40. * svc_dg.c, Server side for connectionless RPC.
  41. */
  42. #include <sys/param.h>
  43. #include <sys/lock.h>
  44. #include <sys/kernel.h>
  45. #include <sys/malloc.h>
  46. #include <sys/mbuf.h>
  47. #include <sys/mutex.h>
  48. #include <sys/protosw.h>
  49. #include <sys/queue.h>
  50. #include <sys/socket.h>
  51. #include <sys/socketvar.h>
  52. #include <sys/sx.h>
  53. #include <sys/systm.h>
  54. #include <sys/uio.h>
  55. #include <net/vnet.h>
  56. #include <rpc/rpc.h>
  57. #include <rpc/rpc_com.h>
  58. static enum xprt_stat svc_dg_stat(SVCXPRT *);
  59. static bool_t svc_dg_recv(SVCXPRT *, struct rpc_msg *,
  60. struct sockaddr **, struct mbuf **);
  61. static bool_t svc_dg_reply(SVCXPRT *, struct rpc_msg *,
  62. struct sockaddr *, struct mbuf *, uint32_t *);
  63. static void svc_dg_destroy(SVCXPRT *);
  64. static bool_t svc_dg_control(SVCXPRT *, const u_int, void *);
  65. static int svc_dg_soupcall(struct socket *so, void *arg, int waitflag);
  66. static struct xp_ops svc_dg_ops = {
  67. .xp_recv = svc_dg_recv,
  68. .xp_stat = svc_dg_stat,
  69. .xp_reply = svc_dg_reply,
  70. .xp_destroy = svc_dg_destroy,
  71. .xp_control = svc_dg_control,
  72. };
  73. /*
  74. * Usage:
  75. * xprt = svc_dg_create(sock, sendsize, recvsize);
  76. * Does other connectionless specific initializations.
  77. * Once *xprt is initialized, it is registered.
  78. * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable
  79. * system defaults are chosen.
  80. * The routines returns NULL if a problem occurred.
  81. */
  82. static const char svc_dg_str[] = "svc_dg_create: %s";
  83. static const char svc_dg_err1[] = "could not get transport information";
  84. static const char svc_dg_err2[] = "transport does not support data transfer";
  85. static const char __no_mem_str[] = "out of memory";
  86. SVCXPRT *
  87. svc_dg_create(SVCPOOL *pool, struct socket *so, size_t sendsize,
  88. size_t recvsize)
  89. {
  90. SVCXPRT *xprt;
  91. struct __rpc_sockinfo si;
  92. struct sockaddr* sa;
  93. int error;
  94. if (!__rpc_socket2sockinfo(so, &si)) {
  95. printf(svc_dg_str, svc_dg_err1);
  96. return (NULL);
  97. }
  98. /*
  99. * Find the receive and the send size
  100. */
  101. sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
  102. recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
  103. if ((sendsize == 0) || (recvsize == 0)) {
  104. printf(svc_dg_str, svc_dg_err2);
  105. return (NULL);
  106. }
  107. xprt = svc_xprt_alloc();
  108. sx_init(&xprt->xp_lock, "xprt->xp_lock");
  109. xprt->xp_pool = pool;
  110. xprt->xp_socket = so;
  111. xprt->xp_p1 = NULL;
  112. xprt->xp_p2 = NULL;
  113. xprt->xp_ops = &svc_dg_ops;
  114. CURVNET_SET(so->so_vnet);
  115. error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
  116. CURVNET_RESTORE();
  117. if (error)
  118. goto freedata;
  119. memcpy(&xprt->xp_ltaddr, sa, sa->sa_len);
  120. free(sa, M_SONAME);
  121. xprt_register(xprt);
  122. SOCKBUF_LOCK(&so->so_rcv);
  123. soupcall_set(so, SO_RCV, svc_dg_soupcall, xprt);
  124. SOCKBUF_UNLOCK(&so->so_rcv);
  125. return (xprt);
  126. freedata:
  127. (void) printf(svc_dg_str, __no_mem_str);
  128. svc_xprt_free(xprt);
  129. return (NULL);
  130. }
  131. /*ARGSUSED*/
  132. static enum xprt_stat
  133. svc_dg_stat(SVCXPRT *xprt)
  134. {
  135. if (soreadable(xprt->xp_socket))
  136. return (XPRT_MOREREQS);
  137. return (XPRT_IDLE);
  138. }
  139. static bool_t
  140. svc_dg_recv(SVCXPRT *xprt, struct rpc_msg *msg,
  141. struct sockaddr **addrp, struct mbuf **mp)
  142. {
  143. struct uio uio;
  144. struct sockaddr *raddr;
  145. struct mbuf *mreq;
  146. XDR xdrs;
  147. int error, rcvflag;
  148. /*
  149. * Serialise access to the socket.
  150. */
  151. sx_xlock(&xprt->xp_lock);
  152. /*
  153. * The socket upcall calls xprt_active() which will eventually
  154. * cause the server to call us here. We attempt to read a
  155. * packet from the socket and process it. If the read fails,
  156. * we have drained all pending requests so we call
  157. * xprt_inactive().
  158. */
  159. uio.uio_resid = 1000000000;
  160. uio.uio_td = curthread;
  161. mreq = NULL;
  162. rcvflag = MSG_DONTWAIT;
  163. error = soreceive(xprt->xp_socket, &raddr, &uio, &mreq, NULL, &rcvflag);
  164. if (error == EWOULDBLOCK) {
  165. /*
  166. * We must re-test for readability after taking the
  167. * lock to protect us in the case where a new packet
  168. * arrives on the socket after our call to soreceive
  169. * fails with EWOULDBLOCK. The pool lock protects us
  170. * from racing the upcall after our soreadable() call
  171. * returns false.
  172. */
  173. SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
  174. if (!soreadable(xprt->xp_socket))
  175. xprt_inactive_self(xprt);
  176. SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
  177. sx_xunlock(&xprt->xp_lock);
  178. return (FALSE);
  179. }
  180. if (error) {
  181. SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
  182. soupcall_clear(xprt->xp_socket, SO_RCV);
  183. SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
  184. xprt_inactive_self(xprt);
  185. sx_xunlock(&xprt->xp_lock);
  186. return (FALSE);
  187. }
  188. sx_xunlock(&xprt->xp_lock);
  189. xdrmbuf_create(&xdrs, mreq, XDR_DECODE);
  190. if (! xdr_callmsg(&xdrs, msg)) {
  191. XDR_DESTROY(&xdrs);
  192. return (FALSE);
  193. }
  194. *addrp = raddr;
  195. *mp = xdrmbuf_getall(&xdrs);
  196. XDR_DESTROY(&xdrs);
  197. return (TRUE);
  198. }
  199. static bool_t
  200. svc_dg_reply(SVCXPRT *xprt, struct rpc_msg *msg,
  201. struct sockaddr *addr, struct mbuf *m, uint32_t *seq)
  202. {
  203. XDR xdrs;
  204. struct mbuf *mrep;
  205. bool_t stat = TRUE;
  206. int error;
  207. mrep = m_gethdr(M_WAITOK, MT_DATA);
  208. xdrmbuf_create(&xdrs, mrep, XDR_ENCODE);
  209. if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
  210. msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
  211. if (!xdr_replymsg(&xdrs, msg))
  212. stat = FALSE;
  213. else
  214. xdrmbuf_append(&xdrs, m);
  215. } else {
  216. stat = xdr_replymsg(&xdrs, msg);
  217. }
  218. if (stat) {
  219. m_fixhdr(mrep);
  220. error = sosend(xprt->xp_socket, addr, NULL, mrep, NULL,
  221. 0, curthread);
  222. if (!error) {
  223. stat = TRUE;
  224. }
  225. } else {
  226. m_freem(mrep);
  227. }
  228. XDR_DESTROY(&xdrs);
  229. xprt->xp_p2 = NULL;
  230. return (stat);
  231. }
  232. static void
  233. svc_dg_destroy(SVCXPRT *xprt)
  234. {
  235. SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
  236. soupcall_clear(xprt->xp_socket, SO_RCV);
  237. SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
  238. sx_destroy(&xprt->xp_lock);
  239. if (xprt->xp_socket)
  240. (void)soclose(xprt->xp_socket);
  241. if (xprt->xp_netid)
  242. (void) mem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
  243. svc_xprt_free(xprt);
  244. }
  245. static bool_t
  246. /*ARGSUSED*/
  247. svc_dg_control(xprt, rq, in)
  248. SVCXPRT *xprt;
  249. const u_int rq;
  250. void *in;
  251. {
  252. return (FALSE);
  253. }
  254. static int
  255. svc_dg_soupcall(struct socket *so, void *arg, int waitflag)
  256. {
  257. SVCXPRT *xprt = (SVCXPRT *) arg;
  258. xprt_active(xprt);
  259. return (SU_OK);
  260. }