OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [snmp/] [lib/] [v2_0/] [src/] [lcd_time.c] - Blame information for rev 27

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      ./lib/current/src/lcd_time.c
4
//
5
//
6
//==========================================================================
7
//####ECOSGPLCOPYRIGHTBEGIN####
8
// -------------------------------------------
9
// This file is part of eCos, the Embedded Configurable Operating System.
10
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
11
//
12
// eCos is free software; you can redistribute it and/or modify it under
13
// the terms of the GNU General Public License as published by the Free
14
// Software Foundation; either version 2 or (at your option) any later version.
15
//
16
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
17
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
18
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19
// for more details.
20
//
21
// You should have received a copy of the GNU General Public License along
22
// with eCos; if not, write to the Free Software Foundation, Inc.,
23
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24
//
25
// As a special exception, if other files instantiate templates or use macros
26
// or inline functions from this file, or you compile this file and link it
27
// with other works to produce a work based on this file, this file does not
28
// by itself cause the resulting work to be covered by the GNU General Public
29
// License. However the source code for this file must still be made available
30
// in accordance with section (3) of the GNU General Public License.
31
//
32
// This exception does not invalidate any other reasons why a work based on
33
// this file might be covered by the GNU General Public License.
34
//
35
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
36
// at http://sources.redhat.com/ecos/ecos-license/
37
// -------------------------------------------
38
//####ECOSGPLCOPYRIGHTEND####
39
//####UCDSNMPCOPYRIGHTBEGIN####
40
//
41
// -------------------------------------------
42
//
43
// Portions of this software may have been derived from the UCD-SNMP
44
// project,  <http://ucd-snmp.ucdavis.edu/>  from the University of
45
// California at Davis, which was originally based on the Carnegie Mellon
46
// University SNMP implementation.  Portions of this software are therefore
47
// covered by the appropriate copyright disclaimers included herein.
48
//
49
// The release used was version 4.1.2 of May 2000.  "ucd-snmp-4.1.2"
50
// -------------------------------------------
51
//
52
//####UCDSNMPCOPYRIGHTEND####
53
//==========================================================================
54
//#####DESCRIPTIONBEGIN####
55
//
56
// Author(s):    hmt
57
// Contributors: hmt
58
// Date:         2000-05-30
59
// Purpose:      Port of UCD-SNMP distribution to eCos.
60
// Description:  
61
//              
62
//
63
//####DESCRIPTIONEND####
64
//
65
//==========================================================================
66
/********************************************************************
67
       Copyright 1989, 1991, 1992 by Carnegie Mellon University
68
 
69
                          Derivative Work -
70
Copyright 1996, 1998, 1999, 2000 The Regents of the University of California
71
 
72
                         All Rights Reserved
73
 
74
Permission to use, copy, modify and distribute this software and its
75
documentation for any purpose and without fee is hereby granted,
76
provided that the above copyright notice appears in all copies and
77
that both that copyright notice and this permission notice appear in
78
supporting documentation, and that the name of CMU and The Regents of
79
the University of California not be used in advertising or publicity
80
pertaining to distribution of the software without specific written
81
permission.
82
 
83
CMU AND THE REGENTS OF THE UNIVERSITY OF CALIFORNIA DISCLAIM ALL
84
WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
85
WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL CMU OR
86
THE REGENTS OF THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY SPECIAL,
87
INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
88
FROM THE LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
89
CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
90
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
91
*********************************************************************/
92
/*
93
 * lcd_time.c
94
 *
95
 * XXX  Should etimelist entries with <0,0> time tuples be timed out?
96
 * XXX  Need a routine to free the memory?  (Perhaps at shutdown?)
97
 */
98
 
99
#include <config.h>
100
 
101
#ifdef CYGPKG_SNMPAGENT_V3_SUPPORT
102
#include <sys/types.h>
103
#if HAVE_WINSOCK_H
104
#include <winsock.h>
105
#endif
106
#include <stdio.h>
107
#ifdef HAVE_STDLIB_H
108
#include <stdlib.h>
109
#endif
110
#if HAVE_STRING_H
111
#include <string.h>
112
#else
113
#include <strings.h>
114
#endif
115
#if TIME_WITH_SYS_TIME
116
# ifdef WIN32
117
#  include <sys/timeb.h>
118
# else
119
#  include <sys/time.h>
120
# endif
121
# include <time.h>
122
#else
123
# if HAVE_SYS_TIME_H
124
#  include <sys/time.h>
125
# else
126
#  include <time.h>
127
# endif
128
#endif
129
#ifdef HAVE_NETINET_IN_H
130
#include <netinet/in.h>
131
#endif
132
 
