Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / net-snmp / snmplib / snmp-tc.c
1 #ifdef BRCM_SNMP_NOT_USED
2 /*
3  *  Host Resources MIB - utility functions - hr_utils.c
4  *
5  */
6
7
8 #include <net-snmp/net-snmp-config.h>
9 #include <sys/types.h>
10 #if HAVE_NETINET_IN_H
11 #include <netinet/in.h>
12 #endif
13 #if HAVE_STDLIB_H
14 #include <stdlib.h>
15 #endif
16 #include <ctype.h>
17 #if HAVE_STRING_H
18 #include <string.h>
19 #endif
20 #if HAVE_STDLIB_H
21 #include <stdlib.h>
22 #endif
23
24 #if TIME_WITH_SYS_TIME
25 #ifdef WIN32
26 # include <sys/timeb.h>
27 #else
28 # include <sys/time.h>
29 #endif
30 # include <time.h>
31 #else
32 # if HAVE_SYS_TIME_H
33 #  include <sys/time.h>
34 # else
35 #  include <time.h>
36 # endif
37 #endif
38
39 #include <net-snmp/types.h>
40 #include <net-snmp/library/snmp-tc.h>   /* for "internal" definitions */
41 #include <net-snmp/library/snmp_api.h>
42
43 u_char         *
44 date_n_time(time_t * when, size_t * length)
45 {
46     struct tm      *tm_p;
47     static u_char   string[11];
48
49     /*
50      * Null time
51      */
52     if (when == NULL || *when == 0 || *when == (time_t) - 1) {
53         string[0] = 0;
54         string[1] = 0;
55         string[2] = 1;
56         string[3] = 1;
57         string[4] = 0;
58         string[5] = 0;
59         string[6] = 0;
60         string[7] = 0;
61         *length = 8;
62         return string;
63     }
64
65
66     /*
67      * Basic 'local' time handling
68      */
69     tm_p = localtime(when);
70     *(short *) string = htons(tm_p->tm_year + 1900);
71     string[2] = tm_p->tm_mon + 1;
72     string[3] = tm_p->tm_mday;
73     string[4] = tm_p->tm_hour;
74     string[5] = tm_p->tm_min;
75     string[6] = tm_p->tm_sec;
76     string[7] = 0;
77     *length = 8;
78
79 #ifndef cygwin
80     /*
81      * Timezone offset
82      */
83 #if !defined(SYSV) && !defined(aix4) && !defined(aix5) && !defined(WIN32) && !defined(irix6)
84 #define timezone tm_p->tm_gmtoff
85 #endif
86     if (timezone > 0)
87         string[8] = '-';
88     else
89         string[8] = '+';
90     string[9] = abs(timezone) / 3600;
91     string[10] = (abs(timezone) - string[9] * 3600) / 60;
92     *length = 11;
93 #endif
94
95 #ifdef SYSV
96     /*
97      * Daylight saving time
98      */
99     if (tm_p->tm_isdst > 0) {
100         /*
101          * Assume add one hour 
102          */
103         if (string[8] == '-')
104             --string[9];
105         else
106             ++string[9];
107
108         if (string[9] == 0)
109             string[8] = '+';
110     }
111 #endif
112
113     return string;
114 }
115
116
117 time_t
118 ctime_to_timet(char *string)
119 {
120     struct tm       tm;
121
122     if (strlen(string) < 24)
123         return 0;
124
125     /*
126      * Month 
127      */
128     if (!strncmp(string + 4, "Jan", 3))
129         tm.tm_mon = 0;
130     else if (!strncmp(string + 4, "Feb", 3))
131         tm.tm_mon = 1;
132     else if (!strncmp(string + 4, "Mar", 3))
133         tm.tm_mon = 2;
134     else if (!strncmp(string + 4, "Apr", 3))
135         tm.tm_mon = 3;
136     else if (!strncmp(string + 4, "May", 3))
137         tm.tm_mon = 4;
138     else if (!strncmp(string + 4, "Jun", 3))
139         tm.tm_mon = 5;
140     else if (!strncmp(string + 4, "Jul", 3))
141         tm.tm_mon = 6;
142     else if (!strncmp(string + 4, "Aug", 3))
143         tm.tm_mon = 7;
144     else if (!strncmp(string + 4, "Sep", 3))
145         tm.tm_mon = 8;
146     else if (!strncmp(string + 4, "Oct", 3))
147         tm.tm_mon = 9;
148     else if (!strncmp(string + 4, "Nov", 3))
149         tm.tm_mon = 10;
150     else if (!strncmp(string + 4, "Dec", 3))
151         tm.tm_mon = 11;
152     else
153         return 0;
154
155     tm.tm_mday = atoi(string + 8);
156     tm.tm_hour = atoi(string + 11);
157     tm.tm_min = atoi(string + 14);
158     tm.tm_sec = atoi(string + 17);
159     tm.tm_year = atoi(string + 20) - 1900;
160
161     /*
162      *  Cope with timezone and DST
163      */
164
165 #ifdef SYSV
166     if (daylight)
167         tm.tm_isdst = 1;
168
169     tm.tm_sec -= timezone;
170 #endif
171
172     return (mktime(&tm));
173 }
174
175 /*
176  * blatantly lifted from opensmp 
177  */
178 char
179 check_rowstatus_transition(int oldValue, int newValue)
180 {
181     /*
182      * From the SNMPv2-TC MIB:
183      *                                          STATE
184      *               +--------------+-----------+-------------+-------------
185      *               |      A       |     B     |      C      |      D
186      *               |              |status col.|status column|
187      *               |status column |    is     |      is     |status column
188      *     ACTION    |does not exist|  notReady | notInService|  is active
189      * --------------+--------------+-----------+-------------+-------------
190      * set status    |noError    ->D|inconsist- |inconsistent-|inconsistent-
191      * column to     |       or     |   entValue|        Value|        Value
192      * createAndGo   |inconsistent- |           |             |
193      *               |         Value|           |             |
194      * --------------+--------------+-----------+-------------+-------------
195      * set status    |noError  see 1|inconsist- |inconsistent-|inconsistent-
196      * column to     |       or     |   entValue|        Value|        Value
197      * createAndWait |wrongValue    |           |             |
198      * --------------+--------------+-----------+-------------+-------------
199      * set status    |inconsistent- |inconsist- |noError      |noError
200      * column to     |         Value|   entValue|             |
201      * active        |              |           |             |
202      *               |              |     or    |             |
203      *               |              |           |             |
204      *               |              |see 2   ->D|see 8     ->D|          ->D
205      * --------------+--------------+-----------+-------------+-------------
206      * set status    |inconsistent- |inconsist- |noError      |noError   ->C
207      * column to     |         Value|   entValue|             |
208      * notInService  |              |           |             |
209      *               |              |     or    |             |      or
210      *               |              |           |             |
211      *               |              |see 3   ->C|          ->C|see 6
212      * --------------+--------------+-----------+-------------+-------------
213      * set status    |noError       |noError    |noError      |noError   ->A
214      * column to     |              |           |             |      or
215      * destroy       |           ->A|        ->A|          ->A|see 7
216      * --------------+--------------+-----------+-------------+-------------
217      * set any other |see 4         |noError    |noError      |see 5
218      * column to some|              |           |             |
219      * value         |              |      see 1|          ->C|          ->D
220      * --------------+--------------+-----------+-------------+-------------
221      
222      *             (1) goto B or C, depending on information available to the
223      *             agent.
224      
225      *             (2) if other variable bindings included in the same PDU,
226      *             provide values for all columns which are missing but
227      *             required, and all columns have acceptable values, then
228      *             return noError and goto D.
229      
230      *             (3) if other variable bindings included in the same PDU,
231      *             provide legal values for all columns which are missing but
232      *             required, then return noError and goto C.
233      
234      *             (4) at the discretion of the agent, the return value may be
235      *             either:
236      
237      *                  inconsistentName:  because the agent does not choose to
238      *                  create such an instance when the corresponding
239      *                  RowStatus instance does not exist, or
240      
241      *                  inconsistentValue:  if the supplied value is
242      *                  inconsistent with the state of some other MIB object's
243      *                  value, or
244      
245      *                  noError: because the agent chooses to create the
246      *                  instance.
247      
248      *             If noError is returned, then the instance of the status
249      *             column must also be created, and the new state is B or C,
250      *             depending on the information available to the agent.  If
251      *             inconsistentName or inconsistentValue is returned, the row
252      *             remains in state A.
253      
254      *             (5) depending on the MIB definition for the column/table,
255      *             either noError or inconsistentValue may be returned.
256      
257      *             (6) the return value can indicate one of the following
258      *             errors:
259      
260      *                  wrongValue: because the agent does not support
261      *                  notInService (e.g., an agent which does not support
262      *                  createAndWait), or
263      
264      *                  inconsistentValue: because the agent is unable to take
265      *                  the row out of service at this time, perhaps because it
266      *                  is in use and cannot be de-activated.
267      
268      *             (7) the return value can indicate the following error:
269      
270      *                  inconsistentValue: because the agent is unable to
271      *                  remove the row at this time, perhaps because it is in
272      *                  use and cannot be de-activated.
273      
274      *             (8) the transition to D can fail, e.g., if the values of the
275      *             conceptual row are inconsistent, then the error code would
276      *             be inconsistentValue.
277      
278      *             NOTE: Other processing of (this and other varbinds of) the
279      *             set request may result in a response other than noError
280      *             being returned, e.g., wrongValue, noCreation, etc.
281      */
282
283     switch (newValue) {
284         /*
285          * these two end up being equivelent as far as checking the 
286          */
287         /*
288          * status goes, although the final states are based on the 
289          */
290         /*
291          * newValue. 
292          */
293     case RS_ACTIVE:
294     case RS_NOTINSERVICE:
295         if (oldValue == RS_NOTINSERVICE || oldValue == RS_ACTIVE);
296         else
297             return SNMP_ERR_INCONSISTENTVALUE;
298         break;
299
300     case RS_NOTREADY:
301         /*
302          * Illegal set value. 
303          */
304         return SNMP_ERR_INCONSISTENTVALUE;
305         break;
306
307     case RS_CREATEANDGO:
308         if (oldValue != RS_NONEXISTENT)
309             /*
310              * impossible, we already exist. 
311              */
312             return SNMP_ERR_INCONSISTENTVALUE;
313         break;
314
315     case RS_CREATEANDWAIT:
316         if (oldValue != RS_NONEXISTENT)
317             /*
318              * impossible, we already exist. 
319              */
320             return SNMP_ERR_INCONSISTENTVALUE;
321         break;
322
323     case RS_DESTROY:
324         break;
325
326     default:
327         return SNMP_ERR_INCONSISTENTVALUE;
328         break;
329     }
330
331     return SNMP_ERR_NOERROR;
332 }
333
334 char
335 check_storage_transition(int oldValue, int newValue)
336 {
337     /*
338      * From the SNMPv2-TC MIB:
339      
340      *             "Describes the memory realization of a conceptual row.  A
341      *             row which is volatile(2) is lost upon reboot.  A row which
342      *             is either nonVolatile(3), permanent(4) or readOnly(5), is
343      *             backed up by stable storage.  A row which is permanent(4)
344      *             can be changed but not deleted.  A row which is readOnly(5)
345      *             cannot be changed nor deleted.
346      
347      *             If the value of an object with this syntax is either
348      *             permanent(4) or readOnly(5), it cannot be written.
349      *             Conversely, if the value is either other(1), volatile(2) or
350      *             nonVolatile(3), it cannot be modified to be permanent(4) or
351      *             readOnly(5).  (All illegal modifications result in a
352      *             'wrongValue' error.)
353      
354      *             Every usage of this textual convention is required to
355      *             specify the columnar objects which a permanent(4) row must
356      *             at a minimum allow to be writable."
357      */
358     switch (oldValue) {
359     case SNMP_STORAGE_PERMANENT:
360     case SNMP_STORAGE_READONLY:
361         return SNMP_ERR_INCONSISTENTVALUE;
362
363     case SNMP_STORAGE_NONE:
364     case SNMP_STORAGE_OTHER:
365     case SNMP_STORAGE_VOLATILE:
366     case SNMP_STORAGE_NONVOLATILE:
367         if (newValue == SNMP_STORAGE_PERMANENT ||
368             newValue == SNMP_STORAGE_READONLY)
369             return SNMP_ERR_INCONSISTENTVALUE;
370     }
371
372     return SNMP_ERR_NOERROR;
373 }
374 #endif /* #if BRCM_SNMP_NOT_USED */