Update.
[glibc.git] / sunrpc / pmap_clnt.c
blobaa5740c8a0b09f8af840b6a08e50048b43dcd843
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.
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.
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.
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.
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.
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
30 * Copyright (C) 1984, Sun Microsystems, Inc.
33 * pmap_clnt.c
34 * Client interface to pmap rpc service.
37 #include <stdio.h>
38 #include <unistd.h>
39 #include <libintl.h>
40 #include <net/if.h>
41 #include <ifaddrs.h>
42 #include <sys/ioctl.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 #include <rpc/rpc.h>
47 #include <rpc/pmap_prot.h>
48 #include <rpc/pmap_clnt.h>
51 * Same as get_myaddress, but we try to use the loopback
52 * interface. portmap caches interfaces, and on DHCP clients,
53 * it could be that only loopback is started at this time.
55 static bool_t
56 __get_myaddress (struct sockaddr_in *addr)
58 struct ifaddrs *ifa;
60 if (getifaddrs (&ifa) != 0)
62 perror ("get_myaddress: getifaddrs");
63 exit (1);
66 int loopback = 1;
67 struct ifaddrs *run;
69 again:
70 run = ifa;
71 while (run != NULL)
73 if ((run->ifa_flags & IFF_UP) && run->ifa_addr->sa_family == AF_INET
74 && ((run->ifa_flags & IFF_LOOPBACK) || loopback == 0))
76 *addr = *((struct sockaddr_in *) run->ifa_addr);
77 addr->sin_port = htons (PMAPPORT);
78 goto out;
81 run = run->ifa_next;
84 if (loopback == 1)
86 loopback = 0;
87 goto again;
89 out:
90 freeifaddrs (ifa);
92 return run == NULL ? FALSE : TRUE;
96 static const struct timeval timeout = {5, 0};
97 static const struct timeval tottimeout = {60, 0};
100 * Set a mapping between program,version and port.
101 * Calls the pmap service remotely to do the mapping.
103 bool_t
104 pmap_set (u_long program, u_long version, int protocol, u_short port)
106 struct sockaddr_in myaddress;
107 int socket = -1;
108 CLIENT *client;
109 struct pmap parms;
110 bool_t rslt;
112 if (!__get_myaddress (&myaddress))
113 return FALSE;
114 client = INTUSE(clntudp_bufcreate) (&myaddress, PMAPPROG, PMAPVERS,
115 timeout, &socket, RPCSMALLMSGSIZE,
116 RPCSMALLMSGSIZE);
117 if (client == (CLIENT *) NULL)
118 return (FALSE);
119 parms.pm_prog = program;
120 parms.pm_vers = version;
121 parms.pm_prot = protocol;
122 parms.pm_port = port;
123 if (CLNT_CALL (client, PMAPPROC_SET, (xdrproc_t)INTUSE(xdr_pmap),
124 (caddr_t)&parms, (xdrproc_t)INTUSE(xdr_bool), (caddr_t)&rslt,
125 tottimeout) != RPC_SUCCESS)
127 clnt_perror (client, _("Cannot register service"));
128 rslt = FALSE;
130 CLNT_DESTROY (client);
131 /* (void)close(socket); CLNT_DESTROY closes it */
132 return rslt;
134 libc_hidden_def (pmap_set)
137 * Remove the mapping between program,version and port.
138 * Calls the pmap service remotely to do the un-mapping.
140 bool_t
141 pmap_unset (u_long program, u_long version)
143 struct sockaddr_in myaddress;
144 int socket = -1;
145 CLIENT *client;
146 struct pmap parms;
147 bool_t rslt;
149 if (!__get_myaddress (&myaddress))
150 return FALSE;
151 client = INTUSE(clntudp_bufcreate) (&myaddress, PMAPPROG, PMAPVERS,
152 timeout, &socket, RPCSMALLMSGSIZE,
153 RPCSMALLMSGSIZE);
154 if (client == (CLIENT *) NULL)
155 return FALSE;
156 parms.pm_prog = program;
157 parms.pm_vers = version;
158 parms.pm_port = parms.pm_prot = 0;
159 CLNT_CALL (client, PMAPPROC_UNSET, (xdrproc_t)INTUSE(xdr_pmap),
160 (caddr_t)&parms, (xdrproc_t)INTUSE(xdr_bool), (caddr_t)&rslt,
161 tottimeout);
162 CLNT_DESTROY (client);
163 /* (void)close(socket); CLNT_DESTROY already closed it */
164 return rslt;
166 libc_hidden_def (pmap_unset)