This repository has been archived by the owner on Feb 22, 2019. It is now read-only.
/
snmp_lib_callback.c
201 lines (169 loc) · 4.64 KB
/
snmp_lib_callback.c
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
/*
* 15.07.02: Ported to VMS
*/
/*
* callback.c: A generic callback mechanism
*/
#ifdef __vms
#include "net-snmp-config.h"
#else
#include <net-snmp/net-snmp-config.h>
#endif
#include <sys/types.h>
#include <stdio.h>
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_WINSOCK_H
#include <winsock.h>
#endif
#if HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#if HAVE_DMALLOC_H
#include <dmalloc.h>
#endif
#ifdef __vms
#include "snmp_types.h"
#include "snmp_output_api.h"
#include "snmp_utilities.h"
#include "snmp_lib_callback.h"
#include "snmp_api.h"
#else
#include <net-snmp/types.h>
#include <net-snmp/output_api.h>
#include <net-snmp/utilities.h>
#include <net-snmp/library/callback.h>
#include <net-snmp/library/snmp_api.h>
#endif
static struct snmp_gen_callback
*thecallbacks[MAX_CALLBACK_IDS][MAX_CALLBACK_SUBIDS];
/*
* the chicken. or the egg. You pick.
*/
void
init_callbacks(void)
{
/*
* probably not needed? Should be full of 0's anyway?
*/
/*
* (poses a problem if you put init_callbacks() inside of
* init_snmp() and then want the app to register a callback before
* init_snmp() is called in the first place. -- Wes
*/
/*
* memset(thecallbacks, 0, sizeof(thecallbacks));
*/
DEBUGMSGTL(("callback", "initialized\n"));
}
int
snmp_register_callback(int major, int minor, SNMPCallback * new_callback,
void *arg)
{
struct snmp_gen_callback *newscp = NULL, *scp = NULL;
struct snmp_gen_callback **prevNext = &(thecallbacks[major][minor]);
if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
return SNMPERR_GENERR;
}
if ((newscp = SNMP_MALLOC_STRUCT(snmp_gen_callback)) == NULL) {
return SNMPERR_GENERR;
} else {
newscp->sc_client_arg = arg;
newscp->sc_callback = new_callback;
newscp->next = NULL;
for (scp = thecallbacks[major][minor]; scp != NULL;
scp = scp->next) {
prevNext = &(scp->next);
}
*prevNext = newscp;
DEBUGMSGTL(("callback", "registered (%d,%d) at %p\n", major, minor,
newscp));
return SNMPERR_SUCCESS;
}
}
int
snmp_call_callbacks(int major, int minor, void *caller_arg)
{
struct snmp_gen_callback *scp;
unsigned int count = 0;
if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
return SNMPERR_GENERR;
}
DEBUGMSGTL(("callback", "START calling callbacks for maj=%d min=%d\n",
major, minor));
/*
* for each registered callback of type major and minor
*/
for (scp = thecallbacks[major][minor]; scp != NULL; scp = scp->next) {
DEBUGMSGTL(("callback", "calling a callback for maj=%d min=%d\n",
major, minor));
/*
* call them
*/
(*(scp->sc_callback)) (major, minor, caller_arg,
scp->sc_client_arg);
count++;
}
DEBUGMSGTL(("callback",
"END calling callbacks for maj=%d min=%d (%d called)\n",
major, minor, count));
return SNMPERR_SUCCESS;
}
int
snmp_count_callbacks(int major, int minor)
{
int count = 0;
struct snmp_gen_callback *scp;
if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
return SNMPERR_GENERR;
}
for (scp = thecallbacks[major][minor]; scp != NULL; scp = scp->next) {
count++;
}
return count;
}
int
snmp_callback_available(int major, int minor)
{
if (major >= MAX_CALLBACK_IDS || minor >= MAX_CALLBACK_SUBIDS) {
return SNMPERR_GENERR;
}
if (thecallbacks[major][minor] != NULL) {
return SNMPERR_SUCCESS;
}
return SNMPERR_GENERR;
}
int
snmp_unregister_callback(int major, int minor, SNMPCallback * target,
void *arg, int matchargs)
{
struct snmp_gen_callback *scp = thecallbacks[major][minor];
struct snmp_gen_callback **prevNext = &(thecallbacks[major][minor]);
int count = 0;
while (scp != NULL) {
if ((scp->sc_callback == target) &&
(!matchargs || (scp->sc_client_arg == arg))) {
DEBUGMSGTL(("callback", "unregistering (%d,%d) at %p\n", major,
minor, scp));
*prevNext = scp->next;
SNMP_FREE(scp);
scp = *prevNext;
count++;
} else {
prevNext = &(scp->next);
scp = scp->next;
}
}
return count;
}
struct snmp_gen_callback *
snmp_callback_list(int major, int minor)
{
return (thecallbacks[major][minor]);
}