Deleted Added
sdiff udiff text old ( 216294 ) new ( 216594 )
full compact
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.

--- 13 unchanged lines hidden (view full) ---

29 * $Begemot: bsnmp/snmpd/trap.c,v 1.9 2005/10/04 11:21:39 brandt_h Exp $
30 *
31 * TrapSinkTable
32 */
33#include <sys/types.h>
34#include <sys/queue.h>
35#include <sys/sysctl.h>
36#include <sys/un.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <stdarg.h>
40#include <stdarg.h>
41#include <string.h>
42#include <ctype.h>
43#include <syslog.h>
44#include <unistd.h>
45#include <netinet/in.h>
46#include <arpa/inet.h>
47
48#include "snmpmod.h"
49#include "snmpd.h"
50#include "tree.h"
51#include "oid.h"
52
53struct trapsink_list trapsink_list = TAILQ_HEAD_INITIALIZER(trapsink_list);
54
55static const struct asn_oid oid_begemotTrapSinkTable =
56 OIDX_begemotTrapSinkTable;
57static const struct asn_oid oid_sysUpTime = OIDX_sysUpTime;
58static const struct asn_oid oid_snmpTrapOID = OIDX_snmpTrapOID;
59
60struct trapsink_dep {
61 struct snmp_dependency dep;
62 u_int set;

--- 330 unchanged lines hidden (view full) ---

393 case LEAF_begemotTrapSinkVersion:
394 value->v.integer = t->version;
395 break;
396
397 }
398 return (SNMP_ERR_NOERROR);
399}
400
401void
402snmp_send_trap(const struct asn_oid *trap_oid, ...)
403{
404 struct snmp_pdu pdu;
405 struct trapsink *t;
406 const struct snmp_value *v;
407 va_list ap;
408 u_char *sndbuf;
409 size_t sndlen;
410 ssize_t len;
411
412 TAILQ_FOREACH(t, &trapsink_list, link) {
413 if (t->status != TRAPSINK_ACTIVE)
414 continue;
415 memset(&pdu, 0, sizeof(pdu));
416 strcpy(pdu.community, t->comm);
417 if (t->version == TRAPSINK_V1) {
418 pdu.version = SNMP_V1;
419 pdu.type = SNMP_PDU_TRAP;
420 pdu.enterprise = systemg.object_id;
421 memcpy(pdu.agent_addr, snmpd.trap1addr, 4);
422 pdu.generic_trap = trap_oid->subs[trap_oid->len - 1] - 1;
423 pdu.specific_trap = 0;
424 pdu.time_stamp = get_ticks() - start_tick;
425
426 pdu.nbindings = 0;
427 } else {
428 pdu.version = SNMP_V2c;
429 pdu.type = SNMP_PDU_TRAP2;
430 pdu.request_id = reqid_next(trap_reqid);
431 pdu.error_index = 0;
432 pdu.error_status = SNMP_ERR_NOERROR;
433
434 pdu.bindings[0].var = oid_sysUpTime;
435 pdu.bindings[0].var.subs[pdu.bindings[0].var.len++] = 0;
436 pdu.bindings[0].syntax = SNMP_SYNTAX_TIMETICKS;
437 pdu.bindings[0].v.uint32 = get_ticks() - start_tick;
438
439 pdu.bindings[1].var = oid_snmpTrapOID;
440 pdu.bindings[1].var.subs[pdu.bindings[1].var.len++] = 0;
441 pdu.bindings[1].syntax = SNMP_SYNTAX_OID;
442 pdu.bindings[1].v.oid = *trap_oid;
443
444 pdu.nbindings = 2;
445 }
446
447 va_start(ap, trap_oid);
448 while ((v = va_arg(ap, const struct snmp_value *)) != NULL)
449 pdu.bindings[pdu.nbindings++] = *v;
450 va_end(ap);
451
452 if ((sndbuf = buf_alloc(1)) == NULL) {
453 syslog(LOG_ERR, "trap send buffer: %m");
454 return;
455 }
456
457 snmp_output(&pdu, sndbuf, &sndlen, "TRAP");
458
459 if ((len = send(t->socket, sndbuf, sndlen, 0)) == -1)
460 syslog(LOG_ERR, "send: %m");
461 else if ((size_t)len != sndlen)
462 syslog(LOG_ERR, "send: short write %zu/%zu",
463 sndlen, (size_t)len);
464
465 free(sndbuf);
466 }
467}