xref: /csrg-svn/lib/librpc/rpc/pmap_getport.c (revision 45084)
1*45084Smckusick /* @(#)pmap_getport.c	2.2 88/08/01 4.0 RPCSRC */
2*45084Smckusick /*
3*45084Smckusick  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4*45084Smckusick  * unrestricted use provided that this legend is included on all tape
5*45084Smckusick  * media and as a part of the software program in whole or part.  Users
6*45084Smckusick  * may copy or modify Sun RPC without charge, but are not authorized
7*45084Smckusick  * to license or distribute it to anyone else except as part of a product or
8*45084Smckusick  * program developed by the user.
9*45084Smckusick  *
10*45084Smckusick  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11*45084Smckusick  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12*45084Smckusick  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13*45084Smckusick  *
14*45084Smckusick  * Sun RPC is provided with no support and without any obligation on the
15*45084Smckusick  * part of Sun Microsystems, Inc. to assist in its use, correction,
16*45084Smckusick  * modification or enhancement.
17*45084Smckusick  *
18*45084Smckusick  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19*45084Smckusick  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20*45084Smckusick  * OR ANY PART THEREOF.
21*45084Smckusick  *
22*45084Smckusick  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23*45084Smckusick  * or profits or other special, indirect and consequential damages, even if
24*45084Smckusick  * Sun has been advised of the possibility of such damages.
25*45084Smckusick  *
26*45084Smckusick  * Sun Microsystems, Inc.
27*45084Smckusick  * 2550 Garcia Avenue
28*45084Smckusick  * Mountain View, California  94043
29*45084Smckusick  */
30*45084Smckusick #if !defined(lint) && defined(SCCSIDS)
31*45084Smckusick static char sccsid[] = "@(#)pmap_getport.c 1.9 87/08/11 Copyr 1984 Sun Micro";
32*45084Smckusick #endif
33*45084Smckusick 
34*45084Smckusick /*
35*45084Smckusick  * pmap_getport.c
36*45084Smckusick  * Client interface to pmap rpc service.
37*45084Smckusick  *
38*45084Smckusick  * Copyright (C) 1984, Sun Microsystems, Inc.
39*45084Smckusick  */
40*45084Smckusick 
41*45084Smckusick #include <rpc/rpc.h>
42*45084Smckusick #include <rpc/pmap_prot.h>
43*45084Smckusick #include <rpc/pmap_clnt.h>
44*45084Smckusick #include <sys/socket.h>
45*45084Smckusick #include <net/if.h>
46*45084Smckusick 
47*45084Smckusick static struct timeval timeout = { 5, 0 };
48*45084Smckusick static struct timeval tottimeout = { 60, 0 };
49*45084Smckusick 
50*45084Smckusick /*
51*45084Smckusick  * Find the mapped port for program,version.
52*45084Smckusick  * Calls the pmap service remotely to do the lookup.
53*45084Smckusick  * Returns 0 if no map exists.
54*45084Smckusick  */
55*45084Smckusick u_short
pmap_getport(address,program,version,protocol)56*45084Smckusick pmap_getport(address, program, version, protocol)
57*45084Smckusick 	struct sockaddr_in *address;
58*45084Smckusick 	u_long program;
59*45084Smckusick 	u_long version;
60*45084Smckusick 	u_int protocol;
61*45084Smckusick {
62*45084Smckusick 	u_short port = 0;
63*45084Smckusick 	int socket = -1;
64*45084Smckusick 	register CLIENT *client;
65*45084Smckusick 	struct pmap parms;
66*45084Smckusick 
67*45084Smckusick 	address->sin_port = htons(PMAPPORT);
68*45084Smckusick 	client = clntudp_bufcreate(address, PMAPPROG,
69*45084Smckusick 	    PMAPVERS, timeout, &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
70*45084Smckusick 	if (client != (CLIENT *)NULL) {
71*45084Smckusick 		parms.pm_prog = program;
72*45084Smckusick 		parms.pm_vers = version;
73*45084Smckusick 		parms.pm_prot = protocol;
74*45084Smckusick 		parms.pm_port = 0;  /* not needed or used */
75*45084Smckusick 		if (CLNT_CALL(client, PMAPPROC_GETPORT, xdr_pmap, &parms,
76*45084Smckusick 		    xdr_u_short, &port, tottimeout) != RPC_SUCCESS){
77*45084Smckusick 			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
78*45084Smckusick 			clnt_geterr(client, &rpc_createerr.cf_error);
79*45084Smckusick 		} else if (port == 0) {
80*45084Smckusick 			rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
81*45084Smckusick 		}
82*45084Smckusick 		CLNT_DESTROY(client);
83*45084Smckusick 	}
84*45084Smckusick 	(void)close(socket);
85*45084Smckusick 	address->sin_port = 0;
86*45084Smckusick 	return (port);
87*45084Smckusick }
88