133
#if HAVE_DMALLOC_H
134
#include <dmalloc.h>
135
#endif
136
 
137
#include "asn1.h"
138
#include "snmp_api.h"
139
#include "callback.h"
140
#include "snmpusm.h"
141
#include "lcd_time.h"
142
#include "snmp_debug.h"
143
#include "tools.h"
144
#include "scapi.h"
145
 
146
#include "transform_oids.h"
147
 
148
 
149
/*
150
 * Global static hashlist to contain Enginetime entries.
151
 *
152
 * New records are prepended to the appropriate list at the hash index.
153
 */
154
static Enginetime etimelist[ETIMELIST_SIZE];
155
 
156
 
157
 
158
 
159
/*******************************************************************-o-******
160
 * get_enginetime
161
 *
162
 * Parameters:
163
 *      *engineID
164
 *       engineID_len
165
 *      *engineboot
166
 *      *engine_time
167
 *
168
 * Returns:
169
 *      SNMPERR_SUCCESS         Success -- when a record for engineID is found.
170
 *      SNMPERR_GENERR          Otherwise.
171
 *
172
 *
173
 * Lookup engineID and return the recorded values for the
174
 * <engine_time, engineboot> tuple adjusted to reflect the estimated time
175
 * at the engine in question.
176
 *
177
 * Special case: if engineID is NULL or if engineID_len is 0 then
178
 * the time tuple is returned immediately as zero.
179
 *
180
 * XXX  What if timediff wraps?  >shrug<
181
 * XXX  Then: you need to increment the boots value.  Now.  Detecting
182
 *            this is another matter.
183
 */
184
int
185
get_enginetime( u_char  *engineID,
186
                u_int    engineID_len,
187
                u_int   *engineboot,
188
                u_int   *engine_time,
189
                u_int   authenticated)
190
{
191
        int             rval     = SNMPERR_SUCCESS;
192
        time_t          timediff = 0;
193
        Enginetime      e        = NULL;
194
 
195
 
196
 
197
        /*
198
         * Sanity check.
199
         */
200
        if ( !engine_time || !engineboot ) {
201
                QUITFUN(SNMPERR_GENERR, get_enginetime_quit);
202
        }
203
 
204
 
205
        /*
206
         * Compute estimated current engine_time tuple at engineID if
207
         * a record is cached for it.
208
         */
209
        *engine_time = *engineboot = 0;
210
 
211
        if ( !engineID || (engineID_len<=0) ) {
212
                QUITFUN(SNMPERR_GENERR, get_enginetime_quit);
213
        }
214
 
215
        if ( !(e = search_enginetime_list(engineID, engineID_len)) ) {
216
                QUITFUN(SNMPERR_GENERR, get_enginetime_quit);
217
        }
218
 
219
#ifdef LCD_TIME_SYNC_OPT
220
        if (!authenticated || e->authenticatedFlag) {
221
#endif  
222
        *engine_time = e->engineTime;
223
        *engineboot = e->engineBoot;
224
 
225
        timediff = time(NULL) - e->lastReceivedEngineTime;
226
#ifdef LCD_TIME_SYNC_OPT        
227
        }
228
#endif  
229
 
230
        if ( timediff > (int)(ENGINETIME_MAX - *engine_time) ) {
231
                *engine_time = (timediff - (ENGINETIME_MAX - *engine_time));
232
 
233
                /* FIX -- move this check up... should not change anything
234
                 * if engineboot is already locked.  ???
235
                 */
236
                if (*engineboot < ENGINEBOOT_MAX) {
237
                        *engineboot += 1;
238
                }
239
 
240
        } else {
241
                *engine_time += timediff;
242
        }
243
 
244
        DEBUGMSGTL(("lcd_get_enginetime", "engineID "));
245
        DEBUGMSGHEX(("lcd_get_enginetime", engineID, engineID_len));
246
        DEBUGMSG(("lcd_get_enginetime", ": boots=%d, time=%d\n", *engineboot,
247
                  *engine_time));
248
 
249
get_enginetime_quit:
250
        return rval;
251
 
252
}  /* end get_enginetime() */
253
 
