xref: /freebsd/lib/libc/rpc/pmap_clnt.c (revision ad133ed648c16801aab7529587cf546c6328aa3d)
199064799SGarrett Wollman /*
299064799SGarrett Wollman  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
399064799SGarrett Wollman  * unrestricted use provided that this legend is included on all tape
499064799SGarrett Wollman  * media and as a part of the software program in whole or part.  Users
599064799SGarrett Wollman  * may copy or modify Sun RPC without charge, but are not authorized
699064799SGarrett Wollman  * to license or distribute it to anyone else except as part of a product or
799064799SGarrett Wollman  * program developed by the user.
899064799SGarrett Wollman  *
999064799SGarrett Wollman  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1099064799SGarrett Wollman  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1199064799SGarrett Wollman  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1299064799SGarrett Wollman  *
1399064799SGarrett Wollman  * Sun RPC is provided with no support and without any obligation on the
1499064799SGarrett Wollman  * part of Sun Microsystems, Inc. to assist in its use, correction,
1599064799SGarrett Wollman  * modification or enhancement.
1699064799SGarrett Wollman  *
1799064799SGarrett Wollman  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
1899064799SGarrett Wollman  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
1999064799SGarrett Wollman  * OR ANY PART THEREOF.
2099064799SGarrett Wollman  *
2199064799SGarrett Wollman  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2299064799SGarrett Wollman  * or profits or other special, indirect and consequential damages, even if
2399064799SGarrett Wollman  * Sun has been advised of the possibility of such damages.
2499064799SGarrett Wollman  *
2599064799SGarrett Wollman  * Sun Microsystems, Inc.
2699064799SGarrett Wollman  * 2550 Garcia Avenue
2799064799SGarrett Wollman  * Mountain View, California  94043
2899064799SGarrett Wollman  */
2999064799SGarrett Wollman 
3099064799SGarrett Wollman #if defined(LIBC_SCCS) && !defined(lint)
3199064799SGarrett Wollman /*static char *sccsid = "from: @(#)pmap_clnt.c 1.37 87/08/11 Copyr 1984 Sun Micro";*/
3299064799SGarrett Wollman /*static char *sccsid = "from: @(#)pmap_clnt.c	2.2 88/08/01 4.0 RPCSRC";*/
33ad133ed6SBill Paul static char *rcsid = "$Id: pmap_clnt.c,v 1.5 1996/12/30 14:46:33 peter Exp $";
3499064799SGarrett Wollman #endif
3599064799SGarrett Wollman 
3699064799SGarrett Wollman /*
3799064799SGarrett Wollman  * pmap_clnt.c
3899064799SGarrett Wollman  * Client interface to pmap rpc service.
3999064799SGarrett Wollman  *
4099064799SGarrett Wollman  * Copyright (C) 1984, Sun Microsystems, Inc.
4199064799SGarrett Wollman  */
4299064799SGarrett Wollman 
43ad133ed6SBill Paul #include <sys/types.h>
44ad133ed6SBill Paul #include <sys/stat.h>
454c3af266SPoul-Henning Kamp #include <unistd.h>
4699064799SGarrett Wollman #include <rpc/rpc.h>
4799064799SGarrett Wollman #include <rpc/pmap_prot.h>
4899064799SGarrett Wollman #include <rpc/pmap_clnt.h>
49ec53c6faSPeter Wemm #include <netinet/in.h>
50c124f3bdSJames Raynard 
5199064799SGarrett Wollman static struct timeval timeout = { 5, 0 };
5299064799SGarrett Wollman static struct timeval tottimeout = { 60, 0 };
5399064799SGarrett Wollman 
5499064799SGarrett Wollman void clnt_perror();
5599064799SGarrett Wollman 
56ad133ed6SBill Paul #ifndef PORTMAPSOCK
57ad133ed6SBill Paul #define PORTMAPSOCK "/var/run/portmapsock"
58ad133ed6SBill Paul #endif
5999064799SGarrett Wollman 
6099064799SGarrett Wollman /*
6199064799SGarrett Wollman  * Set a mapping between program,version and port.
6299064799SGarrett Wollman  * Calls the pmap service remotely to do the mapping.
6399064799SGarrett Wollman  */
6499064799SGarrett Wollman bool_t
6599064799SGarrett Wollman pmap_set(program, version, protocol, port)
6699064799SGarrett Wollman 	u_long program;
6799064799SGarrett Wollman 	u_long version;
6899064799SGarrett Wollman 	int protocol;
6999064799SGarrett Wollman 	u_short port;
7099064799SGarrett Wollman {
7199064799SGarrett Wollman 	struct sockaddr_in myaddress;
7299064799SGarrett Wollman 	int socket = -1;
7399064799SGarrett Wollman 	register CLIENT *client;
7499064799SGarrett Wollman 	struct pmap parms;
7599064799SGarrett Wollman 	bool_t rslt;
76ad133ed6SBill Paul 	struct stat st;
7799064799SGarrett Wollman 
78ad133ed6SBill Paul 	/*
79ad133ed6SBill Paul 	 * Temporary hack for backwards compatibility. Eventually
80ad133ed6SBill Paul 	 * this test will go away and we'll use only the "unix" transport.
81ad133ed6SBill Paul 	 */
82ad133ed6SBill Paul 	if (stat(PORTMAPSOCK, &st) == 0 && st.st_mode & S_IFSOCK)
83ad133ed6SBill Paul 		client = clnt_create(PORTMAPSOCK, PMAPPROG, PMAPVERS, "unix");
84ad133ed6SBill Paul 	else  {
85ec53c6faSPeter Wemm 		if (get_myaddress(&myaddress) != 0)
86ec53c6faSPeter Wemm 			return (FALSE);
87ec53c6faSPeter Wemm 		myaddress.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
8899064799SGarrett Wollman 		client = clntudp_bufcreate(&myaddress, PMAPPROG, PMAPVERS,
8999064799SGarrett Wollman 	    		timeout, &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
90ad133ed6SBill Paul 	}
91ad133ed6SBill Paul 
9299064799SGarrett Wollman 	if (client == (CLIENT *)NULL)
9399064799SGarrett Wollman 		return (FALSE);
9499064799SGarrett Wollman 	parms.pm_prog = program;
9599064799SGarrett Wollman 	parms.pm_vers = version;
9699064799SGarrett Wollman 	parms.pm_prot = protocol;
9799064799SGarrett Wollman 	parms.pm_port = port;
9899064799SGarrett Wollman 	if (CLNT_CALL(client, PMAPPROC_SET, xdr_pmap, &parms, xdr_bool, &rslt,
9999064799SGarrett Wollman 	    tottimeout) != RPC_SUCCESS) {
10099064799SGarrett Wollman 		clnt_perror(client, "Cannot register service");
10199064799SGarrett Wollman 		return (FALSE);
10299064799SGarrett Wollman 	}
10399064799SGarrett Wollman 	CLNT_DESTROY(client);
104ec53c6faSPeter Wemm 	if (socket != -1)
10599064799SGarrett Wollman 		(void)close(socket);
10699064799SGarrett Wollman 	return (rslt);
10799064799SGarrett Wollman }
10899064799SGarrett Wollman 
10999064799SGarrett Wollman /*
11099064799SGarrett Wollman  * Remove the mapping between program,version and port.
11199064799SGarrett Wollman  * Calls the pmap service remotely to do the un-mapping.
11299064799SGarrett Wollman  */
11399064799SGarrett Wollman bool_t
11499064799SGarrett Wollman pmap_unset(program, version)
11599064799SGarrett Wollman 	u_long program;
11699064799SGarrett Wollman 	u_long version;
11799064799SGarrett Wollman {
11899064799SGarrett Wollman 	struct sockaddr_in myaddress;
11999064799SGarrett Wollman 	int socket = -1;
12099064799SGarrett Wollman 	register CLIENT *client;
12199064799SGarrett Wollman 	struct pmap parms;
12299064799SGarrett Wollman 	bool_t rslt;
123ad133ed6SBill Paul 	struct stat st;
12499064799SGarrett Wollman 
125ad133ed6SBill Paul 	/*
126ad133ed6SBill Paul 	 * Temporary hack for backwards compatibility. Eventually
127ad133ed6SBill Paul 	 * this test will go away and we'll use only the "unix" transport.
128ad133ed6SBill Paul 	 */
129ad133ed6SBill Paul 	if (stat(PORTMAPSOCK, &st) == 0 && st.st_mode & S_IFSOCK)
130ad133ed6SBill Paul 		client = clnt_create(PORTMAPSOCK, PMAPPROG, PMAPVERS, "unix");
131ad133ed6SBill Paul 	else {
132ec53c6faSPeter Wemm 		if (get_myaddress(&myaddress) != 0)
133ec53c6faSPeter Wemm 			return (FALSE);
134ec53c6faSPeter Wemm 		myaddress.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
13599064799SGarrett Wollman 		client = clntudp_bufcreate(&myaddress, PMAPPROG, PMAPVERS,
13699064799SGarrett Wollman 	    		timeout, &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
137ad133ed6SBill Paul 	}
13899064799SGarrett Wollman 	if (client == (CLIENT *)NULL)
13999064799SGarrett Wollman 		return (FALSE);
14099064799SGarrett Wollman 	parms.pm_prog = program;
14199064799SGarrett Wollman 	parms.pm_vers = version;
14299064799SGarrett Wollman 	parms.pm_port = parms.pm_prot = 0;
14399064799SGarrett Wollman 	CLNT_CALL(client, PMAPPROC_UNSET, xdr_pmap, &parms, xdr_bool, &rslt,
14499064799SGarrett Wollman 	    tottimeout);
14599064799SGarrett Wollman 	CLNT_DESTROY(client);
146ec53c6faSPeter Wemm 	if (socket != -1)
14799064799SGarrett Wollman 		(void)close(socket);
14899064799SGarrett Wollman 	return (rslt);
14999064799SGarrett Wollman }
150