Update.
[glibc.git] / sunrpc / pmap_clnt.c
blob11e241dfcb09013a304be3878a8151a0c09e280e
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 <net/if.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <rpc/rpc.h>
45 #include <rpc/pmap_prot.h>
46 #include <rpc/pmap_clnt.h>
49 * Same as get_myaddress, but we try to use the loopback
50 * interface. portmap caches interfaces, and on DHCP clients,
51 * it could be that only loopback is started at this time.
53 static void
54 __get_myaddress (struct sockaddr_in *addr)
56 int s;
57 char buf[BUFSIZ];
58 struct ifconf ifc;
59 struct ifreq ifreq, *ifr;
60 int len, loopback = 1;
62 if ((s = __socket (AF_INET, SOCK_DGRAM, 0)) < 0)
64 perror ("__get_myaddress: socket");
65 exit (1);
67 ifc.ifc_len = sizeof (buf);
68 ifc.ifc_buf = buf;
69 if (__ioctl (s, SIOCGIFCONF, (char *) &ifc) < 0)
71 perror (_("__get_myaddress: ioctl (get interface configuration)"));
72 exit (1);
75 again:
76 ifr = ifc.ifc_req;
77 for (len = ifc.ifc_len; len; len -= sizeof ifreq)
79 ifreq = *ifr;
80 if (__ioctl (s, SIOCGIFFLAGS, (char *) &ifreq) < 0)
82 perror ("__get_myaddress: ioctl");
83 exit (1);
85 if ((ifreq.ifr_flags & IFF_UP) && (ifr->ifr_addr.sa_family == AF_INET)
86 && ((ifreq.ifr_flags & IFF_LOOPBACK) || (loopback == 0)))
88 *addr = *((struct sockaddr_in *) &ifr->ifr_addr);
89 addr->sin_port = htons (PMAPPORT);
90 __close (s);
91 return;
93 ifr++;
95 if (loopback == 1)
97 loopback = 0;
98 goto again;
100 __close (s);
104 static const struct timeval timeout = {5, 0};
105 static const struct timeval tottimeout = {60, 0};
108 * Set a mapping between program,version and port.
109 * Calls the pmap service remotely to do the mapping.
111 bool_t
112 pmap_set (u_long program, u_long version, int protocol, u_short port)
114 struct sockaddr_in myaddress;
115 int socket = -1;
116 CLIENT *client;
117 struct pmap parms;
118 bool_t rslt;
120 __get_myaddress (&myaddress);
121 client = clntudp_bufcreate (&myaddress, PMAPPROG, PMAPVERS,
122 timeout, &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
123 if (client == (CLIENT *) NULL)
124 return (FALSE);
125 parms.pm_prog = program;
126 parms.pm_vers = version;
127 parms.pm_prot = protocol;
128 parms.pm_port = port;
129 if (CLNT_CALL (client, PMAPPROC_SET, (xdrproc_t)xdr_pmap, (caddr_t)&parms,
130 (xdrproc_t)xdr_bool, (caddr_t)&rslt,
131 tottimeout) != RPC_SUCCESS)
133 clnt_perror (client, _("Cannot register service"));
134 return FALSE;
136 CLNT_DESTROY (client);
137 /* (void)close(socket); CLNT_DESTROY closes it */
138 return rslt;
142 * Remove the mapping between program,version and port.
143 * Calls the pmap service remotely to do the un-mapping.
145 bool_t
146 pmap_unset (u_long program, u_long version)
148 struct sockaddr_in myaddress;
149 int socket = -1;
150 CLIENT *client;
151 struct pmap parms;
152 bool_t rslt;
154 __get_myaddress (&myaddress);
155 client = clntudp_bufcreate (&myaddress, PMAPPROG, PMAPVERS,
156 timeout, &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
157 if (client == (CLIENT *) NULL)
158 return FALSE;
159 parms.pm_prog = program;
160 parms.pm_vers = version;
161 parms.pm_port = parms.pm_prot = 0;
162 CLNT_CALL (client, PMAPPROC_UNSET, (xdrproc_t)xdr_pmap, (caddr_t)&parms,
163 (xdrproc_t)xdr_bool, (caddr_t)&rslt, tottimeout);
164 CLNT_DESTROY (client);
165 /* (void)close(socket); CLNT_DESTROY already closed it */
166 return rslt;