clnt_udp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. extern unsigned short pmap_getport(struct sockaddr_in *address,
  130. unsigned long program,
  131. unsigned long version,
  132. unsigned int protocol);
  133. if ((port =
  134. pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
  135. goto fooy;
  136. }
  137. raddr->sin_port = htons(port);
  138. }
  139. cl->cl_ops = &udp_ops;
  140. cl->cl_private = (char*) cu;
  141. cu->cu_raddr = *raddr;
  142. cu->cu_rlen = sizeof(cu->cu_raddr);
  143. cu->cu_wait = wait;
  144. cu->cu_total.tv_sec = -1;
  145. cu->cu_total.tv_usec = -1;
  146. cu->cu_sendsz = sendsz;
  147. cu->cu_recvsz = recvsz;
  148. call_msg.rm_xid = ((unsigned long)rt_thread_self()) ^ ((unsigned long)rt_tick_get()) ^ (xid_count++);
  149. call_msg.rm_direction = CALL;
  150. call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  151. call_msg.rm_call.cb_prog = program;
  152. call_msg.rm_call.cb_vers = version;
  153. xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
  154. if (!xdr_callhdr(&(cu->cu_outxdrs), &call_msg))
  155. {
  156. goto fooy;
  157. }
  158. cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
  159. if (*sockp < 0)
  160. {
  161. *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  162. if (*sockp < 0)
  163. {
  164. rt_kprintf("create socket error\n");
  165. goto fooy;
  166. }
  167. cu->cu_closeit = TRUE;
  168. }
  169. else
  170. {
  171. cu->cu_closeit = FALSE;
  172. }
  173. cu->cu_sock = *sockp;
  174. cl->cl_auth = authnone_create();
  175. return (cl);
  176. fooy:
  177. if (cu) rt_free(cu);
  178. if (cl) rt_free(cl);
  179. return ((CLIENT *) NULL);
  180. }
  181. CLIENT *clntudp_create(struct sockaddr_in *raddr,
  182. unsigned long program,
  183. unsigned long version,
  184. struct timeval wait,
  185. int *sockp)
  186. {
  187. return (clntudp_bufcreate(raddr, program, version, wait, sockp,
  188. UDPMSGSIZE, UDPMSGSIZE));
  189. }
  190. static enum clnt_stat clntudp_call(CLIENT *cl, unsigned long proc,
  191. xdrproc_t xargs, char* argsp,
  192. xdrproc_t xresults, char* resultsp,
  193. struct timeval utimeout)
  194. {
  195. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  196. register XDR *xdrs;
  197. register int outlen;
  198. register int inlen;
  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. (*(unsigned long *) (cu->cu_outbuf))++;
  213. if ((!XDR_PUTLONG(xdrs, (long *) &proc)) ||
  214. (!AUTH_MARSHALL(cl->cl_auth, xdrs)) || (!(*xargs) (xdrs, argsp)))
  215. {
  216. cu->cu_error.re_status = RPC_CANTENCODEARGS;
  217. return RPC_CANTENCODEARGS;
  218. }
  219. outlen = (int) XDR_GETPOS(xdrs);
  220. send_again:
  221. if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0,
  222. (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen)
  223. != outlen)
  224. {
  225. cu->cu_error.re_errno = errno;
  226. cu->cu_error.re_status = RPC_CANTSEND;
  227. return RPC_CANTSEND;
  228. }
  229. /*
  230. * sub-optimal code appears here because we have
  231. * some clock time to spare while the packets are in flight.
  232. * (We assume that this is actually only executed once.)
  233. */
  234. reply_msg.acpted_rply.ar_verf = _null_auth;
  235. reply_msg.acpted_rply.ar_results.where = resultsp;
  236. reply_msg.acpted_rply.ar_results.proc = xresults;
  237. /* do recv */
  238. do
  239. {
  240. fromlen = sizeof(struct sockaddr);
  241. inlen = recvfrom(cu->cu_sock, cu->cu_inbuf,
  242. (int) cu->cu_recvsz, 0,
  243. (struct sockaddr *) &from, &fromlen);
  244. }while (inlen < 0 && errno == EINTR);
  245. if (inlen < 4)
  246. {
  247. rt_kprintf("recv error, len %d\n", inlen);
  248. cu->cu_error.re_errno = errno;
  249. cu->cu_error.re_status = RPC_CANTRECV;
  250. return RPC_CANTRECV;
  251. }
  252. /* see if reply transaction id matches sent id */
  253. if (*((uint32_t *) (cu->cu_inbuf)) != *((uint32_t *) (cu->cu_outbuf)))
  254. goto send_again;
  255. /* we now assume we have the proper reply */
  256. /*
  257. * now decode and validate the response
  258. */
  259. xdrmem_create(&reply_xdrs, cu->cu_inbuf, (unsigned int) inlen, XDR_DECODE);
  260. ok = xdr_replymsg(&reply_xdrs, &reply_msg);
  261. /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
  262. if (ok)
  263. {
  264. _seterr_reply(&reply_msg, &(cu->cu_error));
  265. if (cu->cu_error.re_status == RPC_SUCCESS)
  266. {
  267. if (!AUTH_VALIDATE(cl->cl_auth,
  268. &reply_msg.acpted_rply.ar_verf))
  269. {
  270. cu->cu_error.re_status = RPC_AUTHERROR;
  271. cu->cu_error.re_why = AUTH_INVALIDRESP;
  272. }
  273. if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
  274. {
  275. extern bool_t xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap);
  276. xdrs->x_op = XDR_FREE;
  277. (void) xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf));
  278. }
  279. } /* end successful completion */
  280. else
  281. {
  282. /* maybe our credentials need to be refreshed ... */
  283. if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth))
  284. {
  285. nrefreshes--;
  286. goto call_again;
  287. }
  288. } /* end of unsuccessful completion */
  289. } /* end of valid reply message */
  290. else
  291. {
  292. cu->cu_error.re_status = RPC_CANTDECODERES;
  293. }
  294. return (enum clnt_stat)(cu->cu_error.re_status);
  295. }
  296. static void clntudp_geterr(CLIENT *cl, struct rpc_err *errp)
  297. {
  298. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  299. *errp = cu->cu_error;
  300. }
  301. static bool_t clntudp_freeres(CLIENT *cl, xdrproc_t xdr_res, 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()
  309. {
  310. }
  311. static bool_t clntudp_control(CLIENT *cl, int request, char *info)
  312. {
  313. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  314. switch (request)
  315. {
  316. case CLSET_TIMEOUT:
  317. {
  318. int mtimeout;
  319. cu->cu_total = *(struct timeval *) info;
  320. mtimeout = ((cu->cu_total.tv_sec * 1000) + ((cu->cu_total.tv_usec + 500)/1000));
  321. /* set socket option, note: lwip only support msecond timeout */
  322. setsockopt(cu->cu_sock, SOL_SOCKET, SO_RCVTIMEO,
  323. &mtimeout, sizeof(mtimeout));
  324. }
  325. break;
  326. case CLGET_TIMEOUT:
  327. *(struct timeval *) info = cu->cu_total;
  328. break;
  329. case CLSET_RETRY_TIMEOUT:
  330. cu->cu_wait = *(struct timeval *) info;
  331. break;
  332. case CLGET_RETRY_TIMEOUT:
  333. *(struct timeval *) info = cu->cu_wait;
  334. break;
  335. case CLGET_SERVER_ADDR:
  336. *(struct sockaddr_in *) info = cu->cu_raddr;
  337. break;
  338. default:
  339. return (FALSE);
  340. }
  341. return (TRUE);
  342. }
  343. static void clntudp_destroy(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. }