254
/*******************************************************************-o-******
255
 * get_enginetime
256
 *
257
 * Parameters:
258
 *      *engineID
259
 *       engineID_len
260
 *      *engineboot
261
 *      *engine_time
262
 *
263
 * Returns:
264
 *      SNMPERR_SUCCESS         Success -- when a record for engineID is found.
265
 *      SNMPERR_GENERR          Otherwise.
266
 *
267
 *
268
 * Lookup engineID and return the recorded values for the
269
 * <engine_time, engineboot> tuple adjusted to reflect the estimated time
270
 * at the engine in question.
271
 *
272
 * Special case: if engineID is NULL or if engineID_len is 0 then
273
 * the time tuple is returned immediately as zero.
274
 *
275
 * XXX  What if timediff wraps?  >shrug<
276
 * XXX  Then: you need to increment the boots value.  Now.  Detecting
277
 *            this is another matter.
278
 */
279
int
280
get_enginetime_ex(      u_char  *engineID,
281
                u_int    engineID_len,
282
                u_int   *engineboot,
283
                u_int   *engine_time,
284
                u_int   *last_engine_time,
285
                u_int   authenticated)
286
{
287
        int             rval     = SNMPERR_SUCCESS;
288
        time_t          timediff = 0;
289
        Enginetime      e        = NULL;
290
 
291
 
292
 
293
        /*
294
         * Sanity check.
295
         */
296
        if ( !engine_time || !engineboot || !last_engine_time) {
297
                QUITFUN(SNMPERR_GENERR, get_enginetime_ex_quit);
298
        }
299
 
300
 
301
        /*
302
         * Compute estimated current engine_time tuple at engineID if
303
         * a record is cached for it.
304
         */
305
        *last_engine_time = *engine_time = *engineboot = 0;
306
 
307
        if ( !engineID || (engineID_len<=0) ) {
308
                QUITFUN(SNMPERR_GENERR, get_enginetime_ex_quit);
309
        }
310
 
311
        if ( !(e = search_enginetime_list(engineID, engineID_len)) ) {
312
                QUITFUN(SNMPERR_GENERR, get_enginetime_ex_quit);
313
        }
314
 
315
#ifdef LCD_TIME_SYNC_OPT
316
        if (!authenticated || e->authenticatedFlag) {
317
#endif  
318
        *last_engine_time = *engine_time = e->engineTime;
319
        *engineboot = e->engineBoot;
320
 
321
        timediff = time(NULL) - e->lastReceivedEngineTime;
322
#ifdef LCD_TIME_SYNC_OPT        
323
        }
324
#endif  
325
 
326
        if ( timediff > (int)(ENGINETIME_MAX - *engine_time) ) {
327
                *engine_time = (timediff - (ENGINETIME_MAX - *engine_time));
328
 
329
                /* FIX -- move this check up... should not change anything
330
                 * if engineboot is already locked.  ???
331
                 */
332
                if (*engineboot < ENGINEBOOT_MAX) {
333
                        *engineboot += 1;
334
                }
335
 
336
        } else {
337
                *engine_time += timediff;
338
        }
339
 
340
        DEBUGMSGTL(("lcd_get_enginetime_ex", "engineID "));
341
        DEBUGMSGHEX(("lcd_get_enginetime_ex", engineID, engineID_len));
342
        DEBUGMSG(("lcd_get_enginetime_ex", ": boots=%d, time=%d\n", *engineboot,
343
                  *engine_time));
344
 
345
get_enginetime_ex_quit:
346
        return rval;
347
 
348
}  /* end get_enginetime_ex() */
349
 
350
 
351
 
352
 
353
/*******************************************************************-o-******
354
 * set_enginetime
355
 *
356
 * Parameters:
357
 *      *engineID
358
 *       engineID_len
359
 *       engineboot
360
 *       engine_time
361
 *
362
 * Returns:
363
 *      SNMPERR_SUCCESS         Success.
364
 *      SNMPERR_GENERR          Otherwise.
365
 *
366
 *
367
 * Lookup engineID and store the given <engine_time, engineboot> tuple
368
 * and then stamp the record with a consistent source of local time.
369
 * If the engineID record does not exist, create one.
370
 *
371
 * Special case: engineID is NULL or engineID_len is 0 defines an engineID
372
 * that is "always set."
373
 *
374
 * XXX  "Current time within the local engine" == time(NULL)...
375
 */
