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