2 * Copyright (c) 2010, Oracle America, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
33 * Client interface to pmap rpc service.
41 #include <sys/ioctl.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
46 #include <rpc/pmap_prot.h>
47 #include <rpc/pmap_clnt.h>
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.
55 __get_myaddress (struct sockaddr_in
*addr
)
59 if (getifaddrs (&ifa
) != 0)
61 perror ("get_myaddress: getifaddrs");
72 if ((run
->ifa_flags
& IFF_UP
)
73 && run
->ifa_addr
!= NULL
74 && run
->ifa_addr
->sa_family
== AF_INET
75 && ((run
->ifa_flags
& IFF_LOOPBACK
) || loopback
== 0))
77 *addr
= *((struct sockaddr_in
*) run
->ifa_addr
);
78 addr
->sin_port
= htons (PMAPPORT
);
93 return run
== NULL
? FALSE
: TRUE
;
97 static const struct timeval timeout
= {5, 0};
98 static const struct timeval tottimeout
= {60, 0};
101 * Set a mapping between program,version and port.
102 * Calls the pmap service remotely to do the mapping.
105 pmap_set (u_long program
, u_long version
, int protocol
, u_short port
)
107 struct sockaddr_in myaddress
;
113 if (!__get_myaddress (&myaddress
))
115 client
= clntudp_bufcreate (&myaddress
, PMAPPROG
, PMAPVERS
, timeout
, &socket
,
116 RPCSMALLMSGSIZE
, RPCSMALLMSGSIZE
);
117 if (client
== (CLIENT
*) NULL
)
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
)xdr_pmap
,
124 (caddr_t
)&parms
, (xdrproc_t
)xdr_bool
, (caddr_t
)&rslt
,
125 tottimeout
) != RPC_SUCCESS
)
127 clnt_perror (client
, _("Cannot register service"));
130 CLNT_DESTROY (client
);
131 /* (void)close(socket); CLNT_DESTROY closes it */
134 libc_hidden_nolink (pmap_set
, GLIBC_2_0
)
137 * Remove the mapping between program,version and port.
138 * Calls the pmap service remotely to do the un-mapping.
141 pmap_unset (u_long program
, u_long version
)
143 struct sockaddr_in myaddress
;
149 if (!__get_myaddress (&myaddress
))
151 client
= clntudp_bufcreate (&myaddress
, PMAPPROG
, PMAPVERS
, timeout
, &socket
,
152 RPCSMALLMSGSIZE
, RPCSMALLMSGSIZE
);
153 if (client
== (CLIENT
*) NULL
)
155 parms
.pm_prog
= program
;
156 parms
.pm_vers
= version
;
157 parms
.pm_port
= parms
.pm_prot
= 0;
158 CLNT_CALL (client
, PMAPPROC_UNSET
, (xdrproc_t
)xdr_pmap
,
159 (caddr_t
)&parms
, (xdrproc_t
)xdr_bool
, (caddr_t
)&rslt
,
161 CLNT_DESTROY (client
);
162 /* (void)close(socket); CLNT_DESTROY already closed it */
165 libc_hidden_nolink (pmap_unset
, GLIBC_2_0
)