1 /* $NetBSD: check_bound.c,v 1.2 2000/06/22 08:09:26 fvdl Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2009, Sun Microsystems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
36 /* #ident "@(#)check_bound.c 1.15 93/07/05 SMI" */
40 static char sccsid[] = "@(#)check_bound.c 1.11 89/04/21 Copyr 1989 Sun Micro";
46 * Checks to see whether the program is still bound to the
47 * claimed address and returns the universal merged address
51 #include <sys/types.h>
52 #include <sys/socket.h>
54 #include <rpc/svc_dg.h>
55 #include <netconfig.h>
65 struct netconfig *nconf;
70 static struct fdlist *fdhead; /* Link list of the check fd's */
71 static struct fdlist *fdtail;
72 static char *nullstring = "";
74 static bool_t check_bound(struct fdlist *, char *uaddr);
77 * Returns 1 if the given address is bound for the given addr & transport
78 * For all error cases, we assume that the address is bound
79 * Returns 0 for success.
82 check_bound(struct fdlist *fdl, char *uaddr)
88 if (fdl->check_binding == FALSE)
91 na = uaddr2taddr(fdl->nconf, uaddr);
93 return (TRUE); /* punt, should never happen */
95 fd = __rpc_nconf2fd(fdl->nconf);
102 ans = bind(fd, (struct sockaddr *)na->buf, na->len);
108 return (ans == 0 ? FALSE : TRUE);
112 add_bndlist(struct netconfig *nconf, struct netbuf *baddr __unused)
115 struct netconfig *newnconf;
117 newnconf = getnetconfigent(nconf->nc_netid);
118 if (newnconf == NULL)
120 fdl = malloc(sizeof (struct fdlist));
122 freenetconfigent(newnconf);
123 syslog(LOG_ERR, "no memory!");
126 fdl->nconf = newnconf;
128 if (fdhead == NULL) {
135 /* XXX no bound checking for now */
136 fdl->check_binding = FALSE;
142 is_bound(char *netid, char *uaddr)
146 for (fdl = fdhead; fdl; fdl = fdl->next)
147 if (strcmp(fdl->nconf->nc_netid, netid) == 0)
151 return (check_bound(fdl, uaddr));
155 * Returns NULL if there was some system error.
156 * Returns "" if the address was not bound, i.e the server crashed.
157 * Returns the merged address otherwise.
160 mergeaddr(SVCXPRT *xprt, char *netid, char *uaddr, char *saddr)
163 struct netbuf *callee;
164 char *c_uaddr, *s_uaddr, *m_uaddr, *allocated_uaddr = NULL;
166 for (fdl = fdhead; fdl; fdl = fdl->next)
167 if (strcmp(fdl->nconf->nc_netid, netid) == 0)
171 if (check_bound(fdl, uaddr) == FALSE)
172 /* that server died */
175 * Try to determine the local address on which the client contacted us,
176 * so we can send a reply from the same address. If it's unknown, then
177 * try to determine which address the client used, and pick a nearby
180 * If saddr is not NULL, the remote client may have included the
181 * address by which it contacted us. Use that for the "client" uaddr,
182 * otherwise use the info from the SVCXPRT.
184 callee = svc_getrpccallee(xprt);
185 if (callee != NULL && callee->buf != NULL) {
186 c_uaddr = taddr2uaddr(fdl->nconf, callee);
187 allocated_uaddr = c_uaddr;
188 } else if (saddr != NULL) {
191 c_uaddr = taddr2uaddr(fdl->nconf, svc_getrpccaller(xprt));
192 allocated_uaddr = c_uaddr;
194 if (c_uaddr == NULL) {
195 syslog(LOG_ERR, "taddr2uaddr failed for %s",
196 fdl->nconf->nc_netid);
203 fprintf(stderr, "mergeaddr: client uaddr = %s\n",
206 fprintf(stderr, "mergeaddr: contact uaddr = %s\n",
213 * This is all we should need for IP 4 and 6
215 m_uaddr = addrmerge(svc_getrpccaller(xprt), s_uaddr, c_uaddr, netid);
218 fprintf(stderr, "mergeaddr: uaddr = %s, merged uaddr = %s\n",
221 free(allocated_uaddr);
226 * Returns a netconf structure from its internal list. This
227 * structure should not be freed.
230 rpcbind_get_conf(const char *netid)
234 for (fdl = fdhead; fdl; fdl = fdl->next)
235 if (strcmp(fdl->nconf->nc_netid, netid) == 0)