remove COPYING* from .gitignore files
[platform/upstream/glibc.git] / sunrpc / 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 /*
30  * Copyright (C) 1984, Sun Microsystems, Inc.
31  */
32 /*
33  * pmap_clnt.c
34  * Client interface to pmap rpc service.
35  */
36
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>
49
50 /*
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.
54  */
55 static bool_t
56 __get_myaddress (struct sockaddr_in *addr)
57 {
58   struct ifaddrs *ifa;
59
60   if (getifaddrs (&ifa) != 0)
61     {
62       perror ("get_myaddress: getifaddrs");
63       exit (1);
64     }
65
66   int loopback = 1;
67   struct ifaddrs *run;
68
69  again:
70   run = ifa;
71   while (run != NULL)
72     {
73       if ((run->ifa_flags & IFF_UP)
74           && run->ifa_addr != NULL
75           && run->ifa_addr->sa_family == AF_INET
76           && ((run->ifa_flags & IFF_LOOPBACK) || loopback == 0))
77         {
78           *addr = *((struct sockaddr_in *) run->ifa_addr);
79           addr->sin_port = htons (PMAPPORT);
80           goto out;
81         }
82
83       run = run->ifa_next;
84     }
85
86   if (loopback == 1)
87     {
88       loopback = 0;
89       goto again;
90     }
91  out:
92   freeifaddrs (ifa);
93
94   return run == NULL ? FALSE : TRUE;
95 }
96
97
98 static const struct timeval timeout = {5, 0};
99 static const struct timeval tottimeout = {60, 0};
100
101 /*
102  * Set a mapping between program,version and port.
103  * Calls the pmap service remotely to do the mapping.
104  */
105 bool_t
106 pmap_set (u_long program, u_long version, int protocol, u_short port)
107 {
108   struct sockaddr_in myaddress;
109   int socket = -1;
110   CLIENT *client;
111   struct pmap parms;
112   bool_t rslt;
113
114   if (!__get_myaddress (&myaddress))
115     return FALSE;
116   client = INTUSE(clntudp_bufcreate) (&myaddress, PMAPPROG, PMAPVERS,
117                                       timeout, &socket, RPCSMALLMSGSIZE,
118                                       RPCSMALLMSGSIZE);
119   if (client == (CLIENT *) NULL)
120     return (FALSE);
121   parms.pm_prog = program;
122   parms.pm_vers = version;
123   parms.pm_prot = protocol;
124   parms.pm_port = port;
125   if (CLNT_CALL (client, PMAPPROC_SET, (xdrproc_t)INTUSE(xdr_pmap),
126                  (caddr_t)&parms, (xdrproc_t)INTUSE(xdr_bool), (caddr_t)&rslt,
127                  tottimeout) != RPC_SUCCESS)
128     {
129       clnt_perror (client, _("Cannot register service"));
130       rslt = FALSE;
131     }
132   CLNT_DESTROY (client);
133   /* (void)close(socket); CLNT_DESTROY closes it */
134   return rslt;
135 }
136 libc_hidden_def (pmap_set)
137
138 /*
139  * Remove the mapping between program,version and port.
140  * Calls the pmap service remotely to do the un-mapping.
141  */
142 bool_t
143 pmap_unset (u_long program, u_long version)
144 {
145   struct sockaddr_in myaddress;
146   int socket = -1;
147   CLIENT *client;
148   struct pmap parms;
149   bool_t rslt;
150
151   if (!__get_myaddress (&myaddress))
152     return FALSE;
153   client = INTUSE(clntudp_bufcreate) (&myaddress, PMAPPROG, PMAPVERS,
154                                       timeout, &socket, RPCSMALLMSGSIZE,
155                                       RPCSMALLMSGSIZE);
156   if (client == (CLIENT *) NULL)
157     return FALSE;
158   parms.pm_prog = program;
159   parms.pm_vers = version;
160   parms.pm_port = parms.pm_prot = 0;
161   CLNT_CALL (client, PMAPPROC_UNSET, (xdrproc_t)INTUSE(xdr_pmap),
162              (caddr_t)&parms, (xdrproc_t)INTUSE(xdr_bool), (caddr_t)&rslt,
163              tottimeout);
164   CLNT_DESTROY (client);
165   /* (void)close(socket); CLNT_DESTROY already closed it */
166   return rslt;
167 }
168 libc_hidden_def (pmap_unset)