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