xref: /freebsd/usr.sbin/rpcbind/rpcb_stat.c (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
1 /*
2  * $NetBSD: rpcb_stat.c,v 1.2 2000/07/04 20:27:40 matt Exp $
3  * $FreeBSD$
4  */
5 /*
6  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
7  * unrestricted use provided that this legend is included on all tape
8  * media and as a part of the software program in whole or part.  Users
9  * may copy or modify Sun RPC without charge, but are not authorized
10  * to license or distribute it to anyone else except as part of a product or
11  * program developed by the user.
12  *
13  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
14  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
15  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
16  *
17  * Sun RPC is provided with no support and without any obligation on the
18  * part of Sun Microsystems, Inc. to assist in its use, correction,
19  * modification or enhancement.
20  *
21  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
22  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
23  * OR ANY PART THEREOF.
24  *
25  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
26  * or profits or other special, indirect and consequential damages, even if
27  * Sun has been advised of the possibility of such damages.
28  *
29  * Sun Microsystems, Inc.
30  * 2550 Garcia Avenue
31  * Mountain View, California  94043
32  */
33 /* #pragma ident   "@(#)rpcb_stat.c 1.7     94/04/25 SMI" */
34 
35 /*
36  * rpcb_stat.c
37  * Allows for gathering of statistics
38  *
39  * Copyright (c) 1990 by Sun Microsystems, Inc.
40  */
41 
42 #include <stdio.h>
43 #include <netconfig.h>
44 #include <rpc/rpc.h>
45 #include <rpc/rpcb_prot.h>
46 #include <sys/stat.h>
47 #ifdef PORTMAP
48 #include <rpc/pmap_prot.h>
49 #endif
50 #include <stdlib.h>
51 #include <string.h>
52 #include "rpcbind.h"
53 
54 static rpcb_stat_byvers inf;
55 
56 void
57 rpcbs_init(void)
58 {
59 
60 }
61 
62 void
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 	return;
84 }
85 
86 void
87 rpcbs_set(rpcvers_t rtype, bool_t success)
88 {
89 	if ((rtype >= RPCBVERS_STAT) || (success == FALSE))
90 		return;
91 	inf[rtype].setinfo++;
92 	return;
93 }
94 
95 void
96 rpcbs_unset(rpcvers_t rtype, bool_t success)
97 {
98 	if ((rtype >= RPCBVERS_STAT) || (success == FALSE))
99 		return;
100 	inf[rtype].unsetinfo++;
101 	return;
102 }
103 
104 void
105 rpcbs_getaddr(rpcvers_t rtype, rpcprog_t prog, rpcvers_t vers, char *netid,
106 	      char *uaddr)
107 {
108 	rpcbs_addrlist *al;
109 	struct netconfig *nconf;
110 
111 	if (rtype >= RPCBVERS_STAT)
112 		return;
113 	for (al = inf[rtype].addrinfo; al; al = al->next) {
114 
115 		if(al->netid == NULL)
116 			return;
117 		if ((al->prog == prog) && (al->vers == vers) &&
118 		    (strcmp(al->netid, netid) == 0)) {
119 			if ((uaddr == NULL) || (uaddr[0] == 0))
120 				al->failure++;
121 			else
122 				al->success++;
123 			return;
124 		}
125 	}
126 	nconf = rpcbind_get_conf(netid);
127 	if (nconf == NULL) {
128 		return;
129 	}
130 	al = (rpcbs_addrlist *) malloc(sizeof (rpcbs_addrlist));
131 	if (al == NULL) {
132 		return;
133 	}
134 	al->prog = prog;
135 	al->vers = vers;
136 	al->netid = nconf->nc_netid;
137 	if ((uaddr == NULL) || (uaddr[0] == 0)) {
138 		al->failure = 1;
139 		al->success = 0;
140 	} else {
141 		al->failure = 0;
142 		al->success = 1;
143 	}
144 	al->next = inf[rtype].addrinfo;
145 	inf[rtype].addrinfo = al;
146 }
147 
148 void
149 rpcbs_rmtcall(rpcvers_t rtype, rpcproc_t rpcbproc, rpcprog_t prog,
150 	      rpcvers_t vers, rpcproc_t proc, char *netid, rpcblist_ptr rbl)
151 {
152 	rpcbs_rmtcalllist *rl;
153 	struct netconfig *nconf;
154 
155 	if (rtype > RPCBVERS_STAT)
156 		return;
157 	for (rl = inf[rtype].rmtinfo; rl; rl = rl->next) {
158 
159 		if(rl->netid == NULL)
160 			return;
161 
162 		if ((rl->prog == prog) && (rl->vers == vers) &&
163 		    (rl->proc == proc) &&
164 		    (strcmp(rl->netid, netid) == 0)) {
165 			if ((rbl == NULL) ||
166 			    (rbl->rpcb_map.r_vers != vers))
167 				rl->failure++;
168 			else
169 				rl->success++;
170 			if (rpcbproc == RPCBPROC_INDIRECT)
171 				rl->indirect++;
172 			return;
173 		}
174 	}
175 	nconf = rpcbind_get_conf(netid);
176 	if (nconf == NULL) {
177 		return;
178 	}
179 	rl = (rpcbs_rmtcalllist *) malloc(sizeof (rpcbs_rmtcalllist));
180 	if (rl == NULL) {
181 		return;
182 	}
183 	rl->prog = prog;
184 	rl->vers = vers;
185 	rl->proc = proc;
186 	rl->netid = nconf->nc_netid;
187 	if ((rbl == NULL) ||
188 		    (rbl->rpcb_map.r_vers != vers)) {
189 		rl->failure = 1;
190 		rl->success = 0;
191 	} else {
192 		rl->failure = 0;
193 		rl->success = 1;
194 	}
195 	rl->indirect = 1;
196 	rl->next = inf[rtype].rmtinfo;
197 	inf[rtype].rmtinfo = rl;
198 	return;
199 }
200 
201 void *
202 rpcbproc_getstat(void *arg __unused, struct svc_req *req __unused,
203     SVCXPRT *xprt __unused, rpcvers_t versnum __unused)
204 {
205 	return (void *)&inf;
206 }
207