376
int
377
set_enginetime( u_char  *engineID,
378
                u_int    engineID_len,
379
                u_int    engineboot,
380
                u_int    engine_time,
381
                u_int    authenticated)
382
{
383
        int             rval = SNMPERR_SUCCESS,
384
                        iindex;
385
        Enginetime      e = NULL;
386
 
387
 
388
 
389
        /*
390
         * Sanity check.
391
         */
392
        if ( !engineID || (engineID_len <= 0) ) {
393
                return rval;
394
        }
395
 
396
 
397
        /*
398
         * Store the given <engine_time, engineboot> tuple in the record
399
         * for engineID.  Create a new record if necessary.
400
         */
401
        if ( !(e = search_enginetime_list(engineID, engineID_len)) )
402
        {
403
                if ( (iindex = hash_engineID(engineID, engineID_len)) < 0 )
404
                {
405
                        QUITFUN(SNMPERR_GENERR, set_enginetime_quit);
406
                }
407
 
408
                e = (Enginetime) calloc(1,sizeof(*e));
409
 
410
                e->next = etimelist[iindex];
411
                etimelist[iindex] = e;
412
 
413
                e->engineID = (u_char *) calloc(1,engineID_len);
414
                memcpy(e->engineID, engineID, engineID_len);
415
 
416
                e->engineID_len = engineID_len;
417
        }
418
#ifdef LCD_TIME_SYNC_OPT        
419
        if (authenticated || !e->authenticatedFlag) {
420
          e->authenticatedFlag = authenticated;
421
#else
422
        if (authenticated) {
423
#endif
424
          e->engineTime           = engine_time;
425
          e->engineBoot           = engineboot;
426
          e->lastReceivedEngineTime = time(NULL);
427
        }
428
 
429
        e = NULL;       /* Indicates a successful update. */
430
 
431
        DEBUGMSGTL(("lcd_set_enginetime", "engineID "));
432
        DEBUGMSGHEX(("lcd_set_enginetime", engineID, engineID_len));
433
        DEBUGMSG(("lcd_set_enginetime", ": boots=%d, time=%d\n", engineboot,
434
                  engine_time));
435
 
436
set_enginetime_quit:
437
        SNMP_FREE(e);
438
 
439
        return rval;
440
 
441
}  /* end set_enginetime() */
442
 
443
 
444
 
445
 
446
/*******************************************************************-o-******
447
 * search_enginetime_list
448
 *
449
 * Parameters:
450
 *      *engineID
451
 *       engineID_len
452
 *
453
 * Returns:
454
 *      Pointer to a etimelist record with engineID <engineID>  -OR-
455
 *      NULL if no record exists.
456
 *
457
 *
458
 * Search etimelist for an entry with engineID.
459
 *
460
 * ASSUMES that no engineID will have more than one record in the list.
461
 */
462
Enginetime
463
search_enginetime_list(u_char *engineID, u_int engineID_len)
464
{
465
        int             rval = SNMPERR_SUCCESS;
466
        Enginetime      e    = NULL;
467
 
468
 
469
        /*
470
         * Sanity check.
471
         */
472
        if ( !engineID || (engineID_len<=0) ) {
473
                QUITFUN(SNMPERR_GENERR, search_enginetime_list_quit);
474
        }
475
 
476
 
477
        /*
478
         * Find the entry for engineID if there be one.
479
         */
480
        rval = hash_engineID(engineID, engineID_len);
481
        if (rval < 0) {
482
                QUITFUN(SNMPERR_GENERR, search_enginetime_list_quit);
483
        }
484
        e = etimelist[rval];
485
 
486
        for ( /*EMPTY*/; e; e = e->next )
487
        {
488
                if ( (engineID_len == e->engineID_len)
489
                        && !memcmp(e->engineID, engineID, engineID_len) )
490
                {
491
                        break;
492
                }
493
        }
494
 
495
 
496
search_enginetime_list_quit:
497
        return e;
498
 
499
}  /* end search_enginetime_list() */
500
 
501
 
502
 
503
 
504
 
505
/*******************************************************************-o-******
506
 * hash_engineID
507
 *
508
 * Parameters:
509
 *      *engineID
510
 *       engineID_len
511
 *
512
 * Returns:
513
 *      >0                       etimelist index for this engineID.
514
 *      SNMPERR_GENERR          Error.
515
 *
516
 *
517
 * Use a cheap hash to build an index into the etimelist.  Method is
518
 * to hash the engineID, then split the hash into u_int's and add them up
519
 * and modulo the size of the list.
520
 *
521
 */
522
int
523
hash_engineID(u_char *engineID, u_int engineID_len)
524
{
525
        int              rval           = SNMPERR_GENERR;
526
        size_t           buf_len        = SNMP_MAXBUF;
527
        u_int            additive       = 0;
528
        u_char          *bufp,
529
                         buf[SNMP_MAXBUF];
530
        void            *context = NULL;
531
 
532
 
533
 
534
        /*
535
         * Sanity check.
536
         */
537
        if ( !engineID || (engineID_len <= 0) ) {
538
                QUITFUN(SNMPERR_GENERR, hash_engineID_quit);
539
        }
540
 
541
 
542
        /*
543
         * Hash engineID into a list index.
544
         */
545
        rval = sc_hash(usmHMACMD5AuthProtocol,
546
                       sizeof(usmHMACMD5AuthProtocol)/sizeof(oid),
547
                       engineID, engineID_len,
548
                       buf, &buf_len);
549
        QUITFUN(rval, hash_engineID_quit);
550
 
551
        for ( bufp = buf; (bufp-buf) < (int)buf_len; bufp += 4 ) {
552
                additive += (u_int) *bufp;
553
        }
554
 
555
hash_engineID_quit:
556
        SNMP_FREE(context);
557
        memset(buf, 0, SNMP_MAXBUF);
558
 
559
        return (rval < 0) ? rval : (additive % ETIMELIST_SIZE);
560
 
561
}  /* end hash_engineID() */
562
 
563
 
564
 
565
 
566
#ifdef SNMP_TESTING_CODE
567
/*******************************************************************-o-******
568
 * dump_etimelist_entry
569
 *
570
 * Parameters:
571
 *      e
572
 *      count
573
 */
574
void
575
dump_etimelist_entry(Enginetime e, int count)
576
{
577
        u_int    buflen;
578
        char     tabs[SNMP_MAXBUF],
579
                *t = tabs,
580
                *s;
581
 
582
 
583
 
584
        count += 1;
585
        while (count--) {
586
                t += sprintf(t, "  ");
587
        }
588
 
589
 
590
        buflen = e->engineID_len;
591
#ifdef SNMP_TESTING_CODE
592
        if ( !(s = dump_snmpEngineID(e->engineID, &buflen)) ) {
593
#endif
594
                binary_to_hex(e->engineID, e->engineID_len, &s);
595
#ifdef SNMP_TESTING_CODE
596
        }
597
#endif
598
 
599
        DEBUGMSGTL(("dump_etimelist", "%s\n",tabs));
600
        DEBUGMSGTL(("dump_etimelist", "%s%s (len=%d) <%d,%d>\n", tabs,
601
                    s, e->engineID_len,
602
                    e->engineTime, e->engineBoot));
603
        DEBUGMSGTL(("dump_etimelist", "%s%ld (%ld) -- %s", tabs,
604
                    e->lastReceivedEngineTime,
605
                    time(NULL) - e->lastReceivedEngineTime,
606
                    ctime(&e->lastReceivedEngineTime)));
607
 
608
        SNMP_FREE(s);
609
 
610
}  /* end dump_etimelist_entry() */
611
 
612
 
613
 
614
 
615
/*******************************************************************-o-******
616
 * dump_etimelist
617
 */
618
void
619
dump_etimelist(void)
620
{
621
        int             iindex = -1,
622
                        count = 0;
623
        Enginetime      e;
624
 
625
 
626
 
627
        DEBUGMSGTL(("dump_etimelist", "\n"));
628
 
629
        while (++iindex < ETIMELIST_SIZE) {
630
                DEBUGMSG(("dump_etimelist", "[%d]", iindex));
631
 
632
                count = 0;
633
                e = etimelist[iindex];
634
 
635
                while (e) {
636
                        dump_etimelist_entry(e, count++);
637
                        e = e->next;
638
                }
639
 
640
                if (count > 0) {
641
                        DEBUGMSG(("dump_etimelist", "\n"));
642
                }
643
        }  /* endwhile */
644
 
645
        DEBUGMSG(("dump_etimelist", "\n"));
646
 
647
}  /* end dump_etimelist() */
648
#endif /* SNMP_TESTING_CODE */
649
 
650
#endif /* CYGPKG_SNMPAGENT_V3_SUPPORT */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.