xref: /freebsd/contrib/bsnmp/snmp_mibII/mibII_ifstack.c (revision cbd30a72ca196976c1c700400ecd424baa1b9c16)
1 /*
2  * Copyright (c) 2001-2003
3  *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4  *	All rights reserved.
5  *
6  * Author: Harti Brandt <harti@freebsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $Begemot: bsnmp/snmp_mibII/mibII_ifstack.c,v 1.7 2004/08/06 08:47:00 brandt Exp $
30  *
31  * ifStackTable. Read-only.
32  */
33 #include "mibII.h"
34 
35 int
36 mib_ifstack_create(const struct mibif *lower, const struct mibif *upper)
37 {
38 	struct mibifstack *stack;
39 
40 	if ((stack = malloc(sizeof(*stack))) == NULL)
41 		return (-1);
42 
43 	stack->index.len = 2;
44 	stack->index.subs[0] = upper ? upper->index : 0;
45 	stack->index.subs[1] = lower ? lower->index : 0;
46 
47 	INSERT_OBJECT_OID(stack, &mibifstack_list);
48 
49 	mib_ifstack_last_change = get_ticks();
50 
51 	return (0);
52 }
53 
54 void
55 mib_ifstack_delete(const struct mibif *lower, const struct mibif *upper)
56 {
57 	struct mibifstack *stack;
58 
59 	TAILQ_FOREACH(stack, &mibifstack_list, link)
60 		if (stack->index.subs[0] == (upper ? upper->index : 0) &&
61 		    stack->index.subs[1] == (lower ? lower->index : 0)) {
62 			TAILQ_REMOVE(&mibifstack_list, stack, link);
63 			free(stack);
64 			mib_ifstack_last_change = get_ticks();
65 			return;
66 		}
67 }
68 
69 int
70 op_ifstack(struct snmp_context *ctx __unused, struct snmp_value *value,
71     u_int sub, u_int iidx __unused, enum snmp_op op)
72 {
73 	struct mibifstack *stack;
74 
75 	switch (op) {
76 
77 	  case SNMP_OP_GETNEXT:
78 		if ((stack = NEXT_OBJECT_OID(&mibifstack_list, &value->var, sub)) == NULL)
79 			return (SNMP_ERR_NOSUCHNAME);
80 		index_append(&value->var, sub, &stack->index);
81 		break;
82 
83 	  case SNMP_OP_GET:
84 		if ((stack = FIND_OBJECT_OID(&mibifstack_list, &value->var, sub)) == NULL)
85 			return (SNMP_ERR_NOSUCHNAME);
86 		break;
87 
88 	  case SNMP_OP_SET:
89 		if ((stack = FIND_OBJECT_OID(&mibifstack_list, &value->var, sub)) == NULL)
90 			return (SNMP_ERR_NO_CREATION);
91 		return (SNMP_ERR_NOT_WRITEABLE);
92 
93 	  case SNMP_OP_ROLLBACK:
94 	  case SNMP_OP_COMMIT:
95 		abort();
96 	}
97 
98 	switch (value->var.subs[sub - 1]) {
99 
100 	  case LEAF_ifStackStatus:
101 		value->v.integer = 1;
102 		break;
103 	}
104 	return (SNMP_ERR_NOERROR);
105 }
106