xref: /dflybsd-src/usr.sbin/rpcbind/rpcb_stat.c (revision bd91f5c31d05935d34ffa5befc63a95350bbf000)
1ce0e08e2SPeter Avalos /*
2*bd91f5c3SJustin C. Sherrill  * Copyright (c) 2009, Sun Microsystems, Inc.
3*bd91f5c3SJustin C. Sherrill  * All rights reserved.
4ce0e08e2SPeter Avalos  *
5*bd91f5c3SJustin C. Sherrill  * Redistribution and use in source and binary forms, with or without
6*bd91f5c3SJustin C. Sherrill  * modification, are permitted provided that the following conditions are met:
7*bd91f5c3SJustin C. Sherrill  * - Redistributions of source code must retain the above copyright notice,
8*bd91f5c3SJustin C. Sherrill  *   this list of conditions and the following disclaimer.
9*bd91f5c3SJustin C. Sherrill  * - Redistributions in binary form must reproduce the above copyright notice,
10*bd91f5c3SJustin C. Sherrill  *   this list of conditions and the following disclaimer in the documentation
11*bd91f5c3SJustin C. Sherrill  *   and/or other materials provided with the distribution.
12*bd91f5c3SJustin C. Sherrill  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13*bd91f5c3SJustin C. Sherrill  *   contributors may be used to endorse or promote products derived
14*bd91f5c3SJustin C. Sherrill  *   from this software without specific prior written permission.
15ce0e08e2SPeter Avalos  *
16*bd91f5c3SJustin C. Sherrill  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*bd91f5c3SJustin C. Sherrill  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*bd91f5c3SJustin C. Sherrill  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*bd91f5c3SJustin C. Sherrill  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*bd91f5c3SJustin C. Sherrill  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*bd91f5c3SJustin C. Sherrill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*bd91f5c3SJustin C. Sherrill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*bd91f5c3SJustin C. Sherrill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*bd91f5c3SJustin C. Sherrill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*bd91f5c3SJustin C. Sherrill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*bd91f5c3SJustin C. Sherrill  * POSSIBILITY OF SUCH DAMAGE.
27ce0e08e2SPeter Avalos  *
28ce0e08e2SPeter Avalos  * @(#)rpcb_stat.c 1.7     94/04/25 SMI
29ce0e08e2SPeter Avalos  * $NetBSD: rpcb_stat.c,v 1.2 2000/07/04 20:27:40 matt Exp $
30ce0e08e2SPeter Avalos  * $FreeBSD: src/usr.sbin/rpcbind/rpcb_stat.c,v 1.4 2003/10/29 09:29:23 mbr Exp $
31ce0e08e2SPeter Avalos  */
32ce0e08e2SPeter Avalos 
33ce0e08e2SPeter Avalos /*
34ce0e08e2SPeter Avalos  * rpcb_stat.c
35ce0e08e2SPeter Avalos  * Allows for gathering of statistics
36ce0e08e2SPeter Avalos  *
37ce0e08e2SPeter Avalos  * Copyright (c) 1990 by Sun Microsystems, Inc.
38ce0e08e2SPeter Avalos  */
39ce0e08e2SPeter Avalos 
40ce0e08e2SPeter Avalos #include <stdio.h>
41ce0e08e2SPeter Avalos #include <netconfig.h>
42ce0e08e2SPeter Avalos #include <rpc/rpc.h>
43ce0e08e2SPeter Avalos #include <rpc/rpcb_prot.h>
44ce0e08e2SPeter Avalos #include <sys/stat.h>
45ce0e08e2SPeter Avalos #ifdef PORTMAP
46ce0e08e2SPeter Avalos #include <rpc/pmap_prot.h>
47ce0e08e2SPeter Avalos #endif
48ce0e08e2SPeter Avalos #include <stdlib.h>
49ce0e08e2SPeter Avalos #include <string.h>
50ce0e08e2SPeter Avalos #include "rpcbind.h"
51ce0e08e2SPeter Avalos 
52ce0e08e2SPeter Avalos static rpcb_stat_byvers inf;
53ce0e08e2SPeter Avalos 
54ce0e08e2SPeter Avalos void
rpcbs_init(void)55ce0e08e2SPeter Avalos rpcbs_init(void)
56ce0e08e2SPeter Avalos {
57ce0e08e2SPeter Avalos 
58ce0e08e2SPeter Avalos }
59ce0e08e2SPeter Avalos 
60ce0e08e2SPeter Avalos void
rpcbs_procinfo(rpcvers_t rtype,rpcproc_t proc)61ce0e08e2SPeter Avalos rpcbs_procinfo(rpcvers_t rtype, rpcproc_t proc)
62ce0e08e2SPeter Avalos {
63ce0e08e2SPeter Avalos 	switch (rtype + 2) {
64ce0e08e2SPeter Avalos #ifdef PORTMAP
65ce0e08e2SPeter Avalos 	case PMAPVERS:		/* version 2 */
66ce0e08e2SPeter Avalos 		if (proc > rpcb_highproc_2)
67ce0e08e2SPeter Avalos 			return;
68ce0e08e2SPeter Avalos 		break;
69ce0e08e2SPeter Avalos #endif
70ce0e08e2SPeter Avalos 	case RPCBVERS:		/* version 3 */
71ce0e08e2SPeter Avalos 		if (proc > rpcb_highproc_3)
72ce0e08e2SPeter Avalos 			return;
73ce0e08e2SPeter Avalos 		break;
74ce0e08e2SPeter Avalos 	case RPCBVERS4:		/* version 4 */
75ce0e08e2SPeter Avalos 		if (proc > rpcb_highproc_4)
76ce0e08e2SPeter Avalos 			return;
77ce0e08e2SPeter Avalos 		break;
78ce0e08e2SPeter Avalos 	default: return;
79ce0e08e2SPeter Avalos 	}
80ce0e08e2SPeter Avalos 	inf[rtype].info[proc]++;
81ce0e08e2SPeter Avalos 	return;
82ce0e08e2SPeter Avalos }
83ce0e08e2SPeter Avalos 
84ce0e08e2SPeter Avalos void
rpcbs_set(rpcvers_t rtype,bool_t success)85ce0e08e2SPeter Avalos rpcbs_set(rpcvers_t rtype, bool_t success)
86ce0e08e2SPeter Avalos {
87ce0e08e2SPeter Avalos 	if ((rtype >= RPCBVERS_STAT) || (success == FALSE))
88ce0e08e2SPeter Avalos 		return;
89ce0e08e2SPeter Avalos 	inf[rtype].setinfo++;
90ce0e08e2SPeter Avalos 	return;
91ce0e08e2SPeter Avalos }
92ce0e08e2SPeter Avalos 
93ce0e08e2SPeter Avalos void
rpcbs_unset(rpcvers_t rtype,bool_t success)94ce0e08e2SPeter Avalos rpcbs_unset(rpcvers_t rtype, bool_t success)
95ce0e08e2SPeter Avalos {
96ce0e08e2SPeter Avalos 	if ((rtype >= RPCBVERS_STAT) || (success == FALSE))
97ce0e08e2SPeter Avalos 		return;
98ce0e08e2SPeter Avalos 	inf[rtype].unsetinfo++;
99ce0e08e2SPeter Avalos 	return;
100ce0e08e2SPeter Avalos }
101ce0e08e2SPeter Avalos 
102ce0e08e2SPeter Avalos void
rpcbs_getaddr(rpcvers_t rtype,rpcprog_t prog,rpcvers_t vers,char * netid,char * uaddr)103ce0e08e2SPeter Avalos rpcbs_getaddr(rpcvers_t rtype, rpcprog_t prog, rpcvers_t vers, char *netid,
104ce0e08e2SPeter Avalos 	      char *uaddr)
105ce0e08e2SPeter Avalos {
106ce0e08e2SPeter Avalos 	rpcbs_addrlist *al;
107ce0e08e2SPeter Avalos 	struct netconfig *nconf;
108ce0e08e2SPeter Avalos 
109ce0e08e2SPeter Avalos 	if (rtype >= RPCBVERS_STAT)
110ce0e08e2SPeter Avalos 		return;
111ce0e08e2SPeter Avalos 	for (al = inf[rtype].addrinfo; al; al = al->next) {
112ce0e08e2SPeter Avalos 
113ce0e08e2SPeter Avalos 		if(al->netid == NULL)
114ce0e08e2SPeter Avalos 			return;
115ce0e08e2SPeter Avalos 		if ((al->prog == prog) && (al->vers == vers) &&
116ce0e08e2SPeter Avalos 		    (strcmp(al->netid, netid) == 0)) {
117ce0e08e2SPeter Avalos 			if ((uaddr == NULL) || (uaddr[0] == 0))
118ce0e08e2SPeter Avalos 				al->failure++;
119ce0e08e2SPeter Avalos 			else
120ce0e08e2SPeter Avalos 				al->success++;
121ce0e08e2SPeter Avalos 			return;
122ce0e08e2SPeter Avalos 		}
123ce0e08e2SPeter Avalos 	}
124ce0e08e2SPeter Avalos 	nconf = rpcbind_get_conf(netid);
125ce0e08e2SPeter Avalos 	if (nconf == NULL) {
126ce0e08e2SPeter Avalos 		return;
127ce0e08e2SPeter Avalos 	}
128ce0e08e2SPeter Avalos 	al = (rpcbs_addrlist *) malloc(sizeof (rpcbs_addrlist));
129ce0e08e2SPeter Avalos 	if (al == NULL) {
130ce0e08e2SPeter Avalos 		return;
131ce0e08e2SPeter Avalos 	}
132ce0e08e2SPeter Avalos 	al->prog = prog;
133ce0e08e2SPeter Avalos 	al->vers = vers;
134ce0e08e2SPeter Avalos 	al->netid = nconf->nc_netid;
135ce0e08e2SPeter Avalos 	if ((uaddr == NULL) || (uaddr[0] == 0)) {
136ce0e08e2SPeter Avalos 		al->failure = 1;
137ce0e08e2SPeter Avalos 		al->success = 0;
138ce0e08e2SPeter Avalos 	} else {
139ce0e08e2SPeter Avalos 		al->failure = 0;
140ce0e08e2SPeter Avalos 		al->success = 1;
141ce0e08e2SPeter Avalos 	}
142ce0e08e2SPeter Avalos 	al->next = inf[rtype].addrinfo;
143ce0e08e2SPeter Avalos 	inf[rtype].addrinfo = al;
144ce0e08e2SPeter Avalos }
145ce0e08e2SPeter Avalos 
146ce0e08e2SPeter Avalos void
rpcbs_rmtcall(rpcvers_t rtype,rpcproc_t rpcbproc,rpcprog_t prog,rpcvers_t vers,rpcproc_t proc,char * netid,rpcblist_ptr rbl)147ce0e08e2SPeter Avalos rpcbs_rmtcall(rpcvers_t rtype, rpcproc_t rpcbproc, rpcprog_t prog,
148ce0e08e2SPeter Avalos 	      rpcvers_t vers, rpcproc_t proc, char *netid, rpcblist_ptr rbl)
149ce0e08e2SPeter Avalos {
150ce0e08e2SPeter Avalos 	rpcbs_rmtcalllist *rl;
151ce0e08e2SPeter Avalos 	struct netconfig *nconf;
152ce0e08e2SPeter Avalos 
153ce0e08e2SPeter Avalos 	if (rtype > RPCBVERS_STAT)
154ce0e08e2SPeter Avalos 		return;
155ce0e08e2SPeter Avalos 	for (rl = inf[rtype].rmtinfo; rl; rl = rl->next) {
156ce0e08e2SPeter Avalos 
157ce0e08e2SPeter Avalos 		if(rl->netid == NULL)
158ce0e08e2SPeter Avalos 			return;
159ce0e08e2SPeter Avalos 
160ce0e08e2SPeter Avalos 		if ((rl->prog == prog) && (rl->vers == vers) &&
161ce0e08e2SPeter Avalos 		    (rl->proc == proc) &&
162ce0e08e2SPeter Avalos 		    (strcmp(rl->netid, netid) == 0)) {
163ce0e08e2SPeter Avalos 			if ((rbl == NULL) ||
164ce0e08e2SPeter Avalos 			    (rbl->rpcb_map.r_vers != vers))
165ce0e08e2SPeter Avalos 				rl->failure++;
166ce0e08e2SPeter Avalos 			else
167ce0e08e2SPeter Avalos 				rl->success++;
168ce0e08e2SPeter Avalos 			if (rpcbproc == RPCBPROC_INDIRECT)
169ce0e08e2SPeter Avalos 				rl->indirect++;
170ce0e08e2SPeter Avalos 			return;
171ce0e08e2SPeter Avalos 		}
172ce0e08e2SPeter Avalos 	}
173ce0e08e2SPeter Avalos 	nconf = rpcbind_get_conf(netid);
174ce0e08e2SPeter Avalos 	if (nconf == NULL) {
175ce0e08e2SPeter Avalos 		return;
176ce0e08e2SPeter Avalos 	}
177ce0e08e2SPeter Avalos 	rl = (rpcbs_rmtcalllist *) malloc(sizeof (rpcbs_rmtcalllist));
178ce0e08e2SPeter Avalos 	if (rl == NULL) {
179ce0e08e2SPeter Avalos 		return;
180ce0e08e2SPeter Avalos 	}
181ce0e08e2SPeter Avalos 	rl->prog = prog;
182ce0e08e2SPeter Avalos 	rl->vers = vers;
183ce0e08e2SPeter Avalos 	rl->proc = proc;
184ce0e08e2SPeter Avalos 	rl->netid = nconf->nc_netid;
185ce0e08e2SPeter Avalos 	if ((rbl == NULL) ||
186ce0e08e2SPeter Avalos 		    (rbl->rpcb_map.r_vers != vers)) {
187ce0e08e2SPeter Avalos 		rl->failure = 1;
188ce0e08e2SPeter Avalos 		rl->success = 0;
189ce0e08e2SPeter Avalos 	} else {
190ce0e08e2SPeter Avalos 		rl->failure = 0;
191ce0e08e2SPeter Avalos 		rl->success = 1;
192ce0e08e2SPeter Avalos 	}
193ce0e08e2SPeter Avalos 	rl->indirect = 1;
194ce0e08e2SPeter Avalos 	rl->next = inf[rtype].rmtinfo;
195ce0e08e2SPeter Avalos 	inf[rtype].rmtinfo = rl;
196ce0e08e2SPeter Avalos 	return;
197ce0e08e2SPeter Avalos }
198ce0e08e2SPeter Avalos 
199ce0e08e2SPeter Avalos void *
rpcbproc_getstat(void * arg __unused,struct svc_req * req __unused,SVCXPRT * xprt __unused,rpcvers_t versnum __unused)200ce0e08e2SPeter Avalos rpcbproc_getstat(void *arg __unused, struct svc_req *req __unused,
201ce0e08e2SPeter Avalos 		 SVCXPRT *xprt __unused, rpcvers_t versnum __unused)
202ce0e08e2SPeter Avalos {
203ce0e08e2SPeter Avalos 	return (void *)&inf;
204ce0e08e2SPeter Avalos }
205