and added files
[bcm963xx.git] / userapps / opensource / net-snmp / agent / helpers / watcher.c
1 #ifdef BRCM_SNMP_NOT_USED
2 #include <net-snmp/net-snmp-config.h>
3
4 #include <stdlib.h>
5 #if HAVE_STRING_H
6 #include <string.h>
7 #else
8 #include <strings.h>
9 #endif
10
11 #include <net-snmp/net-snmp-includes.h>
12 #include <net-snmp/agent/net-snmp-agent-includes.h>
13
14 #include <net-snmp/agent/watcher.h>
15 #include <net-snmp/agent/instance.h>
16 #include <net-snmp/agent/scalar.h>
17
18 #if HAVE_DMALLOC_H
19 #include <dmalloc.h>
20 #endif
21
22
23 /** @defgroup watcher watcher: watch a specified variable and process
24  *   it as an instance or scalar object
25  *  @ingroup handler
26  *  @{
27  */
28 netsnmp_mib_handler *
29 netsnmp_get_watcher_handler(void)
30 {
31     return netsnmp_create_handler("watcher",
32                                   netsnmp_watcher_helper_handler);
33 }
34
35 netsnmp_watcher_info *
36 netsnmp_create_watcher_info(void *data, size_t size, u_char type, int flags)
37 {
38     netsnmp_watcher_info *winfo = SNMP_MALLOC_TYPEDEF(netsnmp_watcher_info);
39
40     winfo->data      = data;
41     winfo->data_size = size;
42     winfo->max_size  = size;    /* Probably wrong for non-fixed size data */
43     winfo->type      = type;
44     if (flags)
45         winfo->flags = flags;
46     else
47         winfo->flags = WATCHER_FIXED_SIZE;
48
49     return winfo;
50 }
51
52 int
53 netsnmp_register_watched_instance(netsnmp_handler_registration *reginfo,
54                                   netsnmp_watcher_info         *watchinfo)
55 {
56     netsnmp_mib_handler *whandler;
57
58     whandler         = netsnmp_get_watcher_handler();
59     whandler->myvoid = (void *)watchinfo;
60
61     netsnmp_inject_handler(reginfo, whandler);
62     return netsnmp_register_instance(reginfo);
63 }
64
65 int
66 netsnmp_register_watched_scalar(netsnmp_handler_registration *reginfo,
67                                   netsnmp_watcher_info         *watchinfo)
68 {
69     netsnmp_mib_handler *whandler;
70
71     whandler         = netsnmp_get_watcher_handler();
72     whandler->myvoid = (void *)watchinfo;
73
74     netsnmp_inject_handler(reginfo, whandler);
75     return netsnmp_register_scalar(reginfo);
76 }
77
78
79
80 int
81 netsnmp_watcher_helper_handler(netsnmp_mib_handler *handler,
82                                netsnmp_handler_registration *reginfo,
83                                netsnmp_agent_request_info *reqinfo,
84                                netsnmp_request_info *requests)
85 {
86     netsnmp_watcher_info *winfo = (netsnmp_watcher_info *) handler->myvoid;
87     u_char              *old_data;
88     int                  cmp;
89
90     DEBUGMSGTL(("helper:watcher", "Got request:  %d\n", reqinfo->mode));
91     cmp = snmp_oid_compare(requests->requestvb->name,
92                            requests->requestvb->name_length,
93                            reginfo->rootoid, reginfo->rootoid_len);
94
95     DEBUGMSGTL(( "helper:watcher", "  oid:", cmp));
96     DEBUGMSGOID(("helper:watcher", requests->requestvb->name,
97                                    requests->requestvb->name_length));
98     DEBUGMSG((   "helper:watcher", "\n"));
99
100
101
102     switch (reqinfo->mode) {
103         /*
104          * data requests 
105          */
106     case MODE_GET:
107         snmp_set_var_typed_value(requests->requestvb,
108                                  winfo->type,
109                                  winfo->data,
110                                  winfo->data_size);
111         break;
112
113         /*
114          * SET requests.  Should only get here if registered RWRITE 
115          */
116     case MODE_SET_RESERVE1:
117         if (requests->requestvb->type != winfo->type)
118             netsnmp_set_request_error(reqinfo, requests,
119                                       SNMP_ERR_WRONGTYPE);
120
121         if (((winfo->flags & WATCHER_MAX_SIZE) &&
122                requests->requestvb->val_len >  winfo->max_size) ||
123             ((winfo->flags & WATCHER_FIXED_SIZE) &&
124                requests->requestvb->val_len != winfo->data_size))
125              netsnmp_set_request_error(reqinfo, requests,
126                                       SNMP_ERR_WRONGLENGTH);
127         break;
128
129     case MODE_SET_RESERVE2:
130         /*
131          * store old info for undo later 
132          */
133         memdup(&old_data, (u_char *) winfo->data, winfo->data_size);
134         if (old_data == NULL) {
135             netsnmp_set_request_error(reqinfo, requests,
136                                       SNMP_ERR_RESOURCEUNAVAILABLE);
137             return SNMP_ERR_NOERROR;
138         }
139         netsnmp_request_add_list_data(requests,
140                                       netsnmp_create_data_list
141                                       ("watcher", old_data, free));
142         break;
143
144     case MODE_SET_FREE:
145         /*
146          * nothing to do 
147          */
148         break;
149
150     case MODE_SET_ACTION:
151         /*
152          * update current 
153          */
154         memcpy(winfo->data, (void *)requests->requestvb->val.string,
155                                     requests->requestvb->val_len);
156         break;
157
158     case MODE_SET_UNDO:
159         memcpy(winfo->data,
160                netsnmp_request_get_list_data(requests, "watcher"),
161                winfo->data_size);
162         break;
163
164     case MODE_SET_COMMIT:
165         winfo->data_size = requests->requestvb->val_len;
166         break;
167
168     }
169     if (handler->next && handler->next->access_method)
170         return netsnmp_call_next_handler(handler, reginfo, reqinfo,
171                                          requests);
172     return SNMP_ERR_NOERROR;
173 }
174
175
176     /***************************
177      *
178      * A specialised form of the above, reporting
179      *   the sysUpTime indicated by a given timestamp
180      *
181      ***************************/
182
183 netsnmp_mib_handler *
184 netsnmp_get_watched_timestamp_handler(void)
185 {
186     return netsnmp_create_handler("watcher",
187                                   netsnmp_watched_timestamp_handler);
188 }
189
190 int
191 netsnmp_register_watched_timestamp(netsnmp_handler_registration *reginfo,
192                                    marker_t *timestamp)
193 {
194     netsnmp_mib_handler *whandler;
195
196     whandler         = netsnmp_get_watched_timestamp_handler();
197     whandler->myvoid = (void *)timestamp;
198     netsnmp_inject_handler(reginfo, whandler);
199     return netsnmp_register_scalar(reginfo);   /* XXX - or instance? */
200 }
201
202
203 int
204 netsnmp_watched_timestamp_handler(netsnmp_mib_handler *handler,
205                                netsnmp_handler_registration *reginfo,
206                                netsnmp_agent_request_info *reqinfo,
207                                netsnmp_request_info *requests)
208 {
209     marker_t timestamp = (marker_t) handler->myvoid;
210     int      uptime;
211     int      cmp;
212
213     DEBUGMSGTL(("helper:watcher:timestamp",
214                                "Got request:  %d\n", reqinfo->mode));
215     cmp = snmp_oid_compare(requests->requestvb->name,
216                            requests->requestvb->name_length,
217                            reginfo->rootoid, reginfo->rootoid_len);
218
219     DEBUGMSGTL(( "helper:watcher:timestamp", "  oid:", cmp));
220     DEBUGMSGOID(("helper:watcher:timestamp", requests->requestvb->name,
221                                    requests->requestvb->name_length));
222     DEBUGMSG((   "helper:watcher:timestamp", "\n"));
223
224
225
226     switch (reqinfo->mode) {
227         /*
228          * data requests 
229          */
230     case MODE_GET:
231         uptime = netsnmp_marker_uptime( timestamp );
232         snmp_set_var_typed_value(requests->requestvb,
233                                  ASN_TIMETICKS,
234                                  (u_char *) &uptime,
235                                  sizeof(uptime));
236         break;
237
238         /*
239          * Timestamps are inherently Read-Only,
240          *  so don't need to support SET requests.
241          */
242     }
243     if (handler->next && handler->next->access_method)
244         return netsnmp_call_next_handler(handler, reginfo, reqinfo,
245                                          requests);
246     return SNMP_ERR_NOERROR;
247 }
248
249 #endif /* #if BRCM_SNMP_NOT_USED */