1 /* $NetBSD: pmap_getport.c,v 1.19 2013/03/11 20:19:29 tron Exp $ */
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
37 static char *sccsid
= "@(#)pmap_getport.c 1.9 87/08/11 Copyr 1984 Sun Micro";
38 static char *sccsid
= "@(#)pmap_getport.c 2.2 88/08/01 4.0 RPCSRC";
40 __RCSID("$NetBSD: pmap_getport.c,v 1.19 2013/03/11 20:19:29 tron Exp $");
46 * Client interface to pmap rpc service.
48 * Copyright (C) 1984, Sun Microsystems, Inc.
51 #include "namespace.h"
53 #include <sys/types.h>
54 #include <sys/socket.h>
62 #include <rpc/pmap_prot.h>
63 #include <rpc/pmap_clnt.h>
66 __weak_alias(pmap_getport
,_pmap_getport
)
69 static const struct timeval timeout
= { 5, 0 };
70 static const struct timeval tottimeout
= { 60, 0 };
73 * Find the mapped port for program,version.
74 * Calls the pmap service remotely to do the lookup.
75 * Returns 0 if no map exists.
79 remote_pmap_getport(CLIENT
*client
, struct pmap
*parms
, u_short
*port
)
81 if (CLNT_CALL(client
, (rpcproc_t
)PMAPPROC_GETPORT
, (xdrproc_t
)xdr_pmap
,
82 parms
, (xdrproc_t
)xdr_u_short
, port
, tottimeout
) != RPC_SUCCESS
) {
83 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
84 clnt_geterr(client
, &rpc_createerr
.cf_error
);
85 } else if (*port
== 0) {
86 rpc_createerr
.cf_stat
= RPC_PROGNOTREGISTERED
;
87 clnt_geterr(client
, &rpc_createerr
.cf_error
);
93 get_client(struct sockaddr_in
*address
, int tcp
)
97 return clnttcp_create(address
, PMAPPROG
, PMAPVERS
, &sock
, 0, 0);
99 return clntudp_bufcreate(address
, PMAPPROG
, PMAPVERS
, timeout
,
100 &sock
, RPCSMALLMSGSIZE
, RPCSMALLMSGSIZE
);
104 pmap_getport(struct sockaddr_in
*address
, u_long program
, u_long version
,
111 _DIAGASSERT(address
!= NULL
);
113 parms
.pm_prog
= program
;
114 parms
.pm_vers
= version
;
115 parms
.pm_prot
= protocol
;
116 parms
.pm_port
= 0; /* not needed or used */
118 address
->sin_port
= htons(PMAPPORT
);
120 client
= get_client(address
, protocol
== IPPROTO_TCP
);
122 remote_pmap_getport(client
, &parms
, &port
);
125 client
= get_client(address
, protocol
!= IPPROTO_TCP
);
127 remote_pmap_getport(client
, &parms
, &port
);
130 address
->sin_port
= 0;