pmap_getport.c revision 1.1
1193326Sed/*	$NetBSD: pmap_getport.c,v 1.2 1995/02/25 03:01:49 cgd Exp $	*/
2193326Sed
3193326Sed/*
4193326Sed * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5193326Sed * unrestricted use provided that this legend is included on all tape
6193326Sed * media and as a part of the software program in whole or part.  Users
7193326Sed * may copy or modify Sun RPC without charge, but are not authorized
8193326Sed * to license or distribute it to anyone else except as part of a product or
9193326Sed * program developed by the user.
10226633Sdim *
11193326Sed * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12193326Sed * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13193326Sed * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14212904Sdim *
15193326Sed * Sun RPC is provided with no support and without any obligation on the
16212904Sdim * part of Sun Microsystems, Inc. to assist in its use, correction,
17193326Sed * modification or enhancement.
18193326Sed *
19201361Srdivacky * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20249423Sdim * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21249423Sdim * OR ANY PART THEREOF.
22249423Sdim *
23249423Sdim * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24234353Sdim * or profits or other special, indirect and consequential damages, even if
25234353Sdim * Sun has been advised of the possibility of such damages.
26200583Srdivacky *
27226633Sdim * Sun Microsystems, Inc.
28193326Sed * 2550 Garcia Avenue
29193326Sed * Mountain View, California  94043
30193326Sed */
31193326Sed
32193326Sed#if defined(LIBC_SCCS) && !defined(lint)
33193326Sed/*static char *sccsid = "from: @(#)pmap_getport.c 1.9 87/08/11 Copyr 1984 Sun Micro";*/
34193326Sed/*static char *sccsid = "from: @(#)pmap_getport.c	2.2 88/08/01 4.0 RPCSRC";*/
35263508Sdimstatic char *rcsid = "$NetBSD: pmap_getport.c,v 1.2 1995/02/25 03:01:49 cgd Exp $";
36263508Sdim#endif
37263508Sdim
38263508Sdim/*
39263508Sdim * pmap_getport.c
40263508Sdim * Client interface to pmap rpc service.
41263508Sdim *
42263508Sdim * Copyright (C) 1984, Sun Microsystems, Inc.
43263508Sdim */
44263508Sdim
45263508Sdim#include <rpc/rpc.h>
46263508Sdim#include <rpc/pmap_prot.h>
47263508Sdim#include <rpc/pmap_clnt.h>
48263508Sdim#include <sys/socket.h>
49263508Sdim#include <net/if.h>
50263508Sdim
51263508Sdimstatic struct timeval timeout = { 5, 0 };
52263508Sdimstatic struct timeval tottimeout = { 60, 0 };
53263508Sdim
54263508Sdim/*
55263508Sdim * Find the mapped port for program,version.
56263508Sdim * Calls the pmap service remotely to do the lookup.
57263508Sdim * Returns 0 if no map exists.
58263508Sdim */
59263508Sdimu_short
60263508Sdimpmap_getport(address, program, version, protocol)
61193326Sed	struct sockaddr_in *address;
62263508Sdim	u_long program;
63193326Sed	u_long version;
64193326Sed	u_int protocol;
65193326Sed{
66198092Srdivacky	u_short port = 0;
67193326Sed	int socket = -1;
68193326Sed	register CLIENT *client;
69263508Sdim	struct pmap parms;
70193326Sed
71193326Sed	address->sin_port = htons(PMAPPORT);
72193326Sed	client = clntudp_bufcreate(address, PMAPPROG,
73263508Sdim	    PMAPVERS, timeout, &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
74263508Sdim	if (client != (CLIENT *)NULL) {
75193326Sed		parms.pm_prog = program;
76263508Sdim		parms.pm_vers = version;
77263508Sdim		parms.pm_prot = protocol;
78226633Sdim		parms.pm_port = 0;  /* not needed or used */
79226633Sdim		if (CLNT_CALL(client, PMAPPROC_GETPORT, xdr_pmap, &parms,
80226633Sdim		    xdr_u_short, &port, tottimeout) != RPC_SUCCESS){
81226633Sdim			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
82226633Sdim			clnt_geterr(client, &rpc_createerr.cf_error);
83226633Sdim		} else if (port == 0) {
84263508Sdim			rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
85263508Sdim		}
86263508Sdim		CLNT_DESTROY(client);
87263508Sdim	}
88263508Sdim	(void)close(socket);
89263508Sdim	address->sin_port = 0;
90263508Sdim	return (port);
91263508Sdim}
92263508Sdim