xref: /freebsd/crypto/krb5/src/lib/rpc/pmap_getport.c (revision b670c9bafc0e31c7609969bf374b2e80bdc00211)
1 /* @(#)pmap_getport.c	2.2 88/08/01 4.0 RPCSRC */
2 /*
3  * Copyright (c) 2010, Oracle America, Inc.
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *
18  *     * Neither the name of the "Oracle America, Inc." nor the names of
19  *       its contributors may be used to endorse or promote products
20  *       derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 #if !defined(lint) && defined(SCCSIDS)
35 static char sccsid[] = "@(#)pmap_getport.c 1.9 87/08/11 Copyr 1984 Sun Micro";
36 #endif
37 
38 /*
39  * pmap_getport.c
40  * Client interface to pmap rpc service.
41  */
42 
43 #include <unistd.h>
44 #include <gssrpc/rpc.h>
45 #include <gssrpc/pmap_prot.h>
46 #include <gssrpc/pmap_clnt.h>
47 #include <sys/socket.h>
48 #ifdef OSF1
49 #include <net/route.h>
50 #include <sys/mbuf.h>
51 #endif
52 #include <net/if.h>
53 
54 static struct timeval timeout = { 5, 0 };
55 static struct timeval tottimeout = { 60, 0 };
56 
57 /*
58  * Find the mapped port for program,version.
59  * Calls the pmap service remotely to do the lookup.
60  * Returns 0 if no map exists.
61  */
62 u_short
63 pmap_getport(
64 	struct sockaddr_in *address,
65 	rpcprog_t program,
66 	rpcvers_t version,
67 	rpcprot_t protocol)
68 {
69 	unsigned short port = 0;
70 	int sock = -1;
71 	CLIENT *client;
72 	struct pmap parms;
73 
74 	address->sin_port = htons(PMAPPORT);
75 	client = clntudp_bufcreate(address, PMAPPROG,
76 	    PMAPVERS, timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
77 	if (client != (CLIENT *)NULL) {
78 		parms.pm_prog = program;
79 		parms.pm_vers = version;
80 		parms.pm_prot = protocol;
81 		parms.pm_port = 0;  /* not needed or used */
82 		if (CLNT_CALL(client, PMAPPROC_GETPORT, xdr_pmap, &parms,
83 		    xdr_u_short, &port, tottimeout) != RPC_SUCCESS){
84 			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
85 			clnt_geterr(client, &rpc_createerr.cf_error);
86 		} else if (port == 0) {
87 			rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
88 		}
89 		CLNT_DESTROY(client);
90 	}
91 	(void)close(sock);
92 	address->sin_port = 0;
93 	return (port);
94 }
95