pmap.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #include "pmap.h"
  10. #include "clnt.h"
  11. #include <rpc/rpc.h>
  12. static struct timeval timeout = { 5, 0 };
  13. static struct timeval tottimeout = { 60, 0 };
  14. bool_t xdr_pmap(XDR *xdrs, struct pmap *regs)
  15. {
  16. if (xdr_u_long(xdrs, &regs->pm_prog) &&
  17. xdr_u_long(xdrs, &regs->pm_vers) &&
  18. xdr_u_long(xdrs, &regs->pm_prot))
  19. return (xdr_u_long(xdrs, &regs->pm_port));
  20. return (FALSE);
  21. }
  22. /*
  23. * Find the mapped port for program,version.
  24. * Calls the pmap service remotely to do the lookup.
  25. * Returns 0 if no map exists.
  26. */
  27. unsigned short pmap_getport(struct sockaddr_in *address, unsigned long program, unsigned long version, unsigned int protocol)
  28. {
  29. unsigned short port = 0;
  30. int socket = -1;
  31. register CLIENT *client = RT_NULL;
  32. struct pmap parms;
  33. address->sin_port = htons((unsigned short)PMAPPORT);
  34. if (protocol == IPPROTO_UDP)
  35. client = clntudp_bufcreate(address, PMAPPROG, PMAPVERS, timeout,
  36. &socket, RPCSMALLMSGSIZE,
  37. RPCSMALLMSGSIZE);
  38. if (client != (CLIENT *) NULL)
  39. {
  40. parms.pm_prog = program;
  41. parms.pm_vers = version;
  42. parms.pm_prot = protocol;
  43. parms.pm_port = 0; /* not needed or used */
  44. if (CLNT_CALL(client, PMAPPROC_GETPORT, (xdrproc_t)xdr_pmap, (char*)&parms,
  45. (xdrproc_t)xdr_u_short, (char*)&port, tottimeout) != RPC_SUCCESS)
  46. {
  47. rt_kprintf("pmap failure\n");
  48. }
  49. CLNT_DESTROY(client);
  50. }
  51. (void) lwip_close(socket);
  52. address->sin_port = 0;
  53. return (port);
  54. }