Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / net-snmp / apps / snmptrapd_handlers.c
1 #include <net-snmp/net-snmp-config.h>
2
3 #if HAVE_STDLIB_H
4 #include <stdlib.h>
5 #endif
6 #if HAVE_UNISTD_H
7 #include <unistd.h>
8 #endif
9 #include <stdio.h>
10 #if HAVE_STRING_H
11 #include <string.h>
12 #else
13 #include <strings.h>
14 #endif
15 #include <sys/types.h>
16 #if HAVE_WINSOCK_H
17 #include <winsock.h>
18 #else
19 #include <netinet/in.h>
20 #include <netdb.h>
21 #endif
22
23 #include <net-snmp/config_api.h>
24 #include <net-snmp/output_api.h>
25 #include <net-snmp/mib_api.h>
26 #include <net-snmp/utilities.h>
27
28 struct traphandle {
29     char           *exec;
30     oid             trap[MAX_OID_LEN];
31     size_t          traplen;
32     struct traphandle *next;
33 };
34
35 struct traphandle *traphandlers = 0;
36 struct traphandle *defaulthandler = 0;
37
38 /*
39  * handles parsing .conf lines of: 
40  */
41 /*
42  * traphandle OID EXEC           
43  */
44
45 char           *
46 snmptrapd_get_traphandler(oid * name, size_t namelen)
47 {
48     struct traphandle **ttmp;
49     DEBUGMSGTL(("snmptrapd:traphandler", "looking for trap handler for "));
50     DEBUGMSGOID(("snmptrapd:traphandler", name, namelen));
51     DEBUGMSG(("snmptrapd:traphandler", "...\n"));
52     for (ttmp = &traphandlers;
53          *ttmp != NULL
54          && snmp_oid_compare((*ttmp)->trap, (*ttmp)->traplen, name,
55                              namelen); ttmp = &((*ttmp)->next));
56     if (*ttmp == NULL) {
57         if (defaulthandler) {
58             DEBUGMSGTL(("snmptrapd:traphandler",
59                         "  None found, Using the default handler.\n"));
60             return defaulthandler->exec;
61         }
62         DEBUGMSGTL(("snmptrapd:traphandler", "  Didn't find one.\n"));
63         return NULL;
64     }
65     DEBUGMSGTL(("snmptrapd:traphandler", "  Found it!\n"));
66     return (*ttmp)->exec;
67 }
68
69 void
70 snmptrapd_traphandle(const char *token, char *line)
71 {
72     struct traphandle **ttmp;
73     char            buf[STRINGMAX];
74     char           *cptr;
75     int             doingdefault = 0;
76
77     /*
78      * find the current one, if it exists 
79      */
80     if (strncmp(line, "default", 7) == 0) {
81         ttmp = &defaulthandler;
82         doingdefault = 1;
83     } else {
84         for (ttmp = &traphandlers; *ttmp != NULL; ttmp = &((*ttmp)->next));
85     }
86
87     if (*ttmp == NULL) {
88         /*
89          * it doesn't, so allocate a new one. 
90          */
91         *ttmp = (struct traphandle *) malloc(sizeof(struct traphandle));
92         if (!*ttmp) {
93             config_perror("malloc failed");
94             return;
95         }
96         memset(*ttmp, 0, sizeof(struct traphandle));
97     } else {
98         if ((*ttmp)->exec)
99             free((*ttmp)->exec);
100     }
101     cptr = copy_nword(line, buf, sizeof(buf));
102     if (!doingdefault) {
103         (*ttmp)->traplen = MAX_OID_LEN;
104         if (!read_objid(buf, (*ttmp)->trap, &((*ttmp)->traplen))) {
105             char            buf1[STRINGMAX];
106             snprintf(buf1,  sizeof(buf1),
107                     "Bad trap OID in traphandle directive: %s", buf);
108             buf1[ sizeof(buf1)-1 ] = 0;
109             config_perror(buf1);
110             return;
111         }
112     }
113
114     (*ttmp)->exec = strdup(cptr);
115     DEBUGMSGTL(("read_config:traphandler", "registered handler for: "));
116     if (doingdefault) {
117         DEBUGMSG(("read_config:traphandler", "default"));
118     } else {
119         DEBUGMSGOID(("read_config:traphandler", (*ttmp)->trap,
120                      (*ttmp)->traplen));
121     }
122     DEBUGMSG(("read_config:traphandler", "\n"));
123 }