1
0

clnt_udp.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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
  188. clntudp_call(CLIENT *cl,
  189. unsigned long proc,
  190. xdrproc_t xargs, char* argsp,
  191. xdrproc_t xresults, char* resultsp,
  192. struct timeval utimeout)
  193. {
  194. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  195. register XDR *xdrs;
  196. register int outlen;
  197. register int inlen;
  198. struct timeval singlewait;
  199. socklen_t fromlen;
  200. struct sockaddr_in from;
  201. struct rpc_msg reply_msg;
  202. XDR reply_xdrs;
  203. bool_t ok;
  204. int nrefreshes = 2; /* number of times to refresh cred */
  205. call_again:
  206. xdrs = &(cu->cu_outxdrs);
  207. xdrs->x_op = XDR_ENCODE;
  208. XDR_SETPOS(xdrs, cu->cu_xdrpos);
  209. /*
  210. * the transaction is the first thing in the out buffer
  211. */
  212. (*(uint32_t *) (cu->cu_outbuf))++;
  213. if ((!XDR_PUTLONG(xdrs, (long *) &proc)) ||
  214. (!AUTH_MARSHALL(cl->cl_auth, xdrs)) || (!(*xargs) (xdrs, argsp)))
  215. return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
  216. outlen = (int) XDR_GETPOS(xdrs);
  217. send_again:
  218. if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0,
  219. (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen)
  220. != outlen)
  221. {
  222. cu->cu_error.re_errno = errno;
  223. return (cu->cu_error.re_status = RPC_CANTSEND);
  224. }
  225. /*
  226. * sub-optimal code appears here because we have
  227. * some clock time to spare while the packets are in flight.
  228. * (We assume that this is actually only executed once.)
  229. */
  230. reply_msg.acpted_rply.ar_verf = _null_auth;
  231. reply_msg.acpted_rply.ar_results.where = resultsp;
  232. reply_msg.acpted_rply.ar_results.proc = xresults;
  233. /* do recv */
  234. do
  235. {
  236. fromlen = sizeof(struct sockaddr);
  237. inlen = recvfrom(cu->cu_sock, cu->cu_inbuf,
  238. (int) cu->cu_recvsz, 0,
  239. (struct sockaddr *) &from, &fromlen);
  240. }while (inlen < 0 && errno == EINTR);
  241. if (inlen < 4)
  242. {
  243. rt_kprintf("recv error, len %d\n", inlen);
  244. cu->cu_error.re_errno = errno;
  245. return (cu->cu_error.re_status = RPC_CANTRECV);
  246. }
  247. /* see if reply transaction id matches sent id */
  248. if (*((uint32_t *) (cu->cu_inbuf)) != *((uint32_t *) (cu->cu_outbuf)))
  249. goto send_again;
  250. /* we now assume we have the proper reply */
  251. /*
  252. * now decode and validate the response
  253. */
  254. xdrmem_create(&reply_xdrs, cu->cu_inbuf, (unsigned int) inlen, XDR_DECODE);
  255. ok = xdr_replymsg(&reply_xdrs, &reply_msg);
  256. /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
  257. if (ok)
  258. {
  259. _seterr_reply(&reply_msg, &(cu->cu_error));
  260. if (cu->cu_error.re_status == RPC_SUCCESS)
  261. {
  262. if (!AUTH_VALIDATE(cl->cl_auth,
  263. &reply_msg.acpted_rply.ar_verf))
  264. {
  265. cu->cu_error.re_status = RPC_AUTHERROR;
  266. cu->cu_error.re_why = AUTH_INVALIDRESP;
  267. }
  268. if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
  269. {
  270. xdrs->x_op = XDR_FREE;
  271. (void) xdr_opaque_auth(xdrs,
  272. &(reply_msg.acpted_rply.ar_verf));
  273. }
  274. } /* end successful completion */
  275. else
  276. {
  277. /* maybe our credentials need to be refreshed ... */
  278. if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth))
  279. {
  280. nrefreshes--;
  281. goto call_again;
  282. }
  283. } /* end of unsuccessful completion */
  284. } /* end of valid reply message */
  285. else
  286. {
  287. cu->cu_error.re_status = RPC_CANTDECODERES;
  288. }
  289. return (cu->cu_error.re_status);
  290. }
  291. static void clntudp_geterr(cl, errp)
  292. CLIENT *cl;
  293. struct rpc_err *errp;
  294. {
  295. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  296. *errp = cu->cu_error;
  297. }
  298. static bool_t clntudp_freeres(cl, xdr_res, res_ptr)
  299. CLIENT *cl;
  300. xdrproc_t xdr_res;
  301. char* res_ptr;
  302. {
  303. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  304. register XDR *xdrs = &(cu->cu_outxdrs);
  305. xdrs->x_op = XDR_FREE;
  306. return ((*xdr_res) (xdrs, res_ptr));
  307. }
  308. static void clntudp_abort( /*h */ )
  309. /*CLIENT *h; */
  310. {
  311. }
  312. static bool_t clntudp_control(cl, request, info)
  313. CLIENT *cl;
  314. int request;
  315. char *info;
  316. {
  317. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  318. switch (request)
  319. {
  320. case CLSET_TIMEOUT:
  321. cu->cu_total = *(struct timeval *) info;
  322. /* set socket option */
  323. setsockopt(cu->cu_sock, SOL_SOCKET, SO_RCVTIMEO, &cu->cu_total, sizeof(cu->cu_total));
  324. break;
  325. case CLGET_TIMEOUT:
  326. *(struct timeval *) info = cu->cu_total;
  327. break;
  328. case CLSET_RETRY_TIMEOUT:
  329. cu->cu_wait = *(struct timeval *) info;
  330. break;
  331. case CLGET_RETRY_TIMEOUT:
  332. *(struct timeval *) info = cu->cu_wait;
  333. break;
  334. case CLGET_SERVER_ADDR:
  335. *(struct sockaddr_in *) info = cu->cu_raddr;
  336. break;
  337. default:
  338. return (FALSE);
  339. }
  340. return (TRUE);
  341. }
  342. static void clntudp_destroy(cl)
  343. CLIENT *cl;
  344. {
  345. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  346. if (cu->cu_closeit)
  347. {
  348. lwip_close(cu->cu_sock);
  349. }
  350. XDR_DESTROY(&(cu->cu_outxdrs));
  351. rt_free(cu);
  352. rt_free(cl);
  353. }