File: snmp_util.c

package info (click to toggle)
snmp 3.6-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,284 kB
  • ctags: 1,929
  • sloc: ansic: 18,710; sh: 585; makefile: 311
file content (593 lines) | stat: -rw-r--r-- 13,588 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
 * snmp_utils.c --
 *
 * This file contains helper routines for the agent.
 *
 * Copyright (c) 1995-1997
 *
 * Erik Schoenfelder		TU Braunschweig, Germany
 * Juergen Schoenwaelder	University of Twente, The Netherlands
 * Olivier Montanuy 
 * Dick Moran
 *
 * Permission to use, copy, modify, and distribute this software and its 
 * documentation for any purpose and without fee is hereby granted, 
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in 
 * supporting documentation, and that the name of CMU not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  
 * 
 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 * 
 */

/*
 * void snmp_alarm ( int ival, void (*handler)(void) )
 *
 * provides an mechanism to call routines at regular intervals.
 */

/*
 * register a filedescriptor with  
 *	void fd_add (int fd, void (*function)(int fd));
 * with function
 *	void worker (int fd);
 * the function is called every time a select reports it readable.
 *
 * a NULL worker-function unregisters the filedescriptor.
 */

#include <stdio.h>
#include <unistd.h>
#include <malloc.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>

#include "snmp_util.h"

#ifdef HANDLER_DEBUG
# define dprintf	fprintf
#else
# define dprintf	if (0) fprintf
#endif

/*
 * function calls in intervals hadling:
 */

typedef struct _snmp_alarm_handler {
  int ival;
  struct timeval next_event;
  void (*handler) ();
  struct _snmp_alarm_handler *next;
} snmp_alarm_handler;

static snmp_alarm_handler *all_handler = 0;


#define tv_le(t1, t2)	(((t1)->tv_sec < (t2)->tv_sec) \
	|| ((t1)->tv_sec == (t2)->tv_sec && (t1)->tv_usec < (t2)->tv_usec))

#define tv_msdiff(t1, t2)	(((t1)->tv_sec - (t2)->tv_sec) * 1000 \
	+ ((t1)->tv_usec - (t2)->tv_usec) / 1000)

static void
snmp_alarm_do_handler ()
{
  struct timeval now;
  unsigned int timeo = 99999 * 1000;		/* next timeout in ms */
  snmp_alarm_handler *h = all_handler;
  struct itimerval itv;
  static int in_handler = 0;
  struct sigaction action;

  dprintf (stderr, "do_handler... (%d)\n", in_handler);
  
  if (! in_handler) {
    in_handler = 1;
  } else {
    return;
  }

#if 0
  /* XXX: does not work correct: */
  /* (re)initialize signal handler: */
  if (signal (SIGALRM, snmp_alarm_do_handler) < 0) {
    perror ("snmpd: failed to set SIGALRM handler; reason");
    in_handler = 0;
    return;
  }
#else
  action.sa_handler = snmp_alarm_do_handler;
  sigemptyset(&action.sa_mask);
  action.sa_flags = 0;
  if (sigaction(SIGALRM, &action, NULL) != 0) {
    perror("snmpd: sigaction failed; reason");
    in_handler = 0;
    return;
  }
#endif

  /* shut off the timer: */
  memset (&itv, 0, sizeof(itv));
  if (setitimer (ITIMER_REAL, &itv, (struct itimerval *) 0) < 0) {
    perror ("snmpd: failed to clear interval timer; reason");
  }

  gettimeofday (&now, (struct timezone *) 0);

  while (h) {
    if (tv_le (&h->next_event, &now)) {
      dprintf (stderr, "** working for handler 0x%lx  (%d)...\n", 
	       (long) h->handler, h->ival);
      (*h->handler) ();
      memcpy (&h->next_event, &now, sizeof(now));
      h->next_event.tv_sec += h->ival;
    }

  dprintf (stderr, "now: %u.%06u\n", (unsigned) now.tv_sec,
	   (unsigned) now.tv_usec);
  dprintf (stderr, "nev: %u.%06u\n", (unsigned) h->next_event.tv_sec, 
	   (unsigned) h->next_event.tv_usec);

  dprintf (stderr, "diff: %u\n", (unsigned) tv_msdiff(&h->next_event, &now));

    if (tv_msdiff(&h->next_event, &now) < timeo) {
      timeo = tv_msdiff(&h->next_event, &now);
      if (timeo <= 0) {
	timeo = 1; 
      }
    }
    h = h->next;
  }

  dprintf (stderr, "next event: %u.%06u  (now %u.%06u)  alarm: %u\n",
	   (unsigned) (timeo + now.tv_sec), (unsigned) now.tv_usec,
	   (unsigned) now.tv_sec, (unsigned) now.tv_usec, (unsigned) timeo);

  dprintf (stderr, "setting alarm (%u)\n\n", timeo);

  itv.it_interval.tv_sec = timeo / 1000;
  itv.it_interval.tv_usec = (timeo % 1000) * 1000;
  itv.it_value.tv_sec = itv.it_interval.tv_sec;
  itv.it_value.tv_usec = itv.it_interval.tv_usec;

  if (setitimer (ITIMER_REAL, &itv, (struct itimerval *) 0) < 0) {
    perror ("snmpd: failed to set interval timer; reason");
  }

  in_handler = 0;
}

