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