xref: /onnv-gate/usr/src/cmd/agents/snmp/agent/subtree.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1998 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdlib.h>
30*0Sstevel@tonic-gate #include <stdio.h>
31*0Sstevel@tonic-gate #include <string.h>
32*0Sstevel@tonic-gate #include <sys/types.h>
33*0Sstevel@tonic-gate #include <netinet/in.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include "impl.h"
36*0Sstevel@tonic-gate #include "error.h"
37*0Sstevel@tonic-gate #include "trace.h"
38*0Sstevel@tonic-gate #include "asn1.h"
39*0Sstevel@tonic-gate #include "snmp.h"
40*0Sstevel@tonic-gate #include "pdu.h"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #include "pagent.h"
43*0Sstevel@tonic-gate #include "subtree.h"
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate /***** STATIC VARIABLES *****/
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate static Subtree *first_subtree = NULL;
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /***** STATIC FUNCTIONS *****/
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate static void subtree_remove_from_agent_list(Subtree *subtree);
53*0Sstevel@tonic-gate static void subtree_free(Subtree *sp);
54*0Sstevel@tonic-gate extern int SSARegSubtree(SSA_Subtree *);
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate /****************************************************************/
58*0Sstevel@tonic-gate /* currently, the index are processid(agentid) and local index */
59*0Sstevel@tonic-gate 
subtree_add(Agent * agent,Subid * subids,int len)60*0Sstevel@tonic-gate int subtree_add(Agent *agent, Subid *subids, int len)
61*0Sstevel@tonic-gate {
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 	/* call reg. api */
64*0Sstevel@tonic-gate 	Subtree *sp;
65*0Sstevel@tonic-gate 	Subtree *new;
66*0Sstevel@tonic-gate 	Subtree *last = NULL;
67*0Sstevel@tonic-gate 	int ret;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	if(agent == NULL)
71*0Sstevel@tonic-gate 	{
72*0Sstevel@tonic-gate 		error("BUG: subtree_add(): agent is NULL");
73*0Sstevel@tonic-gate 		return -1;
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	new = (Subtree *) malloc(sizeof(Subtree));
77*0Sstevel@tonic-gate 	if(new == NULL)
78*0Sstevel@tonic-gate 	{
79*0Sstevel@tonic-gate 		error("malloc() failed");
80*0Sstevel@tonic-gate 		return -1;
81*0Sstevel@tonic-gate 	}
82*0Sstevel@tonic-gate 	new->next_subtree = NULL;
83*0Sstevel@tonic-gate 	new->agent = agent;
84*0Sstevel@tonic-gate 	new->next_agent_subtree = NULL;
85*0Sstevel@tonic-gate 	new->name.subids = (Subid *) malloc(len * (int32_t)sizeof(Subid));
86*0Sstevel@tonic-gate 	if(new->name.subids == NULL)
87*0Sstevel@tonic-gate 	{
88*0Sstevel@tonic-gate 		error("malloc() failed");
89*0Sstevel@tonic-gate 		subtree_free(new);
90*0Sstevel@tonic-gate 		return -1;
91*0Sstevel@tonic-gate 	}
92*0Sstevel@tonic-gate 	(void)memcpy(new->name.subids, subids, len * (int32_t)sizeof(Subid));
93*0Sstevel@tonic-gate 	new->name.len = len;
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 	for(sp = first_subtree; sp; sp = sp->next_subtree)
96*0Sstevel@tonic-gate 	{
97*0Sstevel@tonic-gate 		ret = SSAOidCmp(&(new->name), &(sp->name));
98*0Sstevel@tonic-gate 		if(ret == 0)
99*0Sstevel@tonic-gate 		{
100*0Sstevel@tonic-gate 			error("The subtree %s already belongs to the agent %s",
101*0Sstevel@tonic-gate 				SSAOidString(&(sp->name)),
102*0Sstevel@tonic-gate 				sp->agent->name);
103*0Sstevel@tonic-gate 			subtree_free(new);
104*0Sstevel@tonic-gate 			return -1;
105*0Sstevel@tonic-gate 		}
106*0Sstevel@tonic-gate 		else
107*0Sstevel@tonic-gate 		if(ret < 0)
108*0Sstevel@tonic-gate 		{
109*0Sstevel@tonic-gate 			break;
110*0Sstevel@tonic-gate 		}
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 		last = sp;
113*0Sstevel@tonic-gate 	}
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	if(last == NULL)
116*0Sstevel@tonic-gate 	{
117*0Sstevel@tonic-gate 		new->next_subtree = first_subtree;
118*0Sstevel@tonic-gate 		first_subtree = new;
119*0Sstevel@tonic-gate 	}
120*0Sstevel@tonic-gate 	else
121*0Sstevel@tonic-gate 	{
122*0Sstevel@tonic-gate 		new->next_subtree = last->next_subtree;
123*0Sstevel@tonic-gate 		last->next_subtree = new;
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	new->next_agent_subtree = agent->first_agent_subtree;
127*0Sstevel@tonic-gate 	agent->first_agent_subtree = new;
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	new->regTreeIndex = ++new->agent->tree_index;
130*0Sstevel@tonic-gate 	new->regTreeAgentID = new->agent->agent_id;
131*0Sstevel@tonic-gate 	new->regTreeStatus = SSA_OPER_STATUS_ACTIVE;
132*0Sstevel@tonic-gate 	if(SSARegSubtree(new)==0)
133*0Sstevel@tonic-gate 	{
134*0Sstevel@tonic-gate 		return -1;
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	return 0;
138*0Sstevel@tonic-gate }
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate /****************************************************************/
142*0Sstevel@tonic-gate 
subtree_match(u_char type,Oid * name)143*0Sstevel@tonic-gate Subtree *subtree_match(u_char type, Oid *name)
144*0Sstevel@tonic-gate {
145*0Sstevel@tonic-gate 	Subtree *sp;
146*0Sstevel@tonic-gate 	Subtree *last;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	if(name == NULL)
150*0Sstevel@tonic-gate 	{
151*0Sstevel@tonic-gate 		error("subtree_match(): name is NULL");
152*0Sstevel@tonic-gate 		return NULL;
153*0Sstevel@tonic-gate 	}
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	if(first_subtree == NULL)
156*0Sstevel@tonic-gate 	{
157*0Sstevel@tonic-gate /*
158*0Sstevel@tonic-gate 		if(trace_level > 1)
159*0Sstevel@tonic-gate 		{
160*0Sstevel@tonic-gate 			trace("subtree_match() returned NULL\n\n");
161*0Sstevel@tonic-gate 		}
162*0Sstevel@tonic-gate */
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 		return NULL;
165*0Sstevel@tonic-gate 	}
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	if(type == GETNEXT_REQ_MSG)
169*0Sstevel@tonic-gate 	{
170*0Sstevel@tonic-gate 		if(SSAOidCmp(name, &(first_subtree->name)) < 0)
171*0Sstevel@tonic-gate 		{
172*0Sstevel@tonic-gate /*
173*0Sstevel@tonic-gate 			if(trace_level > 1)
174*0Sstevel@tonic-gate 			{
175*0Sstevel@tonic-gate 				trace("subtree_match() returned %s supported by %s\n\n",
176*0Sstevel@tonic-gate 					SSAOidString(&(first_subtree->name)),
177*0Sstevel@tonic-gate 					first_subtree->agent->name);
178*0Sstevel@tonic-gate 			}
179*0Sstevel@tonic-gate */
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 			return first_subtree;
182*0Sstevel@tonic-gate 		}
183*0Sstevel@tonic-gate 	}
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	last = NULL;
186*0Sstevel@tonic-gate 	for(sp = first_subtree; sp; sp = sp->next_subtree)
187*0Sstevel@tonic-gate 	{
188*0Sstevel@tonic-gate 		if(SSAOidCmp(name, &(sp->name)) < 0)
189*0Sstevel@tonic-gate 		{
190*0Sstevel@tonic-gate 			break;
191*0Sstevel@tonic-gate 		}
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 		if(sp->name.len <= name->len)
194*0Sstevel@tonic-gate 		{
195*0Sstevel@tonic-gate 			int i;
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 			for(i = 0; i < sp->name.len; i++)
199*0Sstevel@tonic-gate 			{
200*0Sstevel@tonic-gate 				if(sp->name.subids[i] == 0)
201*0Sstevel@tonic-gate 				{
202*0Sstevel@tonic-gate 					continue;
203*0Sstevel@tonic-gate 				}
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 				if(name->subids[i] != sp->name.subids[i])
206*0Sstevel@tonic-gate 				{
207*0Sstevel@tonic-gate 					break;
208*0Sstevel@tonic-gate 				}
209*0Sstevel@tonic-gate 			}
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 			if(i == sp->name.len)
212*0Sstevel@tonic-gate 			{
213*0Sstevel@tonic-gate 				last = sp;
214*0Sstevel@tonic-gate 			}
215*0Sstevel@tonic-gate 		}
216*0Sstevel@tonic-gate 	}
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate /*
220*0Sstevel@tonic-gate 	if(trace_level > 1)
221*0Sstevel@tonic-gate 	{
222*0Sstevel@tonic-gate 		if(last)
223*0Sstevel@tonic-gate 		{
224*0Sstevel@tonic-gate 			trace("subtree_match() returned %s supported by %s\n\n",
225*0Sstevel@tonic-gate 				SSAOidString(&(last->name)),
226*0Sstevel@tonic-gate 				last->agent->name);
227*0Sstevel@tonic-gate 		}
228*0Sstevel@tonic-gate 		else
229*0Sstevel@tonic-gate 		{
230*0Sstevel@tonic-gate 			trace("subtree_match() returned NULL\n\n");
231*0Sstevel@tonic-gate 		}
232*0Sstevel@tonic-gate 	}
233*0Sstevel@tonic-gate */
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	return last;
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate /****************************************************************/
241*0Sstevel@tonic-gate 
trace_subtrees()242*0Sstevel@tonic-gate void trace_subtrees()
243*0Sstevel@tonic-gate {
244*0Sstevel@tonic-gate 	Subtree *sp;
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	trace("SUBTREES:\n");
248*0Sstevel@tonic-gate 	for(sp = first_subtree; sp; sp = sp->next_subtree)
249*0Sstevel@tonic-gate 	{
250*0Sstevel@tonic-gate 		if(sp->agent)
251*0Sstevel@tonic-gate 		{
252*0Sstevel@tonic-gate 			trace("\t%-30s %-30s %d\n",
253*0Sstevel@tonic-gate 				SSAOidString(&(sp->name)),
254*0Sstevel@tonic-gate 				sp->agent->name,
255*0Sstevel@tonic-gate 				sp->agent->address.sin_port);
256*0Sstevel@tonic-gate 		}
257*0Sstevel@tonic-gate 		else
258*0Sstevel@tonic-gate 		{
259*0Sstevel@tonic-gate 			trace("\t%-30s %-30s\n",
260*0Sstevel@tonic-gate 				SSAOidString(&(sp->name)),
261*0Sstevel@tonic-gate 				"NO AGENT!");
262*0Sstevel@tonic-gate 		}
263*0Sstevel@tonic-gate 	}
264*0Sstevel@tonic-gate 	trace("\n");
265*0Sstevel@tonic-gate }
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate /****************************************************************/
269*0Sstevel@tonic-gate 
subtree_free(Subtree * sp)270*0Sstevel@tonic-gate static void subtree_free(Subtree *sp)
271*0Sstevel@tonic-gate {
272*0Sstevel@tonic-gate 	if(sp == NULL)
273*0Sstevel@tonic-gate 	{
274*0Sstevel@tonic-gate 		return;
275*0Sstevel@tonic-gate 	}
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	if(sp->name.subids)
278*0Sstevel@tonic-gate 	{
279*0Sstevel@tonic-gate 		free(sp->name.subids);
280*0Sstevel@tonic-gate 	}
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	free(sp);
283*0Sstevel@tonic-gate }
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate /****************************************************************/
287*0Sstevel@tonic-gate 
subtree_list_delete()288*0Sstevel@tonic-gate void subtree_list_delete()
289*0Sstevel@tonic-gate {
290*0Sstevel@tonic-gate 	Subtree *sp = first_subtree;
291*0Sstevel@tonic-gate 	Subtree *next;
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	while(sp)
295*0Sstevel@tonic-gate 	{
296*0Sstevel@tonic-gate 		next = sp->next_subtree;
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 		subtree_remove_from_agent_list(sp);
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 		subtree_free(sp);
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 		sp = next;
303*0Sstevel@tonic-gate 	}
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 	first_subtree = NULL;
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 	return;
308*0Sstevel@tonic-gate }
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate /****************************************************************/
312*0Sstevel@tonic-gate 
subtree_remove_from_agent_list(Subtree * subtree)313*0Sstevel@tonic-gate static void subtree_remove_from_agent_list(Subtree *subtree)
314*0Sstevel@tonic-gate {
315*0Sstevel@tonic-gate 	Agent *agent = subtree->agent;
316*0Sstevel@tonic-gate 	Subtree *sp;
317*0Sstevel@tonic-gate 	Subtree *osp;
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 	osp = NULL;
321*0Sstevel@tonic-gate 	for(sp = agent->first_agent_subtree; sp; sp = sp->next_agent_subtree)
322*0Sstevel@tonic-gate 	{
323*0Sstevel@tonic-gate 		if(sp == subtree)
324*0Sstevel@tonic-gate 		{
325*0Sstevel@tonic-gate 			break;
326*0Sstevel@tonic-gate 		}
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 		osp = sp;
329*0Sstevel@tonic-gate 	}
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	if(sp == NULL)
332*0Sstevel@tonic-gate 	{
333*0Sstevel@tonic-gate 		error("subtree_remove_from_agent_list() : subtree (0x%x) not found", subtree);
334*0Sstevel@tonic-gate 		return;
335*0Sstevel@tonic-gate 	}
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 	if(osp == NULL)
338*0Sstevel@tonic-gate 	{
339*0Sstevel@tonic-gate 		agent->first_agent_subtree = sp->next_agent_subtree;
340*0Sstevel@tonic-gate 	}
341*0Sstevel@tonic-gate 	else
342*0Sstevel@tonic-gate 	{
343*0Sstevel@tonic-gate 		osp->next_agent_subtree = sp->next_agent_subtree;
344*0Sstevel@tonic-gate 	}
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 	subtree->agent = NULL;
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	return;
349*0Sstevel@tonic-gate }
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 
353