void
snmp_alarm (ival, handler)
     int ival;
     void (* handler) ();
{
    snmp_alarm_handler *nnew;

    if (! (nnew = (snmp_alarm_handler *) 
	   calloc (1, sizeof (snmp_alarm_handler)))) {
      fprintf (stderr, "snmpd: snmp_alarm: out of memory...\n");
      return;
    }

    /* create new time event handler control structure and chain in: */
    nnew->ival = ival;
    gettimeofday (&nnew->next_event, (struct timezone *) 0);
    nnew->handler = handler;
    nnew->next = all_handler;
    all_handler = nnew;

    dprintf (stderr, "new customer: ival %d\n", ival);

    /* call to work for and set timer: */
    snmp_alarm_do_handler ();
}



/*
 * filedescriptor helper functions:
 */

struct f_handler {
  int fd;
  void (*handler) ();
  struct f_handler *next;
};


#if 1
/*
 * filedescriptor helper functions:
 */
static void (*fd_allhandler[FD_SETSIZE]) ();
static fd_set fd_allmask;
static int fd_maxfds = 0;

/*
 * register a function to work for.
 * a NULL func deregisters the handler.
 */
void 
fd_add (fd, func)
     int fd;
     void (*func)();
{

  if (fd >= FD_SETSIZE) { 
    printf ("fd_add:  file number too large for select mask\n");
    return;
  }
  fd_allhandler[fd]  = func;
  if (func)
    FD_SET(fd, &fd_allmask);
  else
    FD_CLR(fd, &fd_allmask);
  fd_maxfds = fd_maxfds < fd ? fd : fd_maxfds;
}


/*
 * service loop; run by the agent.
 */
void
fd_service ()
{
  fd_set fdset;
  int count;
  int i;

  while (1)
  {
    memcpy (&fdset, &fd_allmask, sizeof(fd_allmask));
    count = select (fd_maxfds + 1, &fdset, 0, 0, 0);

    if (count < -1) {
      printf ("snmpd: select returned %d - aborting.\n", count);
      return;
    } else if (count == -1) {
      if (errno == EINTR) {
	continue;
      } else {
	perror ("snmpd: select failed; reason"); 
	return; 
      }
    } else {
      for (i = 0; count > 0 && i <= fd_maxfds; i++) {
	if (FD_ISSET(i, &fdset)) {
	  count--;
	  if (fd_allhandler[i]) {
	    (*fd_allhandler[i])(i);
	  }
	}
      }
    }
  }
}

#else

/* old code; will be removed */

static struct f_handler *fd_allhandler = 0;
static fd_set fd_allmask;
static int fd_maxfds = 0;

/*
 * register a function to work for:
 */
