1 /* $NetBSD: check_bound.c,v 1.2 2000/06/22 08:09:26 fvdl Exp $ */
5 * Copyright (c) 2009, Sun Microsystems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 * - Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 * - Neither the name of Sun Microsystems, Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
35 /* #ident "@(#)check_bound.c 1.15 93/07/05 SMI" */
39 static char sccsid[] = "@(#)check_bound.c 1.11 89/04/21 Copyr 1989 Sun Micro";
45 * Checks to see whether the program is still bound to the
46 * claimed address and returns the universal merged address
50 #include <sys/types.h>
51 #include <sys/socket.h>
54 #include <netconfig.h>
64 struct netconfig *nconf;
69 static struct fdlist *fdhead; /* Link list of the check fd's */
70 static struct fdlist *fdtail;
71 static char *nullstring = "";
73 static bool_t check_bound(struct fdlist *, char *uaddr);
76 * Returns 1 if the given address is bound for the given addr & transport
77 * For all error cases, we assume that the address is bound
78 * Returns 0 for success.
81 check_bound(struct fdlist *fdl, char *uaddr)
87 if (fdl->check_binding == FALSE)
90 na = uaddr2taddr(fdl->nconf, uaddr);
92 return (TRUE); /* punt, should never happen */
94 fd = __rpc_nconf2fd(fdl->nconf);
101 ans = bind(fd, (struct sockaddr *)na->buf, na->len);
107 return (ans == 0 ? FALSE : TRUE);
111 add_bndlist(struct netconfig *nconf, struct netbuf *baddr __unused)
114 struct netconfig *newnconf;
116 newnconf = getnetconfigent(nconf->nc_netid);
117 if (newnconf == NULL)
119 fdl = malloc(sizeof (struct fdlist));
121 freenetconfigent(newnconf);
122 syslog(LOG_ERR, "no memory!");
125 fdl->nconf = newnconf;
127 if (fdhead == NULL) {
134 /* XXX no bound checking for now */
135 fdl->check_binding = FALSE;
141 is_bound(char *netid, char *uaddr)
145 for (fdl = fdhead; fdl; fdl = fdl->next)
146 if (strcmp(fdl->nconf->nc_netid, netid) == 0)
150 return (check_bound(fdl, uaddr));
154 * Returns NULL if there was some system error.
155 * Returns "" if the address was not bound, i.e the server crashed.
156 * Returns the merged address otherwise.
159 mergeaddr(SVCXPRT *xprt, char *netid, char *uaddr, char *saddr)
162 char *c_uaddr, *s_uaddr, *m_uaddr, *allocated_uaddr = NULL;
164 for (fdl = fdhead; fdl; fdl = fdl->next)
165 if (strcmp(fdl->nconf->nc_netid, netid) == 0)
169 if (check_bound(fdl, uaddr) == FALSE)
170 /* that server died */
173 * If saddr is not NULL, the remote client may have included the
174 * address by which it contacted us. Use that for the "client" uaddr,
175 * otherwise use the info from the SVCXPRT.
180 c_uaddr = taddr2uaddr(fdl->nconf, svc_getrpccaller(xprt));
181 if (c_uaddr == NULL) {
182 syslog(LOG_ERR, "taddr2uaddr failed for %s",
183 fdl->nconf->nc_netid);
186 allocated_uaddr = c_uaddr;
192 fprintf(stderr, "mergeaddr: client uaddr = %s\n",
195 fprintf(stderr, "mergeaddr: contact uaddr = %s\n",
202 * This is all we should need for IP 4 and 6
204 m_uaddr = addrmerge(svc_getrpccaller(xprt), s_uaddr, c_uaddr, netid);
207 fprintf(stderr, "mergeaddr: uaddr = %s, merged uaddr = %s\n",
210 if (allocated_uaddr != NULL)
211 free(allocated_uaddr);
216 * Returns a netconf structure from its internal list. This
217 * structure should not be freed.
220 rpcbind_get_conf(char *netid)
224 for (fdl = fdhead; fdl; fdl = fdl->next)
225 if (strcmp(fdl->nconf->nc_netid, netid) == 0)