1 /* $NetBSD: check_bound.c,v 1.2 2000/06/22 08:09:26 fvdl Exp $ */
5 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
6 * unrestricted use provided that this legend is included on all tape
7 * media and as a part of the software program in whole or part. Users
8 * may copy or modify Sun RPC without charge, but are not authorized
9 * to license or distribute it to anyone else except as part of a product or
10 * program developed by the user.
12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
16 * Sun RPC is provided with no support and without any obligation on the
17 * part of Sun Microsystems, Inc. to assist in its use, correction,
18 * modification or enhancement.
20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22 * OR ANY PART THEREOF.
24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25 * or profits or other special, indirect and consequential damages, even if
26 * Sun has been advised of the possibility of such damages.
28 * Sun Microsystems, Inc.
30 * Mountain View, California 94043
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 univeral merged address
51 #include <sys/types.h>
52 #include <sys/socket.h>
54 #include <rpc/svc_dg.h>
56 #include <netconfig.h>
66 struct netconfig *nconf;
71 static struct fdlist *fdhead; /* Link list of the check fd's */
72 static struct fdlist *fdtail;
73 static char *nullstring = "";
75 static bool_t check_bound(struct fdlist *, char *uaddr);
78 * Returns 1 if the given address is bound for the given addr & transport
79 * For all error cases, we assume that the address is bound
80 * Returns 0 for success.
83 check_bound(struct fdlist *fdl, char *uaddr)
89 if (fdl->check_binding == FALSE)
92 na = uaddr2taddr(fdl->nconf, uaddr);
94 return (TRUE); /* punt, should never happen */
96 fd = __rpc_nconf2fd(fdl->nconf);
103 ans = bind(fd, (struct sockaddr *)na->buf, na->len);
109 return (ans == 0 ? FALSE : TRUE);
113 add_bndlist(struct netconfig *nconf, struct netbuf *baddr __unused)
116 struct netconfig *newnconf;
118 newnconf = getnetconfigent(nconf->nc_netid);
119 if (newnconf == NULL)
121 fdl = malloc(sizeof (struct fdlist));
123 freenetconfigent(newnconf);
124 syslog(LOG_ERR, "no memory!");
127 fdl->nconf = newnconf;
129 if (fdhead == NULL) {
136 /* XXX no bound checking for now */
137 fdl->check_binding = FALSE;
143 is_bound(char *netid, char *uaddr)
147 for (fdl = fdhead; fdl; fdl = fdl->next)
148 if (strcmp(fdl->nconf->nc_netid, netid) == 0)
152 return (check_bound(fdl, uaddr));
156 * Returns NULL if there was some system error.
157 * Returns "" if the address was not bound, i.e the server crashed.
158 * Returns the merged address otherwise.
161 mergeaddr(SVCXPRT *xprt, char *netid, char *uaddr, char *saddr)
164 struct svc_dg_data *dg_data;
165 char *c_uaddr, *s_uaddr, *m_uaddr, *allocated_uaddr = NULL;
167 for (fdl = fdhead; fdl; fdl = fdl->next)
168 if (strcmp(fdl->nconf->nc_netid, netid) == 0)
172 if (check_bound(fdl, uaddr) == FALSE)
173 /* that server died */
176 * Try to determine the local address on which the client contacted us,
177 * so we can send a reply from the same address. If it's unknown, then
178 * try to determine which address the client used, and pick a nearby
181 * If saddr is not NULL, the remote client may have included the
182 * address by which it contacted us. Use that for the "client" uaddr,
183 * otherwise use the info from the SVCXPRT.
185 dg_data = (struct svc_dg_data*)xprt->xp_p2;
186 if (dg_data != NULL && dg_data->su_srcaddr.buf != NULL) {
187 c_uaddr = taddr2uaddr(fdl->nconf, &dg_data->su_srcaddr);
188 allocated_uaddr = c_uaddr;
190 else if (saddr != NULL) {
193 c_uaddr = taddr2uaddr(fdl->nconf, svc_getrpccaller(xprt));
194 allocated_uaddr = c_uaddr;
196 if (c_uaddr == NULL) {
197 syslog(LOG_ERR, "taddr2uaddr failed for %s",
198 fdl->nconf->nc_netid);
205 fprintf(stderr, "mergeaddr: client uaddr = %s\n",
208 fprintf(stderr, "mergeaddr: contact uaddr = %s\n",
215 * This is all we should need for IP 4 and 6
217 m_uaddr = addrmerge(svc_getrpccaller(xprt), s_uaddr, c_uaddr, netid);
220 fprintf(stderr, "mergeaddr: uaddr = %s, merged uaddr = %s\n",
223 free(allocated_uaddr);
228 * Returns a netconf structure from its internal list. This
229 * structure should not be freed.
232 rpcbind_get_conf(const char *netid)
236 for (fdl = fdhead; fdl; fdl = fdl->next)
237 if (strcmp(fdl->nconf->nc_netid, netid) == 0)