void
fd_add (fd, func)
     int fd;
     void (*func)();
{
  struct f_handler *nnew = (struct f_handler *) 
    malloc (sizeof(struct f_handler));

  if (! nnew) {
    fprintf (stderr, "snmpd: out of memory... - ignored\n");
    return;
  }

  nnew->fd = fd;
  nnew->handler = func;
  nnew->next = fd_allhandler;
  fd_allhandler = nnew;

  FD_SET(fd, &fd_allmask);
  fd_maxfds = fd_maxfds < fd ? fd : fd_maxfds;
}


/*
 * service loop; run by the agent.
 */
void
fd_service ()
{
  fd_set fdset;
  int count;

  while (1) {

    memcpy (&fdset, &fd_allmask, sizeof(fd_allmask));
    count = select (fd_maxfds + 1, &fdset, 0, 0, 0);

    if (count > 0){
      struct f_handler *fhp = fd_allhandler;
      while (fhp) {
	if (FD_ISSET(fhp->fd, &fdset)) {
	  (*fhp->handler) (fhp->fd);
	}
	fhp = fhp->next;
      }

    } else {
      switch (count) {
      case 0:
	break;
      case -1:
	if (errno == EINTR){
	  continue;
	} else {
	  perror ("snmpd: select failed; reason");
	}
	return;
      default:
	printf ("snmpd: select returned %d - aborting.\n", count);
	return;
      }
    }
  }
}

#endif




/* ---------------------------------------------------------------------- */


/*
** Various utilities, by O.Montanuy.
** this code is public domain, except for the bits borrowed from CMU-SNMP.
*/

#include <sys/socket.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifndef linux
# include <nlist.h>
#else
# include <netinet/in.h>
# include <arpa/inet.h>
#endif
#include "asn1.h"
#include "snmp_api.h"
#include "snmp.h"
#include "snmp_impl.h"
#include "mib.h"
#include "snmp_client.h"
#include "snmp_vars.h"
#include "snmp_config.h"

/*
** Function to safely copy a string, and ensure the last
** character is always '\0'.
*/
void
strcpy_safe(str, str_len, val)
     char *str; int str_len; char *val;
{
  int val_len = 0;
  if((str == NULL)||(str_len < 0)) return ; 
  if(val != NULL) /* Andy sez: only paranoiacs survive. */
    {
      val_len = strlen(val);
      if(val_len >= str_len) val_len--;
      strncpy( str, val, val_len);
    }
  str[val_len]='\0'; /* terminate with a zero */
}


/*
** This function is copied from snmptrap.c (Copyright from CMU)
*/
#ifndef IFF_LOOPBACK
#define IFF_LOOPBACK 0
#endif
#define LOOPBACK    0x7f000001
#define NUM_NETWORKS	16   /* max number of interfaces to check */
u_long 
Util_local_ip_address()
{
  int sd;
  struct ifconf ifc;
  struct ifreq conf[NUM_NETWORKS], *ifrp, ifreq;
  struct sockaddr_in *in_addr;
  int count,interfaces;		/* number of interfaces returned by ioctl */
  if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) return 0;
  ifc.ifc_len = sizeof(conf);
  ifc.ifc_buf = (caddr_t)conf;
  if (ioctl(sd, SIOCGIFCONF, (char *)&ifc) < 0) { close(sd); return 0; }
  ifrp = ifc.ifc_req;
  interfaces = ifc.ifc_len / sizeof(struct ifreq);
  for(count = 0; count < interfaces; count++, ifrp++)
    {
      ifreq = *ifrp;
      if (ioctl(sd, SIOCGIFFLAGS, (char *)&ifreq) < 0) continue;
      in_addr = (struct sockaddr_in *)&ifrp->ifr_addr;
      if ((ifreq.ifr_flags & IFF_UP)
	  && (ifreq.ifr_flags & IFF_RUNNING)
	  && !(ifreq.ifr_flags & IFF_LOOPBACK)
	  && in_addr->sin_addr.s_addr != LOOPBACK)
	{ close(sd); return in_addr->sin_addr.s_addr;}
    }
  close(sd);
  return 0;
}
/*
** Get time in seconds
*/
long 
Util_time_now()
{
  struct timeval now;
  gettimeofday(&now, NULL);
  return now.tv_sec;
}
/*
** This function copied from snmptrap.c (Copyright from CMU)
** Returns uptime in centiseconds(!).
*/
#ifndef linux
static struct nlist nl[] = {
    { "_boottime" },
    { "" }
};
#endif

