clnt_udp.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* @(#)clnt_udp.c 2.2 88/08/01 4.0 RPCSRC */
  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. #if !defined(lint) && defined(SCCSIDS)
  31. static char sccsid[] = "@(#)clnt_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33. /*
  34. * clnt_udp.c, Implements a UDP/IP based, client side RPC.
  35. *
  36. * Copyright (C) 1984, Sun Microsystems, Inc.
  37. */
  38. #include <stdio.h>
  39. #include <rpc/rpc.h>
  40. #include <rtthread.h>
  41. /*
  42. * UDP bases client side rpc operations
  43. */
  44. static enum clnt_stat clntudp_call(register CLIENT *cl, /* client handle */
  45. unsigned long proc, /* procedure number */
  46. xdrproc_t xargs, /* xdr routine for args */
  47. char* argsp, /* pointer to args */
  48. xdrproc_t xresults, /* xdr routine for results */
  49. char* resultsp, /* pointer to results */
  50. struct timeval utimeout);
  51. static void clntudp_abort(void);
  52. static void clntudp_geterr(CLIENT *, struct rpc_err *);
  53. static bool_t clntudp_freeres(CLIENT *, xdrproc_t, char*);
  54. static bool_t clntudp_control(CLIENT *, int, char *);
  55. static void clntudp_destroy(CLIENT *);
  56. static struct clnt_ops udp_ops =
  57. {
  58. clntudp_call,
  59. clntudp_abort,
  60. clntudp_geterr,
  61. clntudp_freeres,
  62. clntudp_destroy,
  63. clntudp_control
  64. };
  65. /*
  66. * Private data kept per client handle
  67. */
  68. struct cu_data
  69. {
  70. int cu_sock;
  71. bool_t cu_closeit;
  72. struct sockaddr_in cu_raddr;
  73. int cu_rlen;
  74. struct timeval cu_wait;
  75. struct timeval cu_total;
  76. struct rpc_err cu_error;
  77. XDR cu_outxdrs;
  78. unsigned int cu_xdrpos;
  79. unsigned int cu_sendsz;
  80. char *cu_outbuf;
  81. unsigned int cu_recvsz;
  82. char cu_inbuf[1];
  83. };
  84. /*
  85. * Create a UDP based client handle.
  86. * If *sockp<0, *sockp is set to a newly created UPD socket.
  87. * If raddr->sin_port is 0 a binder on the remote machine
  88. * is consulted for the correct port number.
  89. * NB: It is the clients responsibility to close *sockp.
  90. * NB: The rpch->cl_auth is initialized to null authentication.
  91. * Caller may wish to set this something more useful.
  92. *
  93. * wait is the amount of time used between retransmitting a call if
  94. * no response has been heard; retransmition occurs until the actual
  95. * rpc call times out.
  96. *
  97. * sendsz and recvsz are the maximum allowable packet sizes that can be
  98. * sent and received.
  99. */
  100. CLIENT *clntudp_bufcreate(struct sockaddr_in *raddr,
  101. unsigned long program,
  102. unsigned long version,
  103. struct timeval wait,
  104. int *sockp,
  105. unsigned int sendsz,
  106. unsigned int recvsz)
  107. {
  108. CLIENT *cl;
  109. register struct cu_data *cu = NULL;
  110. struct rpc_msg call_msg;
  111. static int xid_count = 0;
  112. cl = (CLIENT *) rt_malloc (sizeof(CLIENT));
  113. if (cl == NULL)
  114. {
  115. rt_kprintf("clntudp_create: out of memory\n");
  116. goto fooy;
  117. }
  118. sendsz = ((sendsz + 3) / 4) * 4;
  119. recvsz = ((recvsz + 3) / 4) * 4;
  120. cu = (struct cu_data *) rt_malloc (sizeof(*cu) + sendsz + recvsz);
  121. if (cu == NULL)
  122. {
  123. rt_kprintf("clntudp_create: out of memory\n");
  124. goto fooy;
  125. }
  126. cu->cu_outbuf = &cu->cu_inbuf[recvsz];
  127. if (raddr->sin_port == 0) {
  128. unsigned short port;
  129. if ((port =
  130. pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
  131. goto fooy;
  132. }
  133. raddr->sin_port = htons(port);
  134. }
  135. cl->cl_ops = &udp_ops;
  136. cl->cl_private = (char*) cu;
  137. cu->cu_raddr = *raddr;
  138. cu->cu_rlen = sizeof(cu->cu_raddr);
  139. cu->cu_wait = wait;
  140. cu->cu_total.tv_sec = -1;
  141. cu->cu_total.tv_usec = -1;
  142. cu->cu_sendsz = sendsz;
  143. cu->cu_recvsz = recvsz;
  144. call_msg.rm_xid = ((unsigned long)rt_thread_self()) ^ ((unsigned long)rt_tick_get()) ^ (xid_count++);
  145. call_msg.rm_direction = CALL;
  146. call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  147. call_msg.rm_call.cb_prog = program;
  148. call_msg.rm_call.cb_vers = version;
  149. xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
  150. if (!xdr_callhdr(&(cu->cu_outxdrs), &call_msg))
  151. {
  152. goto fooy;
  153. }
  154. cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
  155. if (*sockp < 0)
  156. {
  157. int dontblock = 1;
  158. *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  159. if (*sockp < 0)
  160. {
  161. rt_kprintf("create socket error\n");
  162. goto fooy;
  163. }
  164. cu->cu_closeit = TRUE;
  165. }
  166. else
  167. {
  168. cu->cu_closeit = FALSE;
  169. }
  170. cu->cu_sock = *sockp;
  171. cl->cl_auth = authnone_create();
  172. return (cl);
  173. fooy:
  174. if (cu) rt_free(cu);
  175. if (cl) rt_free(cl);
  176. return ((CLIENT *) NULL);
  177. }
  178. CLIENT *clntudp_create(struct sockaddr_in *raddr,
  179. unsigned long program,
  180. unsigned long version,
  181. struct timeval wait,
  182. int *sockp)
  183. {
  184. return (clntudp_bufcreate(raddr, program, version, wait, sockp,
  185. UDPMSGSIZE, UDPMSGSIZE));
  186. }
  187. static enum clnt_stat clntudp_call(CLIENT *cl, unsigned long proc,
  188. xdrproc_t xargs, char* argsp,
  189. xdrproc_t xresults, char* resultsp,
  190. struct timeval utimeout)
  191. {
  192. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  193. register XDR *xdrs;
  194. register int outlen;
  195. register int inlen;
  196. socklen_t fromlen;
  197. struct sockaddr_in from;
  198. struct rpc_msg reply_msg;
  199. XDR reply_xdrs;
  200. bool_t ok;
  201. int nrefreshes = 2; /* number of times to refresh cred */
  202. call_again:
  203. xdrs = &(cu->cu_outxdrs);
  204. xdrs->x_op = XDR_ENCODE;
  205. XDR_SETPOS(xdrs, cu->cu_xdrpos);
  206. /*
  207. * the transaction is the first thing in the out buffer
  208. */
  209. (*(unsigned long *) (cu->cu_outbuf))++;
  210. if ((!XDR_PUTLONG(xdrs, (long *) &proc)) ||
  211. (!AUTH_MARSHALL(cl->cl_auth, xdrs)) || (!(*xargs) (xdrs, argsp)))
  212. return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
  213. outlen = (int) XDR_GETPOS(xdrs);
  214. send_again:
  215. if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0,
  216. (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen)
  217. != outlen)
  218. {
  219. cu->cu_error.re_errno = errno;
  220. return (cu->cu_error.re_status = RPC_CANTSEND);
  221. }
  222. /*
  223. * sub-optimal code appears here because we have
  224. * some clock time to spare while the packets are in flight.
  225. * (We assume that this is actually only executed once.)
  226. */
  227. reply_msg.acpted_rply.ar_verf = _null_auth;
  228. reply_msg.acpted_rply.ar_results.where = resultsp;
  229. reply_msg.acpted_rply.ar_results.proc = xresults;
  230. /* do recv */
  231. do
  232. {
  233. fromlen = sizeof(struct sockaddr);
  234. inlen = recvfrom(cu->cu_sock, cu->cu_inbuf,
  235. (int) cu->cu_recvsz, 0,
  236. (struct sockaddr *) &from, &fromlen);
  237. }while (inlen < 0 && errno == EINTR);
  238. if (inlen < 4)
  239. {
  240. rt_kprintf("recv error, len %d\n", inlen);
  241. cu->cu_error.re_errno = errno;
  242. return (cu->cu_error.re_status = RPC_CANTRECV);
  243. }
  244. /* see if reply transaction id matches sent id */
  245. if (*((uint32_t *) (cu->cu_inbuf)) != *((uint32_t *) (cu->cu_outbuf)))
  246. goto send_again;
  247. /* we now assume we have the proper reply */
  248. /*
  249. * now decode and validate the response
  250. */
  251. xdrmem_create(&reply_xdrs, cu->cu_inbuf, (unsigned int) inlen, XDR_DECODE);
  252. ok = xdr_replymsg(&reply_xdrs, &reply_msg);
  253. /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
  254. if (ok)
  255. {
  256. _seterr_reply(&reply_msg, &(cu->cu_error));
  257. if (cu->cu_error.re_status == RPC_SUCCESS)
  258. {
  259. if (!AUTH_VALIDATE(cl->cl_auth,
  260. &reply_msg.acpted_rply.ar_verf))
  261. {
  262. cu->cu_error.re_status = RPC_AUTHERROR;
  263. cu->cu_error.re_why = AUTH_INVALIDRESP;
  264. }
  265. if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
  266. {
  267. xdrs->x_op = XDR_FREE;
  268. (void) xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf));
  269. }
  270. } /* end successful completion */
  271. else
  272. {
  273. /* maybe our credentials need to be refreshed ... */
  274. if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth))
  275. {
  276. nrefreshes--;
  277. goto call_again;
  278. }
  279. } /* end of unsuccessful completion */
  280. } /* end of valid reply message */
  281. else
  282. {
  283. cu->cu_error.re_status = RPC_CANTDECODERES;
  284. }
  285. return (cu->cu_error.re_status);
  286. }
  287. static void clntudp_geterr(CLIENT *cl, struct rpc_err *errp)
  288. {
  289. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  290. *errp = cu->cu_error;
  291. }
  292. static bool_t clntudp_freeres(CLIENT *cl, xdrproc_t xdr_res, char* res_ptr)
  293. {
  294. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  295. register XDR *xdrs = &(cu->cu_outxdrs);
  296. xdrs->x_op = XDR_FREE;
  297. return ((*xdr_res) (xdrs, res_ptr));
  298. }
  299. static void clntudp_abort()
  300. {
  301. }
  302. static bool_t clntudp_control(CLIENT *cl, int request, char *info)
  303. {
  304. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  305. switch (request)
  306. {
  307. case CLSET_TIMEOUT:
  308. {
  309. int mtimeout;
  310. cu->cu_total = *(struct timeval *) info;
  311. mtimeout = ((cu->cu_total.tv_sec * 1000) + ((cu->cu_total.tv_usec + 500)/1000));
  312. /* set socket option, note: lwip only support msecond timeout */
  313. setsockopt(cu->cu_sock, SOL_SOCKET, SO_RCVTIMEO,
  314. &mtimeout, sizeof(mtimeout));
  315. }
  316. break;
  317. case CLGET_TIMEOUT:
  318. *(struct timeval *) info = cu->cu_total;
  319. break;
  320. case CLSET_RETRY_TIMEOUT:
  321. cu->cu_wait = *(struct timeval *) info;
  322. break;
  323. case CLGET_RETRY_TIMEOUT:
  324. *(struct timeval *) info = cu->cu_wait;
  325. break;
  326. case CLGET_SERVER_ADDR:
  327. *(struct sockaddr_in *) info = cu->cu_raddr;
  328. break;
  329. default:
  330. return (FALSE);
  331. }
  332. return (TRUE);
  333. }
  334. static void clntudp_destroy(CLIENT *cl)
  335. {
  336. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  337. if (cu->cu_closeit)
  338. {
  339. lwip_close(cu->cu_sock);
  340. }
  341. XDR_DESTROY(&(cu->cu_outxdrs));
  342. rt_free(cu);
  343. rt_free(cl);
  344. }