xref: /freebsd/usr.sbin/rpcbind/rpcb_stat.c (revision a0ee8cc6)
1 /*
2  * $NetBSD: rpcb_stat.c,v 1.2 2000/07/04 20:27:40 matt Exp $
3  * $FreeBSD$
4  */
5 /*-
6  * Copyright (c) 2009, Sun Microsystems, Inc.
7  * All rights reserved.
8  *
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.
19  *
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.
31  */
32 /* #pragma ident   "@(#)rpcb_stat.c 1.7     94/04/25 SMI" */
33 
34 /*
35  * rpcb_stat.c
36  * Allows for gathering of statistics
37  *
38  * Copyright (c) 1990 by Sun Microsystems, Inc.
39  */
40 
41 #include <stdio.h>
42 #include <netconfig.h>
43 #include <rpc/rpc.h>
44 #include <rpc/rpcb_prot.h>
45 #include <sys/stat.h>
46 #ifdef PORTMAP
47 #include <rpc/pmap_prot.h>
48 #endif
49 #include <stdlib.h>
50 #include <string.h>
51 #include "rpcbind.h"
52 
53 static rpcb_stat_byvers inf;
54 
55 void
56 rpcbs_init(void)
57 {
58 
59 }
60 
61 void
62 rpcbs_procinfo(rpcvers_t rtype, rpcproc_t proc)
63 {
64 	switch (rtype + 2) {
65 #ifdef PORTMAP
66 	case PMAPVERS:		/* version 2 */
67 		if (proc > rpcb_highproc_2)
68 			return;
69 		break;
70 #endif
71 	case RPCBVERS:		/* version 3 */
72 		if (proc > rpcb_highproc_3)
73 			return;
74 		break;
75 	case RPCBVERS4:		/* version 4 */
76 		if (proc > rpcb_highproc_4)
77 			return;
78 		break;
79 	default: return;
80 	}
81 	inf[rtype].info[proc]++;
82 	return;
83 }
84 
85 void
86 rpcbs_set(rpcvers_t rtype, bool_t success)
87 {
88 	if ((rtype >= RPCBVERS_STAT) || (success == FALSE))
89 		return;
90 	inf[rtype].setinfo++;
91 	return;
92 }
93 
94 void
95 rpcbs_unset(rpcvers_t rtype, bool_t success)
96 {
97 	if ((rtype >= RPCBVERS_STAT) || (success == FALSE))
98 		return;
99 	inf[rtype].unsetinfo++;
100 	return;
101 }
102 
103 void
104 rpcbs_getaddr(rpcvers_t rtype, rpcprog_t prog, rpcvers_t vers, char *netid,
105 	      char *uaddr)
106 {
107 	rpcbs_addrlist *al;
108 	struct netconfig *nconf;
109 
110 	if (rtype >= RPCBVERS_STAT)
111 		return;
112 	for (al = inf[rtype].addrinfo; al; al = al->next) {
113 
114 		if(al->netid == NULL)
115 			return;
116 		if ((al->prog == prog) && (al->vers == vers) &&
117 		    (strcmp(al->netid, netid) == 0)) {
118 			if ((uaddr == NULL) || (uaddr[0] == 0))
119 				al->failure++;
120 			else
121 				al->success++;
122 			return;
123 		}
124 	}
125 	nconf = rpcbind_get_conf(netid);
126 	if (nconf == NULL) {
127 		return;
128 	}
129 	al = (rpcbs_addrlist *) malloc(sizeof (rpcbs_addrlist));
130 	if (al == NULL) {
131 		return;
132 	}
133 	al->prog = prog;
134 	al->vers = vers;
135 	al->netid = nconf->nc_netid;
136 	if ((uaddr == NULL) || (uaddr[0] == 0)) {
137 		al->failure = 1;
138 		al->success = 0;
139 	} else {
140 		al->failure = 0;
141 		al->success = 1;
142 	}
143 	al->next = inf[rtype].addrinfo;
144 	inf[rtype].addrinfo = al;
145 }
146 
147 void
148 rpcbs_rmtcall(rpcvers_t rtype, rpcproc_t rpcbproc, rpcprog_t prog,
149 	      rpcvers_t vers, rpcproc_t proc, char *netid, rpcblist_ptr rbl)
150 {
151 	rpcbs_rmtcalllist *rl;
152 	struct netconfig *nconf;
153 
154 	if (rtype > RPCBVERS_STAT)
155 		return;
156 	for (rl = inf[rtype].rmtinfo; rl; rl = rl->next) {
157 
158 		if(rl->netid == NULL)
159 			return;
160 
161 		if ((rl->prog == prog) && (rl->vers == vers) &&
162 		    (rl->proc == proc) &&
163 		    (strcmp(rl->netid, netid) == 0)) {
164 			if ((rbl == NULL) ||
165 			    (rbl->rpcb_map.r_vers != vers))
166 				rl->failure++;
167 			else
168 				rl->success++;
169 			if (rpcbproc == RPCBPROC_INDIRECT)
170 				rl->indirect++;
171 			return;
172 		}
173 	}
174 	nconf = rpcbind_get_conf(netid);
175 	if (nconf == NULL) {
176 		return;
177 	}
178 	rl = (rpcbs_rmtcalllist *) malloc(sizeof (rpcbs_rmtcalllist));
179 	if (rl == NULL) {
180 		return;
181 	}
182 	rl->prog = prog;
183 	rl->vers = vers;
184 	rl->proc = proc;
185 	rl->netid = nconf->nc_netid;
186 	if ((rbl == NULL) ||
187 		    (rbl->rpcb_map.r_vers != vers)) {
188 		rl->failure = 1;
189 		rl->success = 0;
190 	} else {
191 		rl->failure = 0;
192 		rl->success = 1;
193 	}
194 	rl->indirect = 1;
195 	rl->next = inf[rtype].rmtinfo;
196 	inf[rtype].rmtinfo = rl;
197 	return;
198 }
199 
200 void *
201 rpcbproc_getstat(void *arg __unused, struct svc_req *req __unused,
202     SVCXPRT *xprt __unused, rpcvers_t versnum __unused)
203 {
204 	return (void *)&inf;
205 }
206