long 
Util_time_running()
{
#ifndef linux
    struct timeval boottime, now, diff;
    int kmem;
    if ((kmem = open("/dev/kmem", 0)) < 0) return 0;
    nlist("/vmunix", nl);
    if (nl[0].n_type == 0){ close(kmem);return 0;}
    lseek(kmem, (long)nl[0].n_value, L_SET);
    read(kmem, &boottime, sizeof(boottime));
    close(kmem);
    gettimeofday(&now, 0);
    now.tv_sec--;
    now.tv_usec += 1000000L;
    diff.tv_sec = now.tv_sec - boottime.tv_sec;
    diff.tv_usec = now.tv_usec - boottime.tv_usec;
    if (diff.tv_usec > 1000000L){diff.tv_usec -= 1000000L;diff.tv_sec++;}
    return ((diff.tv_sec * 100) + (diff.tv_usec / 10000));
#else /* linux */
    long uptim = 0, a, b;
    FILE *in = fopen ("/proc/uptime", "r");
    if (in)
      {
	if (2 == fscanf (in, "%ld.%ld", &a, &b)) uptim = a * 100 + b;
	fclose (in);
      }
    return uptim;
#endif /* linux */
}
/*
** Size of minimum file chunk
*/
#define FILE_CHUNK  (0x1000)
/*
** Read binary data from file
**   file         = file name
**   offset       = offset in file (zero = beginning of file)
**   data[dataSz] = buffer where to store data
**  returns number of bytes read
*/
int
Util_file_read(file, offset, data, dataSz)
     char *file; int offset; char *data; int dataSz;
{
  int fd;
  if((file==NULL)||(data==NULL)||(dataSz<=0))
    { return 0; }
  fd = open(file, O_RDONLY);
  if(fd < 0)
    {
      printf("Cannot open file %s\n", file);
      return -1; 
    }
  if(offset > 0)
    { lseek(fd, offset, SEEK_SET); }
  {
    int pos, sz;
    struct stat file_stat;
    if(fstat(fd, &file_stat) < 0)
    {
      printf("Cannot read size of %s\n", file);
      return -1;
    }
    if(file_stat.st_size< dataSz) 
      { dataSz= file_stat.st_size; }
    for(pos=0; pos< dataSz; pos+=sz)
      {
	sz = dataSz - pos;
	if(sz > FILE_CHUNK) 
	  sz = FILE_CHUNK;
	if( read(fd, &data[pos], sz)!=sz) 
	  break;
      }
    close(fd);
    if(pos < dataSz)
      {
	printf("Cannot read file %s\n",file);
	return -1;
      }
    return dataSz;
  }
}
/*
** Write binary data into file
**   file         = file name
**   offset       = offset in file (zero = beginning of file)
**   data[dataSz] = buffer where to get data from
**  return 1 if write was ok, -1 else.
*/
int
Util_file_write(file, offset, data, dataSz)
     char *file; int offset; char *data; int dataSz;
{
  int fd;
  if((file==NULL)||(data==NULL)||(dataSz<=0))
    { return 0; }
  fd = open(file, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  if(fd<0)
    {
      printf("Cannot open file %s\n", file);
      return -1; 
    }
  if(offset > 0)
    { lseek(fd, offset, SEEK_SET); }
  {
    int pos, sz;
    for(pos=0; pos< dataSz; pos+=sz)
      {
	sz = dataSz - pos;
	if(sz > FILE_CHUNK) 
	  sz = FILE_CHUNK;
	if( write(fd, &data[pos], sz)!=sz) 
	  break;
      }
    close(fd);
    if(pos < dataSz)
      {
	printf("Cannot write file %s\n",file);
	return -1;
      }
  }
  return 1;
}


/* ---------------------------------------------------------------------- */