fix musl compatibility
[project/librpc-uclibc.git] / 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 #define __FORCE_GLIBC
38 #include <features.h>
39
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <net/if.h>
43 #include <sys/ioctl.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <rpc/rpc.h>
48 #include <rpc/pmap_prot.h>
49 #include <rpc/pmap_clnt.h>
50
51
52 /*
53 * Same as get_myaddress, but we try to use the loopback
54 * interface. portmap caches interfaces, and on DHCP clients,
55 * it could be that only loopback is started at this time.
56 */
57 static bool_t
58 __get_myaddress (struct sockaddr_in *addr)
59 {
60 int s;
61 char buf[BUFSIZ];
62 struct ifconf ifc;
63 struct ifreq ifreq, *ifr;
64 int len, loopback = 1;
65
66 if ((s = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
67 {
68 perror ("__get_myaddress: socket");
69 exit (1);
70 }
71 ifc.ifc_len = sizeof (buf);
72 ifc.ifc_buf = buf;
73 if (ioctl (s, SIOCGIFCONF, (char *) &ifc) < 0)
74 {
75 perror (_("__get_myaddress: ioctl (get interface configuration)"));
76 exit (1);
77 }
78
79 again:
80 ifr = ifc.ifc_req;
81 for (len = ifc.ifc_len; len; len -= sizeof ifreq)
82 {
83 ifreq = *ifr;
84 if (ioctl (s, SIOCGIFFLAGS, (char *) &ifreq) < 0)
85 {
86 perror ("__get_myaddress: ioctl");
87 exit (1);
88 }
89 if ((ifreq.ifr_flags & IFF_UP) && (ifr->ifr_addr.sa_family == AF_INET)
90 && ((ifreq.ifr_flags & IFF_LOOPBACK) || (loopback == 0)))
91 {
92 *addr = *((struct sockaddr_in *) &ifr->ifr_addr);
93 addr->sin_port = htons (PMAPPORT);
94 close (s);
95 return TRUE;
96 }
97 ifr++;
98 }
99 if (loopback == 1)
100 {
101 loopback = 0;
102 goto again;
103 }
104 close (s);
105 return FALSE;
106 }
107
108
109 static const struct timeval timeout = {5, 0};
110 static const struct timeval tottimeout = {60, 0};
111
112 /*
113 * Set a mapping between program,version and port.
114 * Calls the pmap service remotely to do the mapping.
115 */
116 bool_t
117 pmap_set (u_long program, u_long version, int protocol, u_short port)
118 {
119 struct sockaddr_in myaddress;
120 int _socket = -1;
121 CLIENT *client;
122 struct pmap parms;
123 bool_t rslt;
124
125 if (!__get_myaddress (&myaddress))
126 return FALSE;
127 client = clntudp_bufcreate (&myaddress, PMAPPROG, PMAPVERS,
128 timeout, &_socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
129 if (client == (CLIENT *) NULL)
130 return (FALSE);
131 parms.pm_prog = program;
132 parms.pm_vers = version;
133 parms.pm_prot = protocol;
134 parms.pm_port = port;
135 if (CLNT_CALL (client, PMAPPROC_SET, (xdrproc_t)xdr_pmap, (caddr_t)&parms,
136 (xdrproc_t)xdr_bool, (caddr_t)&rslt,
137 tottimeout) != RPC_SUCCESS)
138 {
139 clnt_perror (client, _("Cannot register service"));
140 rslt = FALSE;
141 }
142 CLNT_DESTROY (client);
143 /* (void)close(_socket); CLNT_DESTROY closes it */
144 return rslt;
145 }
146 libc_hidden_def (pmap_set)
147
148 /*
149 * Remove the mapping between program,version and port.
150 * Calls the pmap service remotely to do the un-mapping.
151 */
152 bool_t
153 pmap_unset (u_long program, u_long version)
154 {
155 struct sockaddr_in myaddress;
156 int _socket = -1;
157 CLIENT *client;
158 struct pmap parms;
159 bool_t rslt;
160
161 if (!__get_myaddress (&myaddress))
162 return FALSE;
163 client = clntudp_bufcreate (&myaddress, PMAPPROG, PMAPVERS,
164 timeout, &_socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
165 if (client == (CLIENT *) NULL)
166 return FALSE;
167 parms.pm_prog = program;
168 parms.pm_vers = version;
169 parms.pm_port = parms.pm_prot = 0;
170 CLNT_CALL (client, PMAPPROC_UNSET, (xdrproc_t)xdr_pmap, (caddr_t)&parms,
171 (xdrproc_t)xdr_bool, (caddr_t)&rslt, tottimeout);
172 CLNT_DESTROY (client);
173 /* (void)close(_socket); CLNT_DESTROY already closed it */
174 return rslt;
175 }
176 libc_hidden_def (pmap_unset)