clnt_udp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. /* @(#)clnt_udp.c 2.2 88/08/01 4.0 RPCSRC */
  10. /*
  11. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  12. * unrestricted use provided that this legend is included on all tape
  13. * media and as a part of the software program in whole or part. Users
  14. * may copy or modify Sun RPC without charge, but are not authorized
  15. * to license or distribute it to anyone else except as part of a product or
  16. * program developed by the user.
  17. *
  18. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  19. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  20. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  21. *
  22. * Sun RPC is provided with no support and without any obligation on the
  23. * part of Sun Microsystems, Inc. to assist in its use, correction,
  24. * modification or enhancement.
  25. *
  26. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  27. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  28. * OR ANY PART THEREOF.
  29. *
  30. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  31. * or profits or other special, indirect and consequential damages, even if
  32. * Sun has been advised of the possibility of such damages.
  33. *
  34. * Sun Microsystems, Inc.
  35. * 2550 Garcia Avenue
  36. * Mountain View, California 94043
  37. */
  38. #if !defined(lint) && defined(SCCSIDS)
  39. static char sccsid[] = "@(#)clnt_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro";
  40. #endif
  41. /*
  42. * clnt_udp.c, Implements a UDP/IP based, client side RPC.
  43. *
  44. * Copyright (C) 1984, Sun Microsystems, Inc.
  45. */
  46. #include <stdio.h>
  47. #include <rpc/rpc.h>
  48. #include <rtthread.h>
  49. /*
  50. * UDP bases client side rpc operations
  51. */
  52. static enum clnt_stat clntudp_call(register CLIENT *cl, /* client handle */
  53. unsigned long proc, /* procedure number */
  54. xdrproc_t xargs, /* xdr routine for args */
  55. char* argsp, /* pointer to args */
  56. xdrproc_t xresults, /* xdr routine for results */
  57. char* resultsp, /* pointer to results */
  58. struct timeval utimeout);
  59. static void clntudp_abort(void);
  60. static void clntudp_geterr(CLIENT *, struct rpc_err *);
  61. static bool_t clntudp_freeres(CLIENT *, xdrproc_t, char*);
  62. static bool_t clntudp_control(CLIENT *, int, char *);
  63. static void clntudp_destroy(CLIENT *);
  64. static struct clnt_ops udp_ops =
  65. {
  66. clntudp_call,
  67. clntudp_abort,
  68. clntudp_geterr,
  69. clntudp_freeres,
  70. clntudp_destroy,
  71. clntudp_control
  72. };
  73. /*
  74. * Private data kept per client handle
  75. */
  76. struct cu_data
  77. {
  78. int cu_sock;
  79. bool_t cu_closeit;
  80. struct sockaddr_in cu_raddr;
  81. int cu_rlen;
  82. struct timeval cu_wait;
  83. struct timeval cu_total;
  84. struct rpc_err cu_error;
  85. XDR cu_outxdrs;
  86. unsigned int cu_xdrpos;
  87. unsigned int cu_sendsz;
  88. char *cu_outbuf;
  89. unsigned int cu_recvsz;
  90. char cu_inbuf[1];
  91. };
  92. /*
  93. * Create a UDP based client handle.
  94. * If *sockp<0, *sockp is set to a newly created UPD socket.
  95. * If raddr->sin_port is 0 a binder on the remote machine
  96. * is consulted for the correct port number.
  97. * NB: It is the clients responsibility to close *sockp.
  98. * NB: The rpch->cl_auth is initialized to null authentication.
  99. * Caller may wish to set this something more useful.
  100. *
  101. * wait is the amount of time used between retransmitting a call if
  102. * no response has been heard; retransmition occurs until the actual
  103. * rpc call times out.
  104. *
  105. * sendsz and recvsz are the maximum allowable packet sizes that can be
  106. * sent and received.
  107. */
  108. CLIENT *clntudp_bufcreate(struct sockaddr_in *raddr,
  109. unsigned long program,
  110. unsigned long version,
  111. struct timeval wait,
  112. int *sockp,
  113. unsigned int sendsz,
  114. unsigned int recvsz)
  115. {
  116. CLIENT *cl;
  117. register struct cu_data *cu = NULL;
  118. struct rpc_msg call_msg;
  119. static int xid_count = 0;
  120. cl = (CLIENT *) rt_malloc (sizeof(CLIENT));
  121. if (cl == NULL)
  122. {
  123. rt_kprintf("clntudp_create: out of memory\n");
  124. goto fooy;
  125. }
  126. sendsz = ((sendsz + 3) / 4) * 4;
  127. recvsz = ((recvsz + 3) / 4) * 4;
  128. cu = (struct cu_data *) rt_malloc (sizeof(*cu) + sendsz + recvsz);
  129. if (cu == NULL)
  130. {
  131. rt_kprintf("clntudp_create: out of memory\n");
  132. goto fooy;
  133. }
  134. cu->cu_outbuf = &cu->cu_inbuf[recvsz];
  135. if (raddr->sin_port == 0) {
  136. unsigned short port;
  137. extern unsigned short pmap_getport(struct sockaddr_in *address,
  138. unsigned long program,
  139. unsigned long version,
  140. unsigned int protocol);
  141. if ((port =
  142. pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
  143. rt_kprintf("pmap_getport failure\n");
  144. goto fooy;
  145. }
  146. raddr->sin_port = htons(port);
  147. }
  148. cl->cl_ops = &udp_ops;
  149. cl->cl_private = (char*) cu;
  150. cu->cu_raddr = *raddr;
  151. cu->cu_rlen = sizeof(cu->cu_raddr);
  152. cu->cu_wait = wait;
  153. cu->cu_total.tv_sec = -1;
  154. cu->cu_total.tv_usec = -1;
  155. cu->cu_sendsz = sendsz;
  156. cu->cu_recvsz = recvsz;
  157. call_msg.rm_xid = (uint32_t)(((unsigned long)rt_thread_self()) ^ ((unsigned long)rt_tick_get()) ^ (xid_count++));
  158. call_msg.rm_direction = CALL;
  159. call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  160. call_msg.rm_call.cb_prog = program;
  161. call_msg.rm_call.cb_vers = version;
  162. xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
  163. if (!xdr_callhdr(&(cu->cu_outxdrs), &call_msg))
  164. {
  165. rt_kprintf("xdr_callhdr failure\n");
  166. goto fooy;
  167. }
  168. cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
  169. if (*sockp < 0)
  170. {
  171. *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  172. if (*sockp < 0)
  173. {
  174. rt_kprintf("create socket error\n");
  175. goto fooy;
  176. }
  177. cu->cu_closeit = TRUE;
  178. }
  179. else
  180. {
  181. cu->cu_closeit = FALSE;
  182. }
  183. cu->cu_sock = *sockp;
  184. cl->cl_auth = authnone_create();
  185. return (cl);
  186. fooy:
  187. if (cu) rt_free(cu);
  188. if (cl) rt_free(cl);
  189. return ((CLIENT *) NULL);
  190. }
  191. CLIENT *clntudp_create(struct sockaddr_in *raddr,
  192. unsigned long program,
  193. unsigned long version,
  194. struct timeval wait,
  195. int *sockp)
  196. {
  197. return (clntudp_bufcreate(raddr, program, version, wait, sockp,
  198. UDPMSGSIZE, UDPMSGSIZE));
  199. }
  200. static enum clnt_stat clntudp_call(CLIENT *cl, unsigned long proc,
  201. xdrproc_t xargs, char* argsp,
  202. xdrproc_t xresults, char* resultsp,
  203. struct timeval utimeout)
  204. {
  205. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  206. register XDR *xdrs;
  207. register int outlen;
  208. register int inlen;
  209. socklen_t fromlen;
  210. struct sockaddr_in from;
  211. struct rpc_msg reply_msg;
  212. XDR reply_xdrs;
  213. bool_t ok;
  214. int nrefreshes = 2; /* number of times to refresh cred */
  215. call_again:
  216. xdrs = &(cu->cu_outxdrs);
  217. xdrs->x_op = XDR_ENCODE;
  218. XDR_SETPOS(xdrs, cu->cu_xdrpos);
  219. /*
  220. * the transaction is the first thing in the out buffer
  221. */
  222. (*(unsigned long *) (cu->cu_outbuf))++;
  223. if ((!XDR_PUTLONG(xdrs, (long *) &proc)) ||
  224. (!AUTH_MARSHALL(cl->cl_auth, xdrs)) || (!(*xargs) (xdrs, argsp)))
  225. {
  226. cu->cu_error.re_status = RPC_CANTENCODEARGS;
  227. return RPC_CANTENCODEARGS;
  228. }
  229. outlen = (int) XDR_GETPOS(xdrs);
  230. send_again:
  231. if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0,
  232. (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen)
  233. != outlen)
  234. {
  235. cu->cu_error.re_errno = errno;
  236. cu->cu_error.re_status = RPC_CANTSEND;
  237. return RPC_CANTSEND;
  238. }
  239. /*
  240. * sub-optimal code appears here because we have
  241. * some clock time to spare while the packets are in flight.
  242. * (We assume that this is actually only executed once.)
  243. */
  244. reply_msg.acpted_rply.ar_verf = _null_auth;
  245. reply_msg.acpted_rply.ar_results.where = resultsp;
  246. reply_msg.acpted_rply.ar_results.proc = xresults;
  247. /* do recv */
  248. do
  249. {
  250. fromlen = sizeof(struct sockaddr);
  251. inlen = recvfrom(cu->cu_sock, cu->cu_inbuf,
  252. (int) cu->cu_recvsz, 0,
  253. (struct sockaddr *) &from, &fromlen);
  254. }while (inlen < 0 && errno == EINTR);
  255. if (inlen < 4)
  256. {
  257. rt_kprintf("recv error, len %d\n", inlen);
  258. cu->cu_error.re_errno = errno;
  259. cu->cu_error.re_status = RPC_CANTRECV;
  260. return RPC_CANTRECV;
  261. }
  262. /* see if reply transaction id matches sent id */
  263. if (*((uint32_t *) (cu->cu_inbuf)) != *((uint32_t *) (cu->cu_outbuf)))
  264. goto send_again;
  265. /* we now assume we have the proper reply */
  266. /*
  267. * now decode and validate the response
  268. */
  269. xdrmem_create(&reply_xdrs, cu->cu_inbuf, (unsigned int) inlen, XDR_DECODE);
  270. ok = xdr_replymsg(&reply_xdrs, &reply_msg);
  271. /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */
  272. if (ok)
  273. {
  274. _seterr_reply(&reply_msg, &(cu->cu_error));
  275. if (cu->cu_error.re_status == RPC_SUCCESS)
  276. {
  277. if (!AUTH_VALIDATE(cl->cl_auth,
  278. &reply_msg.acpted_rply.ar_verf))
  279. {
  280. cu->cu_error.re_status = RPC_AUTHERROR;
  281. cu->cu_error.re_why = AUTH_INVALIDRESP;
  282. }
  283. if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
  284. {
  285. extern bool_t xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap);
  286. xdrs->x_op = XDR_FREE;
  287. (void) xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf));
  288. }
  289. } /* end successful completion */
  290. else
  291. {
  292. /* maybe our credentials need to be refreshed ... */
  293. if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth))
  294. {
  295. nrefreshes--;
  296. goto call_again;
  297. }
  298. } /* end of unsuccessful completion */
  299. } /* end of valid reply message */
  300. else
  301. {
  302. cu->cu_error.re_status = RPC_CANTDECODERES;
  303. }
  304. return (enum clnt_stat)(cu->cu_error.re_status);
  305. }
  306. static void clntudp_geterr(CLIENT *cl, struct rpc_err *errp)
  307. {
  308. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  309. *errp = cu->cu_error;
  310. }
  311. static bool_t clntudp_freeres(CLIENT *cl, xdrproc_t xdr_res, char* res_ptr)
  312. {
  313. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  314. register XDR *xdrs = &(cu->cu_outxdrs);
  315. xdrs->x_op = XDR_FREE;
  316. return ((*xdr_res) (xdrs, res_ptr));
  317. }
  318. static void clntudp_abort()
  319. {
  320. }
  321. static bool_t clntudp_control(CLIENT *cl, int request, char *info)
  322. {
  323. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  324. switch (request)
  325. {
  326. case CLSET_TIMEOUT:
  327. {
  328. int mtimeout;
  329. cu->cu_total = *(struct timeval *) info;
  330. mtimeout = ((cu->cu_total.tv_sec * 1000) + ((cu->cu_total.tv_usec + 500)/1000));
  331. /* set socket option, note: lwip only support msecond timeout */
  332. setsockopt(cu->cu_sock, SOL_SOCKET, SO_RCVTIMEO,
  333. &mtimeout, sizeof(mtimeout));
  334. }
  335. break;
  336. case CLGET_TIMEOUT:
  337. *(struct timeval *) info = cu->cu_total;
  338. break;
  339. case CLSET_RETRY_TIMEOUT:
  340. cu->cu_wait = *(struct timeval *) info;
  341. break;
  342. case CLGET_RETRY_TIMEOUT:
  343. *(struct timeval *) info = cu->cu_wait;
  344. break;
  345. case CLGET_SERVER_ADDR:
  346. *(struct sockaddr_in *) info = cu->cu_raddr;
  347. break;
  348. default:
  349. return (FALSE);
  350. }
  351. return (TRUE);
  352. }
  353. static void clntudp_destroy(CLIENT *cl)
  354. {
  355. register struct cu_data *cu = (struct cu_data *) cl->cl_private;
  356. if (cu->cu_closeit)
  357. {
  358. lwip_close(cu->cu_sock);
  359. }
  360. XDR_DESTROY(&(cu->cu_outxdrs));
  361. rt_free(cu);
  362. rt_free(cl);
  363. }