]> git.ipfire.org Git - thirdparty/glibc.git/blame - sunrpc/pmap_clnt.c
Fix dependency tracking on cross-rpcgen-objs
[thirdparty/glibc.git] / sunrpc / pmap_clnt.c
CommitLineData
28f540f4 1/*
a7ab6ec8 2 * Copyright (c) 2010, Oracle America, Inc.
cb636bb2 3 *
a7ab6ec8
UD
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
cb636bb2 7 *
a7ab6ec8
UD
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 * * Neither the name of the "Oracle America, Inc." nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
cbd3dceb 17 *
a7ab6ec8
UD
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
925ed47c 30 */
28f540f4
RM
31/*
32 * pmap_clnt.c
33 * Client interface to pmap rpc service.
28f540f4
RM
34 */
35
925ed47c
UD
36#include <stdio.h>
37#include <unistd.h>
4360eafd 38#include <libintl.h>
925ed47c 39#include <net/if.h>
412c954a 40#include <ifaddrs.h>
925ed47c
UD
41#include <sys/ioctl.h>
42#include <sys/socket.h>
43#include <netinet/in.h>
44#include <arpa/inet.h>
28f540f4
RM
45#include <rpc/rpc.h>
46#include <rpc/pmap_prot.h>
47#include <rpc/pmap_clnt.h>
48
925ed47c
UD
49/*
50 * Same as get_myaddress, but we try to use the loopback
51 * interface. portmap caches interfaces, and on DHCP clients,
52 * it could be that only loopback is started at this time.
53 */
ec28fc7c 54static bool_t
925ed47c
UD
55__get_myaddress (struct sockaddr_in *addr)
56{
412c954a 57 struct ifaddrs *ifa;
925ed47c 58
21ae57c4 59 if (getifaddrs (&ifa) != 0)
925ed47c 60 {
412c954a 61 perror ("get_myaddress: getifaddrs");
925ed47c
UD
62 exit (1);
63 }
64
412c954a
UD
65 int loopback = 1;
66 struct ifaddrs *run;
67
925ed47c 68 again:
412c954a
UD
69 run = ifa;
70 while (run != NULL)
925ed47c 71 {
f7e7a396
UD
72 if ((run->ifa_flags & IFF_UP)
73 && run->ifa_addr != NULL
74 && run->ifa_addr->sa_family == AF_INET
412c954a
UD
75 && ((run->ifa_flags & IFF_LOOPBACK) || loopback == 0))
76 {
77 *addr = *((struct sockaddr_in *) run->ifa_addr);
78 addr->sin_port = htons (PMAPPORT);
79 goto out;
80 }
81
82 run = run->ifa_next;
925ed47c 83 }
412c954a 84
925ed47c
UD
85 if (loopback == 1)
86 {
87 loopback = 0;
88 goto again;
89 }
412c954a
UD
90 out:
91 freeifaddrs (ifa);
92
93 return run == NULL ? FALSE : TRUE;
925ed47c
UD
94}
95
96
e7fd8a39
UD
97static const struct timeval timeout = {5, 0};
98static const struct timeval tottimeout = {60, 0};
28f540f4
RM
99
100/*
101 * Set a mapping between program,version and port.
102 * Calls the pmap service remotely to do the mapping.
103 */
104bool_t
e7fd8a39 105pmap_set (u_long program, u_long version, int protocol, u_short port)
28f540f4 106{
e7fd8a39
UD
107 struct sockaddr_in myaddress;
108 int socket = -1;
109 CLIENT *client;
110 struct pmap parms;
111 bool_t rslt;
28f540f4 112
ec28fc7c
UD
113 if (!__get_myaddress (&myaddress))
114 return FALSE;
7b57bfe5
UD
115 client = clntudp_bufcreate (&myaddress, PMAPPROG, PMAPVERS, timeout, &socket,
116 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
e7fd8a39
UD
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;
7b57bfe5
UD
123 if (CLNT_CALL (client, PMAPPROC_SET, (xdrproc_t)xdr_pmap,
124 (caddr_t)&parms, (xdrproc_t)xdr_bool, (caddr_t)&rslt,
e7fd8a39
UD
125 tottimeout) != RPC_SUCCESS)
126 {
127 clnt_perror (client, _("Cannot register service"));
5bd4d368 128 rslt = FALSE;
e7fd8a39
UD
129 }
130 CLNT_DESTROY (client);
131 /* (void)close(socket); CLNT_DESTROY closes it */
132 return rslt;
28f540f4 133}
021db4be 134libc_hidden_nolink_sunrpc (pmap_set, GLIBC_2_0)
28f540f4
RM
135
136/*
137 * Remove the mapping between program,version and port.
138 * Calls the pmap service remotely to do the un-mapping.
139 */
140bool_t
e7fd8a39 141pmap_unset (u_long program, u_long version)
28f540f4 142{
e7fd8a39
UD
143 struct sockaddr_in myaddress;
144 int socket = -1;
145 CLIENT *client;
146 struct pmap parms;
147 bool_t rslt;
28f540f4 148
ec28fc7c
UD
149 if (!__get_myaddress (&myaddress))
150 return FALSE;
7b57bfe5
UD
151 client = clntudp_bufcreate (&myaddress, PMAPPROG, PMAPVERS, timeout, &socket,
152 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
e7fd8a39
UD
153 if (client == (CLIENT *) NULL)
154 return FALSE;
155 parms.pm_prog = program;
156 parms.pm_vers = version;
157 parms.pm_port = parms.pm_prot = 0;
7b57bfe5
UD
158 CLNT_CALL (client, PMAPPROC_UNSET, (xdrproc_t)xdr_pmap,
159 (caddr_t)&parms, (xdrproc_t)xdr_bool, (caddr_t)&rslt,
77fe0b9c 160 tottimeout);
e7fd8a39
UD
161 CLNT_DESTROY (client);
162 /* (void)close(socket); CLNT_DESTROY already closed it */
163 return rslt;
28f540f4 164}
021db4be 165libc_hidden_nolink_sunrpc (pmap_unset, GLIBC_2_0)