clnt_udp.c 11 KB

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