Merge from vendor branch GDB:
[dragonfly.git] / lib / libc / rpc / pmap_clnt.c
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  *
29  * @(#)pmap_clnt.c 1.37 87/08/11 Copyr 1984 Sun Micro
30  * @(#)pmap_clnt.c      2.2 88/08/01 4.0 RPCSRC
31  * $FreeBSD: src/lib/libc/rpc/pmap_clnt.c,v 1.11 2000/01/27 23:06:39 jasone Exp $
32  * $DragonFly: src/lib/libc/rpc/pmap_clnt.c,v 1.4 2005/01/31 22:29:38 dillon Exp $
33  */
34
35 /*
36  * pmap_clnt.c
37  * Client interface to pmap rpc service.
38  *
39  * Copyright (C) 1984, Sun Microsystems, Inc.
40  */
41
42 #include "namespace.h"
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <unistd.h>
46 #include <rpc/rpc.h>
47 #include <rpc/pmap_prot.h>
48 #include <rpc/pmap_clnt.h>
49 #include <netinet/in.h>
50 #include "un-namespace.h"
51
52 static struct timeval timeout = { 5, 0 };
53 static struct timeval tottimeout = { 60, 0 };
54
55 void clnt_perror();
56
57 #ifndef PORTMAPSOCK
58 #define PORTMAPSOCK "/var/run/portmapsock"
59 #endif
60
61 /*
62  * Set a mapping between program,version and port.
63  * Calls the pmap service remotely to do the mapping.
64  */
65 bool_t
66 pmap_set(program, version, protocol, port)
67         u_long program;
68         u_long version;
69         int protocol;
70         u_short port;
71 {
72         struct sockaddr_in myaddress;
73         int socket = -1;
74         CLIENT *client;
75         struct pmap parms;
76         bool_t rslt;
77         struct stat st;
78
79         /*
80          * Temporary hack for backwards compatibility. Eventually
81          * this test will go away and we'll use only the "unix" transport.
82          */
83         if (stat(PORTMAPSOCK, &st) == 0 && st.st_mode & S_IFSOCK)
84                 client = clnt_create(PORTMAPSOCK, PMAPPROG, PMAPVERS, "unix");
85         else  {
86                 if (get_myaddress(&myaddress) != 0)
87                         return (FALSE);
88                 myaddress.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
89                 client = clntudp_bufcreate(&myaddress, PMAPPROG, PMAPVERS,
90                         timeout, &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
91         }
92
93         if (client == (CLIENT *)NULL)
94                 return (FALSE);
95         parms.pm_prog = program;
96         parms.pm_vers = version;
97         parms.pm_prot = protocol;
98         parms.pm_port = port;
99         if (CLNT_CALL(client, PMAPPROC_SET, xdr_pmap, &parms, xdr_bool, &rslt,
100             tottimeout) != RPC_SUCCESS) {
101                 clnt_perror(client, "Cannot register service");
102                 return (FALSE);
103         }
104         CLNT_DESTROY(client);
105         if (socket != -1)
106                 (void)_close(socket);
107         return (rslt);
108 }
109
110 /*
111  * Remove the mapping between program,version and port.
112  * Calls the pmap service remotely to do the un-mapping.
113  */
114 bool_t
115 pmap_unset(program, version)
116         u_long program;
117         u_long version;
118 {
119         struct sockaddr_in myaddress;
120         int socket = -1;
121         CLIENT *client;
122         struct pmap parms;
123         bool_t rslt;
124         struct stat st;
125
126         /*
127          * Temporary hack for backwards compatibility. Eventually
128          * this test will go away and we'll use only the "unix" transport.
129          */
130         if (stat(PORTMAPSOCK, &st) == 0 && st.st_mode & S_IFSOCK)
131                 client = clnt_create(PORTMAPSOCK, PMAPPROG, PMAPVERS, "unix");
132         else {
133                 if (get_myaddress(&myaddress) != 0)
134                         return (FALSE);
135                 myaddress.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
136                 client = clntudp_bufcreate(&myaddress, PMAPPROG, PMAPVERS,
137                         timeout, &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
138         }
139         if (client == (CLIENT *)NULL)
140                 return (FALSE);
141         parms.pm_prog = program;
142         parms.pm_vers = version;
143         parms.pm_port = parms.pm_prot = 0;
144         CLNT_CALL(client, PMAPPROC_UNSET, xdr_pmap, &parms, xdr_bool, &rslt,
145             tottimeout);
146         CLNT_DESTROY(client);
147         if (socket != -1)
148                 (void)_close(socket);
149         return (rslt);
150 }